Page 84 - iPro_trackGPT_V5_Class8
P. 84
When you enter the value 8, none of the cases will be
matched. Hence, the default case will be executed and
the output “Invalid day number” will be displayed on the
terminal.
LOOPING STATEMENTS
Looping statements are control flow statements that let us repeatedly run a set of instructions
for a specified number of times. These are also known as iteration statements or loops. Java
offers three types of looping statements, which are as follows:
while
do-while
for
Let us learn about these in detail.
The while Loop
The while loop is a control statement in Java that repeatedly executes a block of code as long
as a specified condition remains true. The loop first evaluates the condition before executing
the code inside the loop. If the condition is true, the loop’s body executes, and the condition is
checked again after each iteration. This process continues until the condition evaluates to false,
at which point the loop terminates and control passes to the statement following the loop.
The syntax of the while loop is as follows:
while(< conditional expression >)
Start
{
[statements]
[increment/decrement expression]
}
Conditional False
For example,
Expression
public class WhileLoop
{
True
public static void main(String args[])
{
Body of while loop
int i = 1;
while (i <= 10)
{
System.out.println("The value of i is: " + i);
i++;
} Stop
}
}
82 TrackGPT iPRO (V5.0)-VIII

