If Statements and Conditions
Ever wanted to compare two different items and do something with this info? You can use if statements!
If statements run a block of code depending on whether a condition is true. In this example, the condition is "num > 2". In reality, any Boolean or condition can be in there. Even if you just put "true" as the condition, the if-statement won't mind. As the condition inside the if-statement, num > 2 is true, the code inside the if-statement is run.
int num = 3;
if (num > 2) {
System.out.println("This number is in fact greater than 2.");
}
/* output:
* This number is in fact greater than 2.
*/
Well— what if the number is less than or equal to 2, and we want something else to happen? Let's say... if the number is greater than 0, then we can do something else. We could write another if-statement, like so. There is a problem, though.
int num = 3;
if (num > 2) {
System.out.println("This number is in fact greater than 2.");
}
if (num > 0) {
System.out.println("This number is in fact greater than 0.");
}
/* output:
* 3 is in fact greater than 2.
* This number is in fact greater than 0.
*/
Notice how both if-statements are running. I mean- both of their conditions are true, so this makes sense. What if you only wanted to print the second line ONLY if the number is less than or equal to 2, but greater than 0? We can use else-if statements!
Else-If Statements
Essentially, they are just an if-statement chained under the first one, that acts as a backup or second condition. If the if-statement above doesn't run, else-if statements check their own conditions to see if they are true.
int num = 1;
if (num > 2) {
System.out.println("This number is in fact greater than 2.");
}
if (num > 0) {
System.out.println("This number is in fact greater than 0.");
}
/* output:
* This number is in fact greater than 0.
*/
Else-if statements are similar to if-statements but run when the previous conditions are false. They provide additional criteria for the code. In the code above, the else-if statement will run if num is greater than 2 and less than 7.
You can stack else-if statements, creating a chain of if/else-if statements. These are useful to check for a bunch of different scenarios.
int num = 10;
if (num > 2) {
System.out.println("3 is in fact greater than 2.");
} else if (num < 7) {
System.out.println("num is probably greater than 7");
} else if (num < 25) {
System.out.println("help me i cant think of anything mildly interesting for this part");
}
/* output:
* help me i cant think of anything mildly interesting for this part
*/
And if none of these work, we can just use else-statements.
Else Statements
Else statements are like the catch-all statement. If somehow the rest of the if statements don't run, this is run instead. (If you want to think of it like this, else statements are basically else if (true)
)
int num = 10;
if (num > 2) {
System.out.println("3 is in fact greater than 2.");
} else if (num < 7) {
System.out.println("num is probably greater than 7");
} else if (num < 25) {
System.out.println("help me i cant think of anything mildly interesting for this part");
} else {
System.out.println("wow number is pretty big or pretty small i think");
}
/* output:
* wow number is pretty big or pretty small i think
*/
As you can see, the else-statement is ran instead of the other conditions, as the other ones were false.
Last updated