Page 78 - Trackpad_ipro 4.1_Class8
P. 78
The if…else Statement
The if…else statement is used to execute either of the blocks of statements from 'if' or 'else'
statements. When the condition next to the 'if' keyword evaluates to true, the statement(s)
inside the 'if' block will be executed. Otherwise, the statement(s) inside the 'else' block will
be executed.
The syntax of the 'if...else' statement is as follows:
if (< conditional expression >)
Start
{
[statements]
}
else Test False Body of else
Expression
{
[statements]
} True
For example:
Body of if
public class If Else Statement
{
public static void main(int num) Stop
{
System.out.println("Entered number is: " + num);
if (num % 2 == 0)
{
System.out.println("The number is even");
}
else
{
System.out.println("The number is odd");
}
System.out.println("Statement outside the if...else statement");
}
}
As you did in the previous example, run the program
twice for values of 22 and 7, respectively.
When you enter 22, the condition (22 % 2 == 0) returns
true. So, the output appears as shown.
76 iPro (Ver. 4.1)-VIII

