Java If statement Tutorials - Java If ElseIF Switch Tutorial
Java If-Else-Switch
In java the if statement executes a piece of code, if the specified expression in the piece of code is true. If the value is false execution continues with the rest of the program.
The if statement in its simplest configuration takes the form
if (condition) {
statements;
}
if statements are used to check conditions, result can either be true or false.
Example:
public class IfDemo {
public static void main(String[] args) {
int numA = 60, numB = 40;
if (numA > numB)
System.out.println("numA > numB");
}
}
The above will output numA > numB as 60 is greater than 40.
The if-else Statement:
if-else statement is their simplest form look like:
if (condition) {
statements;
} else {
statements;
}
The only difference here is that If the statements in the if statement fails, the statements in the else block are executed.
Let's see an example:
public class IfElseDemo {
public static void main(String[] args) {
int numA = 20, numB = 40;
if (numA > numB)
System.out.println("numA > numB");
}else{
System.out.println("numA < numB");
}
}
In the above example the output would be numA < numB as 20 is less than 40.
![]() |
![]() |

