In C++, jump statements are used to alter the flow of control in a program by transferring control to a specific location. The primary jump statements are break, continue, goto, and return.
List of Jump Statements in C++
Statement | Description |
---|---|
break | Exits the current loop or switch statement |
continue | Skips the current iteration and moves to the next iteration in a loop |
goto | Jumps to a labeled statement within a function |
return | Exits a function and returns a value (if specified) |
Let’s explore each jump statement with examples.
1. break Statement
The break statement is used to exit a loop or switch statement immediately, regardless of the current iteration or condition.
Example: Using break in a for Loop
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 10; i++) { if (i == 5) { break; // Exit the loop when i is 5 } cout << "i = " << i << endl; } return 0; }
Explanation:
- The loop runs from 1 to 10, but break exits the loop when i is 5.
Output:
i = 1 i = 2 i = 3 i = 4
Example: Using break in a switch Statement
#include <iostream> using namespace std; int main() { int choice = 2; switch (choice) { case 1: cout << "Option 1 selected." << endl; break; case 2: cout << "Option 2 selected." << endl; break; case 3: cout << "Option 3 selected." << endl; break; default: cout << "Invalid option." << endl; } return 0; }
Explanation:
- The break statement exits each case in the switch, preventing fall-through.
Output:
Option 2 selected.
2. continue Statement
The continue statement skips the current iteration of a loop and moves to the next iteration.
Example: Using continue in a for Loop
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // Skip the iteration when i is 3 } cout << "i = " << i << endl; } return 0; }
Explanation:
- The continue statement skips the iteration when i is 3, so 3 is not printed.
Output:
i = 1 i = 2 i = 4 i = 5
Example: Using continue in a while Loop
#include <iostream> using namespace std; int main() { int i = 1; while (i <= 5) { i++; if (i == 3) { continue; // Skip the iteration when i is 3 } cout << "i = " << i << endl; } return 0; }
Explanation:
- The loop skips printing i when it’s 3.
Output:
i = 2 i = 4 i = 5 i = 6
3. goto Statement
The goto statement transfers control to a labeled statement in the function. However, it’s generally advised to avoid goto as it can make code harder to understand and maintain.
Example: Using goto to Jump to a Label
#include <iostream> using namespace std; int main() { int number = 5; cout << "Checking number..." << endl; if (number < 10) { goto small; // Jumps to the label 'small' } cout << "Number is large." << endl; return 0; small: cout << "Number is small." << endl; return 0; }
Explanation:
- When number is less than 10, control jumps to the small label.
Output:
Checking number... Number is small.
Example: Using goto in a Loop
#include <iostream> using namespace std; int main() { int i = 1; start: if (i > 5) { return 0; // Exit program if i > 5 } cout << "i = " << i << endl; i++; goto start; // Go back to the 'start' label }
Explanation:
- The loop runs until i is greater than 5 by using goto to jump to the label start.
Output:
i = 1 i = 2 i = 3 i = 4 i = 5
4. return Statement
The return statement is used to exit a function. In functions with a return type other than void, it also returns a value.
Example: Using return in a Function
#include <iostream> using namespace std; int add(int a, int b) { return a + b; // Returns the sum of a and b } int main() { int result = add(5, 3); cout << "Sum: " << result << endl; return 0; }
Explanation:
- return exits the add function and returns the result of a + b.
Output:
Sum: 8
Example: Using return in main
You can use return to terminate the main function early.
#include <iostream> using namespace std; int main() { int num = -1; if (num < 0) { cout << "Negative number detected, exiting program." << endl; return 0; // Exit the program if num is negative } cout << "This line will not execute if num is negative." << endl; return 0; }
Explanation:
- return 0 exits the main function immediately if num is negative.
Output:
Negative number detected, exiting program.
Summary Table
Statement | Description | Example |
---|---|---|
break | Exits the current loop or switch statement | if (i == 5) { break; } |
continue | Skips the current loop iteration and moves to the next iteration | if (i == 5) { continue; } |
goto | Jumps to a labeled statement within a function | goto label; |
return | Exits a function, optionally returning a value | return 0; |
Complete Example: Using All Jump Statements Together
This example demonstrates the use of break, continue, goto, and return in various scenarios.
#include <iostream> using namespace std; int main() { // 1. Using break in a loop for (int i = 1; i <= 10; i++) { if (i == 5) { break; // Exits the loop when i is 5 } cout << "i = " << i << " (break example)" << endl; } cout << endl; // 2. Using continue in a loop for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // Skips the iteration when i is 3 } cout << "i = " << i << " (continue example)" << endl; } cout << endl; // 3. Using goto to jump to a label int number = 7; if (number < 10) { goto small; // Jumps to the 'small' label } cout << "Number is large." << endl; return 0; small: cout << "Number is small (goto example)." << endl; cout << endl; // 4. Using return to exit the program early cout << "Returning from main (return example)." << endl; return 0; }
Explanation:
- The break statement exits the loop when i is 5.
- The continue statement skips the iteration when i is 3.
- The goto statement jumps to the label small if number is less than 10.
- The return statement exits the program.
Sample Output:
i = 1 (break example) i = 2 (break example) i = 3 (break example) i = 4 (break example) i = 1 (continue example) i = 2 (continue example) i = 4 (continue example) i = 5 (continue example) Number is small (goto example ). Returning from main (return example).
Summary
The jump statements in C++ (break, continue, goto, and return) provide control over program flow, allowing you to skip iterations, exit loops, jump to specific locations, and return values.