Java Tutorial

Basic Concepts

Flow Control Statements

Object Oriented Concepts

Coming Tutorials

>Home>Java Tutorial>If, If Else, Nested If Else in Java

Java Tutorial

Basic Concepts

Flow Control Statements

Object Oriented Concepts

Coming Tutorials



If, If Else, Else If Ladder Syntaxes and Examples in Java

The if statement in Java allows you to control the flow of program execution on the basis of the outcome of an expression or condition. You can either have a single statement or a block of code within if statement body. The if statement is applicable if you want to execute single block of statement on a condition.

Note: that the conditional expression must be a Boolean expression.

Syntax:

if(b)

{

// statements

// if body

}

b must be boolean expression


Example 1:


int i = 10;
if(i) // i is not a boolean expression
{
// if body
}

Output:-


compile time exception, incompatible types.

Example 2:


int i = 10;
if(i=20) // i=20 is assignment and not a boolean expression
{
// if body
}

Output:-


compile time exception, incompatible types.

Example 3:


int i = 10;
if(i==10) // i==10 is boolean expression
{
System.out.println("Value of i is "+i);

}

Output:-


Value of i is 10

Example 4:


boolean b = false;
if(b=true) // b=true gives true value
{
System.out.println("Value of b is true");

}

Output:-


Value of b is true

Example 5:


boolean b = false;
if(b==false) // b==false evaluate to true
{
System.out.println("Value of b is false");

}

Output:-


Value of b is false

Note: The curly braces { & } are optional for if statement but a statement after if must not be a declarative statement.


Examples:


if(true); // valid

if(true) System.out.println("valid if"); // valid


if(true) int i=10; // invalid, declaration not allowed


if(true){ int i=10;} // valid, declaration statement inside body is allowed


If Else Statement in Java

The if else statement is an extension of if statement. If you have two blocks of statements and one of them must be executed then you need if else statement. If the expression in if evaluate to true then the body of if statement will get executed otherwise body of else will get executed.


Syntax:

if(b)

{

//Statements execution if b is true

}

else{

// Statements execution if b is false

}


Example: Find even and odd numbers


import java.util.Scanner;
class Test
{
// main() method
public static void main(String[] args)
{
Scanner sc = new Scanner(System. in);
System.out.println("Enter a Number");
int num = sc.nextInt();
if(num%2==0)
{
System.out.println("Number is Even");
}else
{
System.out.println("Number is Odd");
}
}
}


Else If Ladder in Java

The if statement and if else statements are used to check the single condition but if you have multiple conditions dependent on each other means only one condition can be true at a time then you should use else if ladder in Java.


Syntax:

if(b1)

{

//Statements execution if b1 is true

}

else if(b2){

// Statements execution if b2 is true

}

else if(b3){

// Statements execution if b3 is true

}

else{

// Statements execute if all of the above conditions b1, b2, b3 becomes false

}


Example: Find the grade by percentage


import java.util.Scanner;
class Test
{
// main() method
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a percentage");
float per = sc.nextFloat();
if(per>=70)
{
System.out.println("Distinction");
}else if(per>=60)
{
System.out.println("First Class");
}else if(per>=50)
{
System.out.println("Second Class");
}else if(per>=40)
{
System.out.println("Pass");
}
else
{
System.out.println("Failed");
}
}
}


Nested If Else in Java

The nested if else statement means using if else statement inside if body or else body.

Syntax:

if(b1)

{

//Statements execution if b1 is true

      if(nb){

      // Statements execution if nb is true

      }

      else{

      // Statements execute if all of the above conditions b1, b2, b3 becomes false

      }

}

else{

// Statements execute if all of the above conditions b1, b2, b3 becomes false

}

Share the article to help your friends