Page 172 - Cs_withBlue_J_C11_Flipbook
P. 172
For example,
if(a==10)
System.out.println("Equal to 10");
if(a%2==0)
System.out.println("Is an even Number");
if(a%5==0)
System.out.println("Is a multiple of 5");
Here, all three conditions will be checked and if all conditions are found to be true, the program will print as follows:
The output of the preceding program is as follows:
Equal to 10
Is an even Number
Is a multiple of 5
if-else-if statement
The situations may arise when there are multiple conditions to be checked but only when the first condition is not
satisfied. If the first condition is satisfied, it will execute the statements under it ignoring the other conditions.
Note: The difference between if and only if statement and if-else-if statement is that the first statement
checks all the conditions and the later one checks the condition only when the previous condition is not
satisfied.
Syntax:
if(Condition1)
{
Statement 1; Yes
} Condition 1?
else if(Condition2)
Statement 1
{ No
Statement 2;
Yes
}
Condition 2?
else if(Condition3)
{ Statement 2
Statement 3; No
}
Statement 3
else
{
Statement last;
Flowchart of if-else-if statement
}
For example,
1. if(a>=90)
System.out.println("Grade A");
else
if(a>=80)
System.out.println("Grade B");
else
170170 Touchpad Computer Science-XI

