Nested if statement in Java;
In this section let's discuss about Nested if statements in Java.
Syntax:
- This is used to test the condition.
- One if block inside another if block is called nested if.
Example 1:
class Easy
{
public static void main(String[] args)
{
int no=6;
if(no>2) //true condition
{
if(no<3) //false condition
{
System.out.println("Hello1");
}
System.out.println("Hello2");
}
}
}
/ ### Output ###
Hello2
Example 2: Find greater value in three number.
import java.util.Scanner;
class Easy
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int a,b,c;
System.out.println("Enter three number");
a=in.nextInt(); b=in.nextInt();
c=in.nextInt();
if(a>b) if(a>c)
System.out.println(a+" is greater");
if(b>a) if(b>c)
System.out.println(b+" is greater");
if(c>a) if(c>b)
System.out.println(c+" is greater");
}
}
/ ### Output ###
Enter three number 45 68 25 68 is greater
For Videos Join Our Youtube Channel: Join Now
Thank you!