➢ Jumps in Loops:-
➢ C++ break and continue Statement:-
There are two statements (break; and continue ;) built in C++ programming to alter the normal flow of program.
Loops are used to perform repetitive task until test expression is false but sometimes it is desirable to skip some statement/s inside loop or terminate the loop immediately with checking test condition. On these type of scenarios, continue; statement and break; statement is used respectively. The break; statement is also used to terminate switch statement.
1) break Statement :-
The break; statement terminates the loop(for, while and do..while loop) and switch statement immediately when it appears .
Syntax of break :-
break;
In real practice, break statement is almost always used inside the body of conditional statement(if...else) inside the loop.
2) continue Statement :-
It is sometimes necessary to skip some statement/s inside the loop. In that case, continue; statement is used in C++ programming.
Syntax of continue :-
continue;
In practice, continue; statement is almost always used inside conditional statement.
Example Program:-
// C++ Program to demonstrate working of continue statement
// C++ program to display integer from 1 to 10 except 6 and 9.
#include <iostream.h>
#include <conio.h>
int main()
{
for (int i = 1; i <= 10; ++i)
{
if ( i == 6 || i == 9)
{
continue;
}
cout<<i<<"\t";
}
getch();
}
Output :-
1 2 3 4 5 7 8 10
Comments
Post a Comment