Page 61 - Modular_V1.1_Flipbook
P. 61
The minimum number of iterations of a while loop is zero. But, in case of a do-while loop,
the minimum number of iterations is one.
while and for loops are interchangeable in almost every situation. But do-while and for
loops are not interchangeable in most of the situations.
JUMP STATEMENTS
Sometimes, there is a situation when the control of the program needs to be transferred out of
the loop body, even if all iterations of the loop have not been completed. For this purpose, jump
statements are used. C++ offers four jump statements — break, continue, return and goto —
which are used within the loop.
The break Statement
The break statement in C++ is used for bringing the program control out of the loop. The break
statement halts the execution of a loop and program flow switches to the statement after the
loop. A single break statement will break out of only one loop. The syntax of the break statement
is shown below:
while(condition)
{
if(conditional expression)
{
break;
}
}
Program 7: To display the numbers from 1 to 10. The loop will exit when the value of the
number will become 6.
#include<iostream.h>
#include<conio.h>
void main()
{
int num = 10;
for(int i = 1; i <= num; i++)
if(i==6)
break;
else
cout<<i << “ “; Output:
cout<<”\nOut of the loop”; 1 2 3 4 5
getch(); Out of the loop
}
In the preceding output, you can see that the loop is terminated the numbers after 5 because of
the break statement.
Loops 59

