0% found this document useful (0 votes)
53 views4 pages

Nested If Examples in Java

Uploaded by

namyab2009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views4 pages

Nested If Examples in Java

Uploaded by

namyab2009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Nested If in Java- Flow Chart

The following picture will show the flow chart of the Java Nested If statement.
The execution flow of the Nested If statement is

• If Condition1 is FALSE, then STATEMENT3 will execute.


• If Test Condition1 is TRUE, it will check for the Test Condition2
• expression is TRUE, then STATEMENT1 will execute
• Else STATEMENT2 executed.
Examples of nested if in Java

EG.1
import java.util.Scanner;
class Menu
{
public static void main()
{
Scanner obj = new Scanner(System.in);
System.out.println("Menu");
System.out.println("1. North Indian");
System.out.println("2. South Indian");
int ch1;
char ch2;
System.out.println("Enter your choice- 1 or 2");
ch1 = obj.nextInt();
if(ch1==1)
{
System.out.println("Menu for North Indian");
System.out.println("a. Shahi Paneer");
System.out.println("b. Rajma Rice");
System.out.println("Enter your choice");
ch2 = obj.next().charAt(0);
if(ch2=='a')

System.out.println("You chose Shahi Paneer");

else if(ch2=='b')
System.out.println("You chose Rajma Rice");
}
else if(ch1==2)
{
System.out.println("Menu for South Indian");
System.out.println("a. Idli");
System.out.println("b. Vada");
ch2 = obj.next().charAt(0);
if(ch2=='a')
System.out.println("You chose Idli ");
else if(ch2=='b')
System.out.println("You chose Vada");
}
else
System.out.println("Incorrect option");

}//main
}//class
CODING TIME

Write a java program using nested if else statement through the algorithm given:

1. Start
2. Create an instance of the Scanner class.
3. Declare two variables to store the age and weight of a person.
4. Ask the user to initialize the age and weight.
5. Use the first if statement to check if the person is above 18 years of age.
6. If the person is above 18 years of age then use another if statement to check if the weight
of the person is above 50 or not.
7. If the person's age is above 18 and weight is also above 50 then, print the message that
the person is eligible to donate blood.
8. If the person is above 18 years of age but his/her weight is below 50 then print the
message that the person is not eligible to donate blood.
9. If the person is below 18 years of age, then print the message that the age must be greater
than 18.
10. Display the result.
11. Stop

You might also like