Today, you will learn about conditional logic in Java and how to use if
statements.
if
statementBelow, is the syntax for an if-then statement in Java:
if (condition) {
commands
}
The condition will either return true
or false
. If the condition is true
, the commands within the if
statement will execute. If the condition is false
, the statements will not execute and the program will move on to the next lines of code.
Example
class main {public static void main( String args[] ) {int num = 15;if(num < 10){System.out.println("num is less than 10");}if(num > 10){System.out.println("num is greater than 10");}}}
In the example above, we have an integer variable called num
with a value of . The first if
statement checks if num
is less than . Since is not less than , the command within the first if
statement will not execute.
The next if
statement checks whether num
is greater than . Because is greater than , the print statement nested below will be executed.
if
… else
statementif (condition) {
commands
} else {
commands
}
Looking at the syntax above, the if
statement will check a condition. If the condition is true
, the commands nested under the if
statement will be executed. Then, it will skip past the else
statement.
However, if the condition in the if
statement is false
, the commands under the else
statement will be executed.
Example
class main {public static void main( String args[] ) {int num = 15;if (num < 10) {System.out.println("num is less than 10");} else {System.out.println("num is greater than 10");}}}
In the example above, we once again have an integer variable called num
. The if
statement checks whether num
is less than . Because num
has a value of , the print statement nested below the if
statement will not execute.
Because the condition in the if
statement returned false
, the command under the else
statement will run and print out, num is greater than 10
.
If you are looking for a simplified way to write an if
… else
statement, consider reading about ternary operators in Java.
if
… else if
… else
statementif (condition) {
commands
} else if (condition) {
commands
} else (condition) {
commands
}
When working with if
… else if
… else
statements, the first if
statement will check a condition. If the condition is true
, the commands nested below it will run and then skip past the else if
and else
statements.
However, if the if
condition is false
, the program will move onto the else if
statement and evaluate its condition. If the condition is true
, the commands nested below it will execute and the program will skip past the else
statement. If the condition is false
, the commands nested under the else
statement will execute.
class main {public static void main( String args[] ) {int num = 20;if (num < 10) {System.out.println("num is less than 10");} else if (num > 10 && num <= 15) {System.out.println("num is greater than 10 and less than 15");} else {System.out.println("num is greater than 15");}}}
It’s also possible to have multiple else if
statements if you would like to handle more cases.
If you’re interested in learning about other conditional statements, check out our Edpresso shot about the switch statement in Java.
Free Resources