codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

17 / 87 topics
14if, if...else, and Nested if...else15switch..case Statement16Loops17Jump Statements: break, continue, goto
Tutorials/C++ Programming/Jump Statements: break, continue, goto
⚡C++ Programming

Jump Statements: break, continue, goto

Updated 2026-05-12
18 min read

Jump Statements: break, continue, goto

In this tutorial, we will explore jump statements in C++, which are used to alter the normal sequence of program execution. These include break, continue, and goto. Understanding these statements is crucial for controlling loops and managing complex control flow scenarios efficiently.

Introduction

Jump statements allow you to "jump" to a different part of your code, bypassing the usual sequential execution. This can be particularly useful in loop constructs where certain conditions require immediate termination or skipping of iterations. However, excessive use of jump statements can lead to less readable and maintainable code, so it's important to use them judiciously.

Core Content

The break Statement

The break statement is used to exit a loop or switch statement immediately when a certain condition is met. It terminates the execution of the enclosing loop or switch block and transfers control to the next statement following the loop or switch.

Example 1: Using break in a for Loop

C++
1#include <iostream>
2using namespace std;
3
4int main() {
5 for (int i = 0; i < 10; ++i) {
6 if (i == 5) {
7 break;
8 }
9 cout << "i: " << i << endl;
10 }
11 return 0;
12}
Output
i: 0
i: 1
i: 2
i: 3
i: 4

In this example, the loop is terminated when i equals 5. The numbers from 0 to 4 are printed.

Example 2: Using break in a switch Statement

C++
1#include <iostream>
2using namespace std;
3
4int main() {
5 int number = 3;
6 switch (number) {
7 case 1:
8 cout << "Number is 1" << endl;
9 break;
10 case 2:
11 cout << "Number is 2" << endl;
12 break;
13 case 3:
14 cout << "Number is 3" << endl;
15 break;
16 default:
17 cout << "Number is not 1, 2, or 3" << endl;
18 }
19 return 0;
20}
Output

In this example, the continue statement skips printing even numbers.

Example 2: Using continue in a while Loop

C++
1#include <iostream>
2using namespace std;
3
4int main() {
5 int i = 0;
6 while (i < 10) {
7 ++i;
8 if (i % 3 == 0) {
9 continue;
10 }
11 cout << "i: " << i << endl;
12 }
13 return 0;
14}
Output
i: 1
i: 2
i: 4
i: 5
i: 7
i: 8
i: 10

Here, the continue statement skips printing multiples of 3.

The goto Statement

The goto statement allows you to transfer control to a labeled statement within the same function. This can be useful for breaking out of deeply nested loops or skipping complex sections of code. However, it is generally discouraged due to its potential to make code difficult to read and maintain.

Example 1: Using goto in a Loop

C++
1#include <iostream>
2using namespace std;
3
4int main() {
5 int i = 0;
6loop_start:
7 if (i == 5) {
8 goto end_loop;
9 }
10 cout << "i: " << i << endl;
11 ++i;
12 goto loop_start;
13end_loop:
14 cout << "Loop ended" << endl;
15 return 0;
16}
Output
i: 0
i: 1
i: 2
i: 3
i: 4
Loop ended

In this example, the goto statement is used to exit the loop when i equals 5.

Example 2: Using goto with Labels

C++
1#include <iostream>
2using namespace std;
3
4int main() {
5 int x = 10;
6 if (x > 5) {
7 goto label;
8 }
9 cout << "x is not greater than 5" << endl;
10label:
11 cout << "x is greater than or equal to 5" << endl;
12 return 0;
13}
Output

In this example, the program continues to read numbers until a negative number is entered, at which point it exits the loop using break.

Summary

Jump StatementDescription
breakExits a loop or switch block immediately.
continueSkips the current iteration of a loop and proceeds with the next one.
gotoTransfers control to a labeled statement within the same function.
  • Use break to exit loops or switch blocks when a specific condition is met.
  • Use continue to skip iterations in loops based on certain conditions.
  • Avoid using goto as it can lead to less readable and maintainable code.

What's Next?

In the next tutorial, we will explore functions in C++, including how to define user-defined functions and understand different types of function parameters. Functions are a fundamental building block for creating modular and reusable code. Stay tuned!


PreviousLoopsNext Function Basics & User-defined Types

Recommended Gear

LoopsFunction Basics & User-defined Types