Page 79 - iPro_trackGPT_V5_Class8
P. 79
The syntax of the if statement is as follows:
Start
if (< conditional expression >)
{
[statements]
Conditional False
}
Expression
For example,
public class IfStatement True
{ Body of if
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");
}
System.out.println("Statement outside the if statement");
}
}
Run the program twice for values of 22 and 7,
respectively. When you enter 22, the condition (22 %
2 == 0) returns true. So, the statement inside the body
of the if statement gets executed and the output will
appear as shown:
When you enter the value 7, then the output will
appear as shown:
Since the condition (7 % 2 == 0) returns false, the
statement inside the body of the if statement does
not get executed and only the statement outside the
if statement gets executed.
The if…else Statement
The if…else statement helps decide which block
Just as the heart repeatedly pumps
of code to run. If the condition is true, the code
blood, iteration in programming
inside the if block runs. If the condition is not true,
involves repeatedly running a set
the code inside the else block runs instead. of instructions.
Conditional, Looping and Jump Statements in Java 77

