If Statements

If statements are tools which complete different actions based on the state of a boolean. Else statements and else-if statements are optional follow-ups to if statements. If statements are actions which only occur if their boolean is true. If the boolean is false, then the code in the brackets is ignored. Here is the if statement syntax:

``````    if (BOOLEAN) {

...

}
``````

If statements are very useful because they serve to make situational decisions. Here is an example:

``````    if (a % 2 == 0) {

System.out.print("even");

}
``````

This if statement will print “even” if the number a is even, but it will do nothing otherwise.

Else and else-if statements are actions which must follow an if statement and will only have an effect if the previous if statement’s boolean is false. Else-if statements have a secondary boolean which must be true for the content to enact while else statements will enact whenever the previous if statement’s boolean is false. Here are else-if and else statement syntaxes:

``````    else if (BOOLEAN) {

...

} else {

...

}
``````

Else-if and else statements are useful because they make situational actions according to different booleans and whether or not the first boolean was true. Here is an example:

``````    if (a > 0) {

System.out.print("a is positive");

} else if (a < 0) {

System.out.print("a is negative");

} else {

System.out.print("a equals 0");

}
``````

This series of statements will print “a is positive” if a > 0, “a is negative” if a < 0, or “a equals 0” if neither.

If, else-if, and else statements can make large chains of situational actions. Within these chains, there can be an infinite number of else-if statements; however, at least one original if statement is needed to head the chain and only one optional else statement is allowed to finish the chain. Here is an example of such a chain:

``````    if (a == 0) {

System.out.print("a equals 0");

} else if (a == 1) {

System.out.print("a equals 1");

} else if (a == 2) {

System.out.print("a equals 2");

} else if (a == 3) {

System.out.print("a equals 3");

} else {

System.out.print("a does not equal 0, 1, 2, or 3");

}
``````

The program will exit the chain once any of the statements activates or all statements fail to do so.

If statements can also nest, so that multiple situations with multiple booleans can be tested. Here is an example:

``````    if (a > 0) {

if (b > 0) {

...

}

if (b <= 0) {

...

}

}
``````

Nested if statements greatly increase the functionality of if statements, and allows large amounts of conditional information to be dealt with quickly.

Lesson Quiz

1. What will the below code print if a is 2?

``````    if (a == 3) {

System.out.print("3");

} else {

System.out.print("else");

}
``````
a. `3`
b. `else`
c. Nothing
d. `3else`

2. What will the above code print if b > 0 and z < 9?

``````    if (b < 0) {

if (z > 9) {

System.out.print("A");

} else if (z < 9) {

System.out.print("B");

} else {

System.out.print("C");

}

}
``````
a. `A`
b. `B`
c. `C`
d. Nothing

Written by James Richardson

Notice any mistakes? Please email us at [email protected] so that we can fix any inaccuracies.