#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
cout << i << endl;
}
return 0;
}Jump Statements in C++: break, continue, goto, and return
Direct Answer & Core Concept
When writing a C++ program, instructions normally execute one after another, from top to bottom. However, sometimes you need to change this normal execution flow.
For example, you may want to stop a loop when a particular condition is met, skip an unwanted iteration, exit a function after completing a task, or move execution to a specific location in a function.
C++ provides special instructions called jump statements to handle these situations.
There are four main jump statements in C++:
break– Terminates a loop or switch statement.continue– Skips the current loop iteration.goto– Transfers execution to a labeled statement.return– Ends the execution of a function.
In this tutorial, you will learn how jump statements work in C++, their syntax, differences, and practical uses with easy-to-understand programming examples.
Detailed Explanation & Mechanics
What Are Jump Statements in C++?
Jump statements are instructions that change the normal flow of execution in a C++ program.
Normally, a program executes statements sequentially. Jump statements allow you to transfer control to another part of the program or terminate the current execution structure.
For example, consider a loop that prints numbers from 1 to 10. If you want to stop printing when the number reaches 5, you can use the break statement.
Example: Understanding Jump Statements
Output:
1
2
3
4When the value of i becomes 5, the break statement immediately terminates the loop.
This is a simple example of how jump statements change program execution.
Types of Jump Statements in C++
C++ provides four main jump statements.
| Jump statement | Purpose | Common use |
|---|---|---|
break | Terminates the nearest loop or switch | Stop a loop |
continue | Skips the current loop iteration | Skip specific values |
goto | Transfers control to a labeled statement | Special control-flow situations |
return | Ends a function | Return a result or exit a function |
Let's understand each statement with its syntax and practical examples.
1. break Statement in C++
The break statement is used to immediately terminate the nearest enclosing loop or switch statement.
Once the break statement executes, the program continues from the first statement after the terminated loop or switch.
It is commonly used when you want to stop a loop after finding a particular value or when a condition is satisfied.
Syntax of break Statement
break;The break statement does not require any condition. It is usually placed inside an if statement when you want to terminate execution based on a specific condition.
Example 1: Using break in a for Loop
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
cout << i << endl;
}
return 0;
}Output:
1
2
3
4Explanation
The loop starts with
i = 1.The condition checks whether
iis less than or equal to 10.Numbers are printed one by one.
When
ibecomes 5, theifcondition becomes true.The
breakstatement terminates the loop immediately.
The numbers 5 to 10 are not printed.
Example 2: Using break in a while Loop
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 10) {
if (i == 6) {
break;
}
cout << i << endl;
i++;
}
return 0;
}Output:
1
2
3
4
5The loop stops when the value of i reaches 6.
Example 3: Using break in a switch Statement
The break statement is also commonly used inside a switch statement.
#include <iostream>
using namespace std;
int main() {
int choice = 2;
switch (choice) {
case 1:
cout << "Home";
break;
case 2:
cout << "About Us";
break;
case 3:
cout << "Contact";
break;
default:
cout << "Invalid Choice";
}
return 0;
}Output:
About UsHere, break prevents execution from continuing into the next case.
Practical Uses of break
The break statement is useful when:
You find the required item in a search operation.
A user selects an exit option from a menu.
A specific condition requires a loop to stop.
You want to exit a
switchcase.An operation has reached its required result.
2. continue Statement in C++
The continue statement is used to skip the remaining statements in the current loop iteration.
Unlike break, it does not terminate the entire loop.
After executing continue, the loop proceeds to its next iteration according to the rules of that loop.
It is useful when you want to ignore certain values but continue processing the remaining values.
Syntax of continue Statement
continue;The continue statement is used inside looping statements such as for, while, and do-while.
Example 1: Using continue in a for Loop
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
cout << i << endl;
}
return 0;
}Output:
1
2
4
5Explanation
The loop starts with
i = 1.The program checks whether
iis equal to 3.When the condition is true,
continueskips the remaining statements in that iteration.The loop proceeds with the next value.
Therefore, the number 3 is not printed.
Example 2: Using continue to Print Even Numbers
You can also use continue to skip odd numbers and print only even numbers.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0) {
continue;
}
cout << i << endl;
}
return 0;
}Output:
2
4
6
8
10Explanation
The modulus operator % checks the remainder after division.
Even numbers produce a remainder of 0 when divided by 2.
Odd numbers produce a remainder of 1.
The
continuestatement skips the odd numbers.
Example 3: Using continue in a while Loop
#include <iostream>
using namespace std;
int main() {
int i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue;
}
cout << i << endl;
}
return 0;
}Output:
1
2
4
5Notice that the increment happens before continue. This is important in a while loop because the update must still happen to avoid an infinite loop.
Practical Uses of continue
The continue statement is useful when:
Skipping invalid records.
Ignoring unwanted values.
Printing only even or odd numbers.
Filtering data inside loops.
Processing only records that meet specific conditions.
3. goto Statement in C++
The goto statement is used to transfer program execution directly to a labeled statement within the same function.
Unlike break and continue, it does not need to be used only with loops or switch statements.
You can use goto to jump forward or backward to a label in the same function, subject to C++ scope and initialization rules.
Syntax of goto Statement
goto label_name;
// Other statements
label_name:
// Statement to executeHere:
gotois the jump keyword.label_nameis the name of the destination label.A colon (
:) is placed after the label.
Example 1: Using goto Statement
#include <iostream>
using namespace std;
int main() {
int number = 10;
if (number == 10) {
goto message;
}
cout << "This message will be skipped." << endl;
message:
cout << "Welcome to C++ Programming!";
return 0;
}Output:
Welcome to C++ Programming!Explanation
The program declares a variable named
number.The
ifcondition checks whether the value is 10.Since the condition is true,
goto messagetransfers control to themessagelabel.The statement between the
gotoand the label is skipped.The program executes the statement after the label.
Example 2: Using goto to Repeat an Operation
The goto statement can also transfer control backward to repeat a section of code.
#include <iostream>
using namespace std;
int main() {
int i = 1;
start:
if (i <= 5) {
cout << i << endl;
i++;
goto start;
}
return 0;
}Output:
1
2
3
4
5Explanation
The variable
istarts at 1.The program checks whether
iis less than or equal to 5.The current value is displayed.
The value of
iincreases by one.goto starttransfers execution back to thestartlabel.When
ibecomes 6, the condition becomes false and the program exits the block.
This example demonstrates repetition using goto, although a for or while loop is a better choice for this task.
Advantages and Disadvantages of goto
| Advantages | Disadvantages |
|---|---|
| Can transfer control to a specific label | Can make code difficult to understand |
| Useful in certain low-level control-flow situations | Can create complicated execution paths |
| Can exit deeply nested structures in special cases | Makes debugging and maintenance harder |
| Supported by the C++ language | Can make program logic less organized |
Important: Avoid using goto in ordinary programming whenever possible. Prefer loops, functions, and structured control statements because they make code easier to read and maintain.
4. return Statement in C++
The return statement is used to terminate the execution of a function.
It can also send a value back to the function that called it.
The return statement is commonly used in functions that calculate results, perform operations, or provide information to another part of a program.
Syntax of return Statement
For a function that returns a value:
return value;For a function that does not return a value:
return;The value returned must be compatible with the function's declared return type.
Example 1: Using return in a Function
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(10, 20);
cout << "Sum: " << result;
return 0;
}Output:
Sum: 30Explanation
The
add()function receives two integer arguments.It calculates their sum.
return a + b;sends the result back tomain().The returned value is stored in the
resultvariable.coutdisplays the final result.
Example 2: Using return to Exit a Function Early
The return statement can terminate a function before reaching its final statement.
#include <iostream>
using namespace std;
void checkNumber(int number) {
if (number < 0) {
cout << "Negative Number";
return;
}
cout << "Positive Number or Zero";
}
int main() {
checkNumber(-5);
return 0;
}Output:
Negative NumberExplanation
When the number is negative, the function displays a message and executes return.
The remaining statements in the function are not executed.
Example 3: Using return 0 in main()
The main() function is the starting point of a C++ program.
When main() returns 0, it normally indicates successful program completion to the operating system.
#include <iostream>
using namespace std;
int main() {
cout << "Program Started" << endl;
cout << "Program Completed";
return 0;
}Output:
Program Started
Program CompletedImportant: In C++, reaching the closing brace of main() is equivalent to returning 0. However, writing return 0; explicitly is common in beginner tutorials.
Practical Uses of return
The return statement is useful when:
Returning calculated values from functions.
Ending a function after completing a task.
Exiting a function when an invalid condition is detected.
Returning status information.
Ending the
main()function.
Difference Between break, continue, goto, and return in C++
All four jump statements change program execution, but they work differently.
Comparison Table
| Feature | break | continue | goto | return |
|---|---|---|---|---|
| Main purpose | Stop a loop or switch | Skip current loop iteration | Jump to a label | Exit a function |
| Used in loops | Yes | Yes | Can be used | Can exit a function containing a loop |
| Used in switch | Yes | No | Can jump to a label | Can exit the function |
| Requires a label | No | No | Yes | No |
| Returns a value | No | No | No | Sometimes |
| Common use | Stop execution | Skip values | Special control transfer | Finish a function |
Simple Example of All Four Jump Statements
#include <iostream>
using namespace std;
int calculate(int number) {
if (number < 0) {
return 0;
}
return number * 2;
}
int main() {
// break example
for (int i = 1; i <= 5; i++) {
if (i == 4) {
break;
}
cout << i << " ";
}
cout << endl;
// continue example
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
cout << i << " ";
}
cout << endl;
// goto example
goto message;
cout << "Skipped" << endl;
message:
cout << "Goto executed" << endl;
// return example
cout << "Result: " << calculate(10) << endl;
return 0;
}Output:
1 2 3
1 2 4 5
Goto executed
Result: 20Understanding the Output
The first loop stops when
ireaches 4 because ofbreak.The second loop skips 3 because of
continue.The
gotostatement transfers control to themessagelabel.The
calculate()function returns 20.Finally,
return 0ends themain()function successfully.
Difference Between break and continue in C++
Beginners often confuse break and continue because both are used inside loops.
However, they perform different operations.
| Feature | break | continue |
|---|---|---|
| Purpose | Terminates the nearest loop | Skips the current iteration |
| Loop execution | Stops completely | Continues with the next iteration |
| Remaining iterations | Not executed | Executed normally |
| Common use | Stop when a result is found | Skip unwanted values |
Example Using break
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
cout << i << " ";
}Output:
1 2Example Using continue
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
cout << i << " ";
}Output:
1 2 4 5Remember: break stops the loop, while continue skips only the current iteration.
How Jump Statements Work in Nested Loops
C++ also allows jump statements to be used inside nested loops.
A nested loop is a loop placed inside another loop.
Example: break in Nested Loops
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
break;
}
cout << i << " " << j << endl;
}
}
return 0;
}Output:
1 1
2 1
3 1The break statement terminates only the nearest enclosing loop, which is the inner loop. The outer loop continues normally.
Example: continue in Nested Loops
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue;
}
cout << i << " " << j << endl;
}
}
return 0;
}Output:
1 1
1 3
2 1
2 3Here, the inner loop skips the value 2 during each iteration of the outer loop.
Common Mistakes When Using Jump Statements in C++
Beginners often make mistakes when working with jump statements. Understanding these problems can help you write better C++ programs.
1. Confusing break and continue
Incorrect understanding:
breakskips only one iteration.continuestops the entire loop.
This is incorrect.
Correct understanding:
breakterminates the nearest enclosing loop or switch.continueskips the current loop iteration.
2. Using continue Outside a Loop
The continue statement can only be used within an iteration statement.
Incorrect:
int a = 10;
continue;Correct:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
cout << i << endl;
}3. Forgetting to Update a Variable After continue in a while Loop
Incorrect:
int i = 1;
while (i <= 5) {
if (i == 3) {
continue;
}
cout << i;
i++;
}When i becomes 3, the loop repeatedly executes continue without increasing i. This creates an infinite loop.
Correct:
int i = 1;
while (i <= 5) {
if (i == 3) {
i++;
continue;
}
cout << i << endl;
i++;
}4. Using goto Without a Clear Reason
Excessive use of goto can make a program difficult to read and maintain.
Prefer for, while, if, and function-based solutions.
5. Returning the Wrong Data Type
A function declared to return an integer should return a compatible integer value.
Example:
int add(int a, int b) {
return a + b;
}Make sure the returned value matches the function's declared return type.
6. Forgetting That break Only Exits the Nearest Loop
When using nested loops, break terminates only the innermost loop that contains it.
If you want to stop multiple nested loops, you need additional control logic, a function return, or another appropriate structured solution.
Best Practices for Using Jump Statements in C++
Follow these simple practices when writing C++ programs:
Use
breakwhen you need to terminate a loop or switch.Use
continuewhen you want to skip a particular iteration.Use
returnto finish a function or send a result back.Avoid unnecessary use of
goto.Always make sure loop variables are updated correctly.
Use meaningful conditions with jump statements.
Be careful when using jump statements inside nested loops.
Keep functions small and easy to understand.
Return values that match the declared function type.
Test different conditions to verify program behaviour.
Frequently Asked Questions About Jump Statements in C++
1. What are jump statements in C++?
Jump statements are instructions that change the normal flow of execution in a C++ program. The four main jump statements are break, continue, goto, and return.
2. How many jump statements are there in C++?
C++ has four main jump statements: break, continue, goto, and return.
3. What is the use of break in C++?
The break statement immediately terminates the nearest enclosing loop or switch statement.
4. What is the use of continue in C++?
The continue statement skips the remaining statements in the current loop iteration and moves to the next iteration.
5. What is the use of goto in C++?
The goto statement transfers program execution to a labeled statement within the same function.
6. What is the use of return in C++?
The return statement terminates a function and can return a value to the calling function.
7. What is the difference between break and continue?
break terminates the entire nearest enclosing loop, whereas continue skips only the current iteration.
8. Can break be used outside a loop in C++?
Yes. The break statement can also be used inside a switch statement. It cannot be used freely in an ordinary block or function.
9. Can continue be used in a switch statement?
A continue statement can appear inside a switch nested within a loop. It applies to the nearest enclosing loop, not the switch statement itself.
10. Is goto recommended in C++?
Generally, no. Although C++ supports goto, structured alternatives such as loops, functions, and conditional statements usually produce more readable code.
11. Can return be used inside a loop?
Yes. A return statement can be used inside a loop to terminate the entire function, not just the loop.
12. What happens when return 0 is used in main()?
return 0; normally indicates that the program completed successfully and sends a success status to the operating system.
13. What is the difference between break and return?
break exits the nearest enclosing loop or switch, while return exits the entire function.
14. Can goto jump outside a function?
No. A goto statement cannot transfer control to a label in another function. Its destination must be within the same function.
Runnable Code Example: Jump Statements in C++
C++20 / C++23#include <iostream>
struct ResourceTracker {
std::string name;
ResourceTracker(std::string n) : name(n) { std::cout << " Constructed: " << name << "
"; }
~ResourceTracker() { std::cout << " Destructed: " << name << "
"; }
};
int searchNumber(int target) {
ResourceTracker tracker("SearchScope");
for (int i = 1; i <= 5; ++i) {
if (i == target) {
std::cout << "Target found! Returning from function.
";
return i; // Automatically destructs 'tracker' before returning
}
}
return -1;
}
int main() {
std::cout << "Starting search...
";
int result = searchNumber(3);
std::cout << "Search result: " << result << "
";
return 0;
}Common C++ Technical Interview Questions & Answers
Interview PrepQ: Does return safely invoke destructors of stack variables?
Q: When is goto acceptable in modern C++?
Key Takeaways, Modern Best Practices & Idioms
C++ Best Practices & Production Rules
goto.2. Ensure every non-void function path explicitly returns a valid value.
3. Leverage early returns (guard clauses) to simplify deep conditional nesting.