If else statements in java are used to test some conditions. It evaluates a condition to be true
or false
.
Table of Contents
- If statement
- If else statement
- If else if ladder statement
Simple If statement
1 2 3 4 5 6 |
if(condition) { // If condition is true, this block will be executed } |
For example:
1 2 3 4 5 6 7 8 9 10 11 |
public class IfMain { public static void main(String[] args) { int price=20; if(price > 10) System.out.println("Price is greater than 10"); } } |
Output:
You can write single line followed by semi color after if or you can use curly braces to write block of code.
You can rewrite above code as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class IfMain { public static void main(String[] args) { int price=20; if(price > 10) { System.out.println("Price is greater than 10"); } } } |
If else statement
1 2 3 4 5 6 7 8 9 10 |
if(condition) { // If condition is true, this block will be executed } else { // If condition is false, this block will be executed } |
For example:
Check if number is odd or even.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.java2blog; public class IfElseMain { public static void main(String[] args) { IfElseMain ieMain=new IfElseMain(); ieMain.checkOddOrEven(20); } public void checkOddOrEven(int number) { if(number%2==0) { System.out.println(number+" is even"); } else { System.out.println(number+" is odd"); } } } |
Output:
If else if ladder statement
Before we learn about If else if ladder statement, let’s first see some conditional operator.
Conditional Operators
Java provides many conditional operators.Some of them are:
- == : To check if two variables are equal
- != : To check if two variables are not equal
- < : To check if first variable is less than other
- <= : To check if first variable is less than or equal to other
- > : To check if first variable is greater than other
- >= : To check if first variable is greater than or equal to other
- && : And operation is used to check if both conditions,used with && operator, are true
- || : or operation is used to check if one of the conditions,used with || operator, are true
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
if(condition1) { // If condition1 is true, this block will be executed. } else if(condition2) { // If condition2 is true, this block will be executed. } else if(condition3) { // If condition3 is true, this block will be executed. } else { // If all above conditions are false, this block will be executed. } |
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; public class IfElseIfLadderMain { public static void main(String[] args) { int age=28; if(age >= 18 && age <= 25) { System.out.println("Age is between 18 and 25"); } else if(age >= 26 && age <= 35) { System.out.println("Age is between 26 and 35"); } else if(age >= 36 && age <= 60){ System.out.println("Age is between 35 and 60"); } else{ System.out.println("Age is greater than 60"); } } } |
Output:
That’s all about if else statement in java.