Page 236 - IT-802_class_12
P. 236
System.out.println (“pASSED”);
}
If we also want to display “FAILED” when the student has scored less than 40%, we use the if else statement. The syntax
of the Java if else statement is as shown below:
if(expression)
{
Statements
}
else
{
statements
}
As before, the block of code following if is executed if the conditional expression evaluates to true. The block of code
following the else is executed if the conditional expression evaluates to false. In our Percentage Calculator program,
we can write,
if (percentage >= 40)
{
System.out.println(“pASSED”);
}
else
{
System.out.println(“FAiLED”);
}
Sometimes, you may want to execute a block of code based on the evaluation of two or more conditional expressions.
For this, you can combine expressions using the logical && or || operators. Let’s say, we want to print the division with
which the student has passed, then, we can write the following code. The first if statement uses the logical AND (&&)
operator to combine two relational expressions.
if ((percentage >= 40)&& (percentage < 60))
{
System.out.println(“pASSED with ii Division”);
}
if (percentage >= 60) {
System.out.println(“pASSED with i Division “);
}
if (percentage < 40)
{
System.out.println(“FAiLED”);
}
234 Touchpad Information Technology-XII

