Java if???else Statement
The if-else
statement is used to execute the block of codes, if the specified condition is satisfied.
if Statement
Syntax for if statement
if(condition){
// block of codes.
}
Here, the if
statement takes a condition
and if the condition
is satisfied, then it will execute the block of codes.
Example for if statement
class Main {
public static void main(String[] args) {
int age = 19;
// checks if number is less than 0
if (age >= 18) {
System.out.println("Welcome");
}
System.out.println("End");
}
}
Output
Welcome
End
In the above program, the value of age
variable is 17. The if
statement checks whether age
is greater than or equal to 18
.
If the condition is true, the block of code inside it will execute; if not, it is skipped.
if-else Statement
The if-else
statement is used to execute the if
block statement(s); if the condition is satisfied. If the condition is not satisfied, then it will execute the else
block statement(s).
Syntax for if-else statement
if(condition){
// block of codes
}
else {
// block of codes
}
Example program for if-else statement
class Main {
public static void main(String[] args) {
int a = 100,b=200;
// checks if number is less than 0
if (a>b) {
System.out.println(a +" is greater than "+ b);
}
else{
System.out.println(b +" is greater than "+ a);
}
}
}
Output
200 is greater than 100
In the above program, we have checked whether the value of a
is greater that the value of b
.
The variable a
has the value 100
and b has the value as 200
.Here, the condition is not true, so the else
block is executed.
else if Statement
If you want to check multiple condition, then we have to use else-if
statement.
Syntax for else-if statement
if (condition1) {
// block of codes
} else if (condition2) {
// block of codes
} else {
// block of codes
}
Here the if
block condition is checked first. If the condition is not satisfied, then it checks the condition in else-if
statement. If that condition is satisfied, then else-if
block is executed.
If both the condition is not satisfied, then the else
block is executed.
Example program for else-if statement
class Main {
public static void main(String[] args) {
int a = 100,b=200, c=300;
// checks if number is less than 0
if (a >b && a>c) {
System.out.println(a +" is the biggest number.");
}
else if(b>c){
System.out.println(b +" is the biggest number.");
}
else{
System.out.println(c +" is the biggest number.");
}
}
}
Output
300 is the biggest number.
Here, in the if
statement we check whether the value of a
is greater than b
and c
. If it is satisfied, then the if
block is executed.
If the above condition is not satisfied, then with else-if
statement we wil check whether b
is greater than c
. If it is satisfied, then else-if
block is executed.
If both the conditions are not satisfied, then else
block is executed.
Nested if???else Statement
In java, it is possible to nest if-else
statement inside an other if-else
statement.
Syntax for nested if statements
//check if the first condition holds
if (condition 1) {
//if the second condition holds
if (condition 2) {
do something
}
//if the second condition does not hold
else {
do something else
}
}
// if the first condition does not hold
else{
//if the third condition holds
if (condition 3) {
do something
}
//if the third condition does not hold
else {
do something else
}
}
In the above syntax, we have the if-else
statement inside the if
block as well as else
block.
Let us see a program to get a number from the user and check whether the number is even and divisible by 4
or the number is odd and divisible by 3
.
Example for nested if statement
import java.util.*;
class Main {
public static void main(String[] args) {
// variable to store the given number
int n;
Scanner scanner = new Scanner(System.in);
//take input from the user
n= scanner.nextInt();
//if else condition to check whether the number is even or odd
if (n % 2 == 0) {
//the number is even
System.out.print("Even Number");
//nested if else condition to check if n is divisible by 4 or not
if (n % 4 == 0) {
//the number is divisible by 4
System.out.print("and divisible by 4");
} else {
//the number is not divisible by 4
System.out.print("and not divisible by 4");
}
} else {
//the number is odd
System.out.print("Odd Number");
//nested if else condition to check if n is divisible by 3 or not
if (n % 3 == 0) {
//the number is divisible by 3
System.out.print("and divisible by 3");
} else {
//the number is not divisible by 3
System.out.print("and not divisible by 3");
}
}
}
}
Output
10
Even Numberand not divisible by 4