Page 191 - ComputerScience_Class_11
P. 191
else if(Condition2) True
{ Condition 1?
Statement 2;
} Statement 1
False
else if(Condition3)
{ True
Statement 3; Condition 2?
}
else Statement 2
False
{
Statement last;
Statement 3
}
For example,
1. if(a>=90) Flowchart of if-else-if statement
System.out.println("Grade A");
else if(a>=80)
System.out.println("Grade B");
else
System.out.println("Grade C");
System.out.println("Grade Obtained");
Here, if the value of variable a is greater than or equal to 90, Grade A will be printed . If a is greater than or equal to
80, then Grade B will be printed otherwise Grade C will be printed. The message “Grade Obtained” will be printed
in each case.
2. if(a>=0 && a<=20)
System.out.println("From 0 to 20");
else if(a>=21&& a<=40)
System.out.println("From 21 to 40");
else
System.out.println("More than 40");
Here, depending on the value stored in variable a, the corresponding message will be printed on the basis of the
given conditions.
nested if statement
When an if statement is placed inside another if statement, then it is known as a nested if statement.
Syntax:
if(Condition1)
{
if( Condition2) //nested
{
Statement1;
}
else
{
Statement2;
}
else
{
Statements and Scope 189

