Let’s learn if else in java.
If else in java
If else statement executes a block of code where; if the specified boolean condition is true, statements inside “if” executes. Else statements inside “else” execute. Here’s the syntax for if else statement.
Syntax:
if(condition) { // body } else { // body }
If else example:
In the below program 14 modulo 2 is equal to 0. Because remainder is 0. Hence it is even number.
class JavaIfElse { public static void main(String[] args) { int number = 14; // check if number is divisible by 2 if(number % 2 == 0) { System.out.println(number + " is even number"); } else { System.out.println(number + " is odd number"); } } }
Output:
14 is even number
Nested if statement
Nested if statement means if statement within an if statement. Here inner if block code will execute only when outer if block condition is true.
Syntax:
if(condition1) { if(condition2) { //code } }
Let’s see an example on nested if statement.
class NestedIfDemo { public static void main(String[] args) { int a = 20; int b = 40; if (a == 20) { if(b == 40) { System.out.println("Both a and b are equal."); } } } }
Output:
Largest number: 10
If-Else-if statement
In if-else-if statement, if condition is true, corresponding statements will be executed and the remaining conditions will not execute. If none of the conditions are true then “else” block will be executed.
Syntax:
if(condition1) { // body } else if (condition2) { // body } else if (condition3) { // body } else { // body }
Let’s see an example on if else if statement in java.
import java.util.Scanner; public class ElseIfLadder { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter students percentage: "); float percentage = sc.nextFloat(); if(percentage >= 70) { System.out.println("student has got distinction."); } else if(percentage >= 60) { System.out.println("student has got first class."); } else if(percentage >= 50) { System.out.println("student has got second class."); } else if(percentage >= 40) { System.out.println("student has just pass."); } else { System.out.println("student is failed."); } sc.close(); } }
Output:
Please enter students percentage: 90
student has got distinction.
If statement java
If statement executes only when specified condition is true.
Syntax:
if(condition) { // body }
Example:
public class IfStatementExample { public static void main(String[] args) { int num = 50; if(num > 41) { System.out.println("50 is greater than 41!!"); } } }
Output:
50 is greater than 41!!
If else statement exercises
Now let’s see conditional statement exercises. Here we are going to execute if else statement exercise or java program which allow user to input age using Scanner class.
Then using conditional statement, i.e, if else statement will show if user is eligible for driving licence. So a user should be older than or equal to 18 to get driving licence.
import java.util.Scanner; public class Exercise { public static void main(String[] args) { int age; Scanner sc = new Scanner(System.in); System.out.println("Input your age: "); age = sc.nextInt(); if(age >= 18) { System.out.println("You are eligible for driving licence."); } else { System.out.println("You are not eligible for driving licence."); } sc.close(); } }
Output:
Input your age: 24
You are eligible for driving licence.
Input your age: 15
You are not eligible for driving licence.
Also read – abstraction in java