Java If Else Statement
Decision making Statements
The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in java.
- if statement
- if-else statement
- if-else-if ladder
- nested if statement
Java if Statement
- Based on some condition if we want to execute some set of statements then we need to use if condition in java.
- We need to keep all the required statements inside if block .
- If the condition is true then control executes the whole block otherwise it skips the if block of statements.
The structure of the “if” statement in Java looks like this:
Syntax :
if (condition) {
// execute this code
}
Java example program on if control statement:
public class IfExample{
public static void main(String[] args) {
int age = 3;
System.out.println("Shivani is " + age + " years old");
if (age < 4) {
System.out.println("Shivani is a baby");
}
}
}
Output :
Shivani is 3 years old
Shivani is a baby
Output Detail : In the example above we check if the age is lower than 4. As the age is set to be 3, the boolean condition age < 4 is true and as result we print “Shivani is a baby”. If we change the age to any value greater than 3 the code in the block want execute anymore and “Shivani is a baby” is not printed.
Java IF-else Statement
- If the condition is true then control executes the whole block otherwise it skips the if block of statements
- if condition is false if we need to execute another set of statement we can write another if and keep all the statements in that block.
- instead of checking multiple times we can simply use else block .
The structure of the “if-else” statement in Java looks like this:
Syntax :
if(condition){
//code if condition is true
}else{
//code if condition is false
}
Java example program on if-else control statement:
public class IfElseExample{
public static void main(String[] args) {
int age = 15;
System.out.println("Shivani is " + age + " years old");
if (age < 4) {
System.out.println("Shivani is a baby");
}else{
System.out.println("Shivani is not a baby may be adult");
}
}
}
Output
Shivani is 15 years old
Shivani is not a baby may be adult
Output Detail : In the example above we check if the age is lower than 4. As the age is set to be 15, the boolean condition age < 4 is true and as result we print “Shivani is a baby”. If we change the age to any value greater than 4 the code in the block want execute anymore and “Shivani is not a baby may be adult” is printed.
Java if-else-if ladder Statement
- instead of checking multiple times we can simply use else block .
- In some cases we may get requirement with multiple conditions in such cases we need to use if else ladder.(if -else-if-else if -else...)
The structure of the “if-else-if ladder” statement in Java looks like this:
Syntax :
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
else if(condition4){
//code to be executed if condition4 is true
}
...
else{
//code to be executed if all the conditions are false
}
Java example program on if-else-if ladder control statement:
public class IfElseIfLedder{
public static void main(String[] args) {
int age = 20;
System.out.println("Shivani is " + age + " years old");
if (age < 4) {
System.out.println("Shivani is a baby");
} else if (age >= 4 && age < 14) {
System.out.println("Shivani is a child");
} else if (age >= 14 && age < 18) {
System.out.println("Shivani is a teenager");
} else if (age >= 18 && age < 68) {
System.out.println("Shivani is adult");
} else {
System.out.println("Shivani is an old women");
}
}
}
Output :
Shivani is 20 years old
Shivani is adult
