Jump statements.
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?).
#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;
}
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:
w are going to skip the number 5 in our countdown:
#include <iostream>
using namespace std;
int main ()
{
for (int n=10; n>0; n--)
{
if (n==5) continue;
cout << n << ", ";
}
cout << "FIRE!\n";
return 0;
}
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:
#include <iostream>
using namespace std;
int main ()
{
int n=10;
loop:
cout << n << ", ";
n--;
if (n>0) goto loop;
cout << "FIRE!\n";
return 0;
}
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: 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.void exit (int exitcode);
No comments:
Post a Comment