Jump statements are used to alter the flow of control unconditionally. That is, jump statements transfer the program control within a function unconditionally. The jump statements defined in C++ are break, continue, goto and return. In addition to these jump statements, a standard library function exit () is used to jump out of an entire program.
The break Statement: The break statement is extensively used in loops and switch statements. A break statement immediately terminates the loop or the switch statement, bypassing the remaining statements. The control then passes to the statement that immediately follows the loop or the switch statement. A break statement can be used in any of the three C++ loops.
Note that a break statement used in a nested loop affects only the inner loop in which it is used and not any of the outer loops. Similarly, a break statement used in a switch statement breaks out of that switch statement and not out of any loop that contains the switch statement.
The continue Statement: The continue statement is used to ‘continue’ the loop with its next iteration. In other words, continue statement skips any remaining statements in the current iteration and immediately passes the control to the next iteration. The continue statement does not terminate the loop (as in the case of break statements), rather it only terminates the current iteration of the loop. Like a break statement, a continue statement can be used in any of the three loops.
To understand the concept of the break and continue statement, consider this example.
Example : A program to add the factors of a number
The output of the program is
#include<iostream> using namespace std; int main () { int x=0, y, sum=0; cout<<"Enter a number: "; cin>>y; while(1) { x++; if (x>y) break; if(y%x!=0) continue; sum=sum+x; } cout<<"\n Sum of factors: "<<sum; return 0; }
Enter a number: 8
Sum of factors: 15
The goto Statement: The goto statement can be used anywhere within a function or a loop. As the name suggests, goto statements transfer the control from one part to another part in a program which is specified by a label. Labels are user-defined identifies followed by a colon that are prefixed to a statement to specify the destination of a goto Statement.
To understand the concept of the goto statement, consider this example.
Example: A program to demonstrate the use of goto statement
#include<iostream> using namespace std; int main () { int x = 10; loop: cout<<x<<","; //loop is a label x--; if (x<0) goto loop; cout<<"\n Here is the example of goto !"; return 0; }
The output of the program is
10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
Here is the example of goto
The exit () Function: The exit( ) function is a standard library function that terminates the entire program immediately and passes the control to the operating system. This function takes a single parameter, that is, exit status of the program and returns the same status to the operating system upon termination. The status can be either a zero or non-zero value, where zero shows successful termination and non-zero shows unsuccessful termination of the program.
To understand the concept of the exit () function, consider this example.
Example: A program to demonstrate the use of exit( )
#include<iostream> #include<cstdlib> //for exit() function using namespace std; int main () { int a; cout<<"Enter the value for a: "; while(cin>>a) { if(a<0) { cout<<"This program is going" <<"to terminate!"; exit(0) ; } cout<<"Enter another value for a: "; } return 0; }
The output of the program is
Enter the value for a: 7
Enter another value for a: 8
Enter another value for a: -4
This program is going to terminate!