If else in java

Let’s learn if else in java.

If else in java

If else statement executes a block of code where; statements inside “if” executes if condition is true else statements inside “else” execute. Here’s the syntax for if else statement.

Syntax:

if(condition)
{
// if condition is true;
}
else
{
// if condition is false;
}

In java, decision making is made through control flow statements based on a condition. Condition is based on boolean condition: true or false.

If else statement example:

In the below program 15 modulo 2 is not equal to 0, hence it is odd number. So the control shifts to else condition and prints 15 is odd number. If 15 modulo 2 was equal to 0 then output would be even number.

public class JavaIfElse
{
public static void main(String[] args)
{
int number = 15;
// 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:

15 is odd number

Nested if else

Nested if else means one under the other. That is you can write one if else statement inside another if else statement.

public class NestedIfElse
{
public static void main(String[] args)
{
Integer num1 = 2, num2 = 8, num3 = 10, largest;
if(num1 >= num2)
{
if(num1 >= num3)
{
largest = num1;
}
else
{
largest = num3;
}
}
else
{
if(num2 >= num3)
{
largest = num2;
}
else
{
largest = num3;
}
}
System.out.println("Largest number: " + largest);
}
}

Output:

Largest number: 10

Else if statement

Else if statement is used to specify new condition if first condition is false.

Syntax:

if(condition1)
{
// execute if condition1 is true
}
else if (condition2)
{
// execute if condition2 is true
}
else if (condition3)
{
// execute if condition3 is true
}
else
{
// execute if conditions 1, 2 and 3 becomes false
}

Let’s see an example on else if statement in java.

import java.util.Scanner;
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
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:

student has got distinction.

If statement java

If statement executes a set of statements if condition is true.

Syntax:

if(condition)
{
// if condition is true;
}

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);
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:

You are eligible for driving licence.

You are not eligible for driving licence.

Now let’s see another if else statement exercise or java program where we are going to check whether given number is even number or odd number. Let’s see if else statement java exercises.

import java.util.Scanner;
public class Exercise
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
if(number%2 == 0)
{
System.out.println("even number.");
}
else
{
System.out.println("odd number.");
}
sc.close();
}
}

Output: