Java If-else Statement
The Java if statement is used to test the condition. It checks boolean condition: true or false.
For example, if a number is greater than zero then we want to print “Positive Number” but if it is less than zero then we want to print “Negative Number”. In this case we have two print statements in the program, but only one print statement executes at a time based on the input value.
There are various types of if statement in java.
a) if statement
b) nested if statement
c) if-else statement
d) if-else-if statement
a) if statement
If statement consists a condition, followed by statement or a set of statements as shown below:
Syntax:
if(condition){
//code to be executed
}
The statements gets executed only when the given condition is true. If the condition is false then the statements inside if statement body will not be executed.
Example:
public class IfStatementExample {
public static void main(String args[]){
int num=250;
if( num < 300 ){
/* This println statement will only execute,
* if the above condition is true
*/
System.out.println("number is less than 300");
}
}
}
Output:
number is less than 300
b) nested if statement
When there is an if statement inside another if statement then it is called the nested if statement.
Syntax
if(condition_1) {
Statement1(s);
if(condition_2) {
Statement2(s);
}
}
Example:
public class NestedIfExample {
public static void main(String args[]){
int num=70;
if( num < 100 ){
System.out.println("number is less than 100");
if(num > 50){
System.out.println("number is greater than 50");
}
}
}
}
Output:
number is less than 100
number is greater than 50
c) If else statement
The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.
Syntax:
if(condition) {
Statement(s);
}
else {
Statement(s);
}
The statements inside “if” would execute if the condition is true, and the statements inside “else” would execute if the condition is false.
Example:
public class IfElseExample {
public static void main(String[] args) {
int number=13;
if(number%2==0){
System.out.println("even number");
}else{
System.out.println("odd number");
}
}
}
Output:
odd number
d) if-else-if Statement
The if-else-if statement executes one condition from multiple statements. In this statement we have only one “if” and one “else”, however we can have multiple “else if”. It is also known as if else if ladder.
Syntax:
if(condition_1) {
/*if condition_1 is true execute this*/
statement(s);
}
else if(condition_2) {
/* execute this if condition_1 is not met and
* condition_2 is met
*/
statement(s);
}
else if(condition_3) {
/* execute this if condition_1 & condition_2 are
* not met and condition_3 is met
*/
statement(s);
}
...
else {
/* if none of the condition is true
* then these statements gets executed
*/
statement(s);
}
NOTE: In if-else-if statement, as soon as the condition is met, the corresponding set of statements get executed, rest gets ignored. If none of the condition is met then the statements inside “else” gets executed.
Example:
public class Sample {
public static void main(String args[]) {
int a = 30, b = 30;
if (b > a) {
System.out.println("b is greater");
}
else if(a > b){
System.out.println("a is greater");
}
else {
System.out.println("Both are equal");
}
}
}
Output:
Both are equal