C++ 15: Jump Statements
Jump Statements are used to jump the program control to specific line of code in program. C++ provides us three different jump statement as follows.
- break
- continue
- goto
1. break:
- The break statement is used to exit from any loop before the condition gets false.
- That means the break statement is used to terminate current loop.
- Also break statement is used to exit from the switch statement.
- As soon as the break statement is executed the program control moves to the frist statement after loop or switch statement.
Example:
#include <iostream>
using namespace std;main(){ int i = 1;
while(i<=5) { cout<<i<<endl; if (i==3) break; i++; }}2. Continue Statement
Whenever continue statement is executed, the loop starts its next iteration.
The continue statement transfers the program at different points at different loops as follows.
#include <iostream>
using namespace std;main(){ int i = 1;
while(i<8){ i++; if(i%2==0) continue; cout<<i<<endl; }}3. GOTO Statement
A goto statement transfers program control anywhere only inside the same function.
Also it allows the program to exit from the loop or switch statement.
The syntax for gets goto statement is given below.
Syntax:
goto label;
This identifier label can be defined using following syntax.
Syntax;
label:
NOTE:
By using goto we may move program control inside the loop, if-else etc.
#include <iostream>
using namespace std;main(){ out << "JACK" << endl; goto ABC;
PQR: cout << "James" << endl; goto XYZ;
ABC: cout << "MICHELLE" << endl; goto PQR;
XYZ: cout << "Welcome" << endl;
goto MNO; while (i <= 5) { MNO: cout << i << endl; if (i == 3) break; i++; } cout << "Welcome" << endl;}
Comments
Post a Comment