Page 190 - ComputerScience_Class_11
P. 190
if and only if Statement True
Sometimes, there is a situation when all the conditions need to be checked Condition 1?
and whatever conditions are satisfied, the statements under them are
executed. It may happen that all the conditions are satisfied. Statement 1
False
Syntax:
if(Condition1)
True
{ Condition 2?
Statement 1;
Statement 2
}
False
if(Condition2)
{
True
Statement 2;
Condition 3?
}
if(Condition3) Statement 3
{ False
Statement 3;
}
For example, Flowchart of if and only if statement
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 display output
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;
}
188 Touchpad Computer Science (Ver. 3.0)-XI

