Loop Control Statements in C++
Loop Control Statements
Now, we know about the different types of loops in C++ and how to use them.
However, many times a situation occurs when the coder has to break out of the loop before the condition becomes false or the coder may not want to execute the loop at certain values of the counter variable.
In such a case, the C++ provides the facility of the various loop control statements, through which we can easily achieve the above-mentioned cases.
These loop control statements are also known as the jumping statements in C++.
The following Loop Control Statements are provided by C++:
Let us discuss them in detail.
Break statement is used when there is a need to terminate the loop.
The break statement causes the loop to terminate and begins execution with the very next statement outside the loop.
cout<< “You have entered ONE\n”;
cout<< “You have entered TWO\n”;
cout<< “You have entered THREE\n”;
cout<< “You have entered more than THREE\n”;
cout<< “Outside Switch\n ”;
You have entered TWO
Here, the break statement is used to prevent falling through of cases.
Continue is also a jumping statement, it is used to prevent the current iteration of the loop and starts execution with the very next iteration of the loop.
cout<< “\nEven Number: ”<<i;
Even Number: 2
Even Number: 4
The above program only prints even numbers from 1 to 5.
Goto statement uses a label and jumps the control where the label is defined in the C++ program.
cout<< “Value is THREE”;