Page 83 - Trackpad_ipro 4.1_Class8
P. 83
LOOP STATEMENTS
Loop statements are control flow statements that allow us to repeatedly execute a set of
statements for a given number of times. These are also called iteration statements or loops.
Java provides three kinds of loop statements:
while do-while for
The 'for' loop is more commonly used as compared to 'while' and 'do-while' loops.
Let us learn about these in detail.
The while Loop
The while loop is a flow control statement in Java. It is used
Start
to execute a block of statements repeatedly until a given
condition becomes false. In a 'while' loop, the condition is
evaluated first and if it returns true, then the statements
inside the loop are executed. Test False
Expression
The syntax of 'while' loop is as follows:
while(<conditional expression>)
{ True
[statements]
increment/decrement expression Body of while loop
}
For example:
public class WhileLoop
{
Stop
public static void main(String args[])
{
int i = 1;
while (i <= 10)
{
System.out.println
("The value of i is: " + i);
i++;
}
}
}
In the preceding code, the 'while' loop checks the condition
(i <= 10) that evaluates to true. The statements written inside
the body of the 'while' loop get executed.
The value of variable 'i' will be incremented by 1 (i++).
The loop checks the condition again and again until the value of variable 'i' becomes more than
10. This variable is also called a control variable. The control variable defines the duration until
which the loop will execute.
Conditional, Looping and Jumping Statements in Java 81

