Page 159 - CA_Blue( J )_Class10
P. 159
// job performed by the body of the loop
increment or decrement;
} while (condition for testing);
Start
do…while Loop
Body
true Conditional
Expression
false
End
Flowchart of … do-while loop
Example: Print sum of 1st five natural numbers.
Dry Run & Output
int i, s = 0;
i s=s+i System.out.println(s)
i = 1;
1 0+1=1
do
2 1+2=3
{
3 3+3=6
s = s + i;
i++; 4 6+4=10
} 5 10+5=15 15
while(i <= 5);
System.out.println(s);
Since the do-while loop executes at least once, we can call it an exit controlled loop. Example of a do-while loop
working as an exit controlled loop:
In the program given alongside, the value of i is 1 and the condition to be checked i <= 0. This means that the condition
will not be true. But, since the condition is checked at the end, that is why the line System.out.println("Exit Controlled
loop"); will execute only once.
int i=1;
do
{
System.out.println("Exit Controlled" +"Loop");
i++;
}while(i<=0);
8.6 DIFFERENT FORMS OF LOOPS
There are various forms of loops that are as follows:
• Infinite loop: A loop that never ends is called an infinite loop. This type of loop runs infinite number of times as the
test condition is always true.
Using infinite for loop:
157
Iterative constructs in Java 157

