Nested If Example Java Program


Definition

A nested if statement is an if-else statement with another if statement as the if body or the else body. Here's an example: if ( num > 0 ) // Outer if if ( num < 10 ) // Inner if System.out.println( "num is between 0 and 10" ) ; There is an outer if statement, and an inner if.

Syntax

if (condition){
	if (condition){
		//Do something
	}
	//Do something
}

Nested If Example Program

import java.util.Scanner;
public class NestedIfDemo {
	public static void main(String args[]){
		Scanner in=new Scanner(System.in);
		System.out.print("value of x: ");
		int x =in.nextInt();
		System.out.print("value of y: ");
		int y =in.nextInt();
		if( x == 100 ){
			if( y == 10 ){
				System.out.print("X = 100 and Y = 10");
			}
		}
		else{
			System.out.println("The values of x and y are not 100 and 10 simultaneously");
		}
	}
}

Sample Output

Output is:
value of x: 56
value of y: 34
The values of x and y are not 100 and 10 simultaneously