Jump Statements In Cpp
The break statement:
Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end. For example, we are going to stop the count down before its natural end (maybe because of an engine check failure?):
Syntax
The syntax of a break statement in C++ is −
break;
CODE/PROGRAM/EXAMPLE
//break loop example
#include <iostream>
using namespace std;
int main ()
{
int n;
for (n=10; n>0; n--)
{
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}
}
return 0;
}
O/P : 10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!
The continue statement:
The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration.
For example, we are going to skip the number 5 in our countdown:
Syntax
The syntax of a continue statement in C++ is −
continue;
CODE/PROGRAM/EXAMPLE
//continue loop example
#include <iostream>
using namespace std;
int main ()
{
for (int n=10; n>0; n--) {
if (n==5) continue;
cout << n <<
The goto statement :
goto allows to make an absolute jump to another point in the program. You should use this feature with caution since its execution causes an unconditional jump ignoring any type of nesting limitations.
The destination point is identified by a label, which is then used as an argument for the goto statement. A label is made of a valid identifier followed by a colon (:).
Generally speaking, this instruction has no concrete use in structured or object oriented programming aside from those that low-level programming fans may find for it.
For example, here is our countdown loop using goto:
Syntax
//The syntax of a goto statement in C++ is −
goto label;
..
.
label: statement;
CODE/PROGRAM/EXAMPLE
//goto loop example
#include <iostream>
using namespace std;
int main ()
{
int n=10;
loop:
cout << n <<
The exit function:
exit is a function defined in the cstdlib library.
The purpose of exit is to terminate the current program with a specific exit code. Its prototype is:
Syntax
void exit (int exitcode);
The exitcode is used by some operating systems and may be used by calling programs. By convention, an exit code of 0 means that the program finished normally and any other value means that some error or unexpected results happened.