Page 157 - CA_Blue( J )_Class10
P. 157
• Exit Controlled Loop: If the testing condition is checked after executing the body of the loop, then it is called exit
controlled loop. Example of exit controlled loop is do-while.
8.3 THE FOR LOOP
The for loop executes a set of statements repeatedly for a fixed number of times. To use this loop, the user knows how
many times the loop will execute. The syntax of the for loop is:
for (initialization; condition for testing; increment or decrement)
{
// job performed by the body of the loop
}
Start
Initialization
false Conditional
expression
true Increment/
decrement
Body of for loop
End
Flowchart of the for loop
Example: Print all the even numbers upto 10.
for (int i=2; i<=10; i=i+2)
{ Dry Run & Output
System.out.println(i); i System.out.println(i)
} 2 2
As the for loop checks the condition before executing the body of 4 4
loop, so it is entry controlled loop.
6 6
Java also allows us to use multiple loop control variables, test 8 8
conditions and increment or decrement expressions in the for loop. 10 10
The syntax is:
for (initialization1, initialization2 …; condition1…, condition2… ; increment… /
decrement…)
{
statements;
}
8.4 THE WHILE LOOP
Sometimes, we may need to execute a set of statements repeatedly without knowing the number of times the loop will
execute. The while loop solves this problem. It is used to execute a set of statements repeatedly unless the specified
condition becomes false. The syntax of the while loop is:
initialization;
while(condition for testing)
{
155
Iterative constructs in Java 155

