Friday 17 May 2013

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:

// goto loop example
#include <iostream>
using namespace std;
int main ()
{
int n=10;
loop:
cout << n << ", ";
n--;
if (n>0) goto loop;
cout << "FIRE!\n";
return 0;
}
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!


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.


No comments:

Post a Comment