About Jump Statements in C++

About Jump Statements in C++

6 mins read276 Views Comment
clickHere
Updated on Mar 16, 2023 17:18 IST

Jump statements manipulate flow of program when certain conditions are met. It gets terminated or continues the loop within a program or stop the execution of a function.

2023_02_MicrosoftTeams-image-153.jpg

In this tutorial, you will be introduced to jump statements in C++ and discuss the various kinds of jump statements with the aid of suitable examples.

We will be covering the following sections:

C++ Control Statements

In C++, control statements are statements that alter the flow of a program’s execution. They allow the program to make decisions, perform repetitive tasks, and change the sequence of program execution. Control statements are essential for programming because they make it possible to create more complex and flexible programs.

There are three main types of control statements in C++: decision-making, iterative, and jump statements.

  • Decision-making statements (such as if, if-else, switch) allow the program to make a decision based on a condition and execute different code depending on whether the condition is true or false.
  • Iterative statements (such as for, while, do-while) allow the program to repeat a block of code until a certain condition is met.
  • Jump statements (such as break, continue, goto, return) allow the program to transfer control to a different part of the program, skip over parts of the code, or exit a function early.

Explore free C++ courses

Jump statements in C++

There are four main types of jump statements in C++:

  1. Break statement
  2. Continue statement
  3. Goto statement
  4. Return statement

Let’s Loos at each type in detail below:

1. The “break” statement

The “break” statement in C++ is a control statement used to immediately terminate a loop such as a “for” or “while” loop, and move on to the next statement following the loop.

When a “break” statement is executed inside a loop, the program immediately jumps to the statement following the loop, skipping any remaining iterations of the loop. The “break” statement is typically used in conjunction with a conditional statement, allowing the loop to be terminated when a specific condition is met.

Here is an example of using the “break” statement inside a “while” loop in C++:

 
#include
\n <iostream>\n
\n
using namespace std;\n
\n
int main() {\n
int i = 1;\n
\n
while (i <= 10) {\n
if (i == 5) {\n
break;\n
}\n
cout << "i = " << i << endl;\n
i++;\n
}\n
\n
cout << "Loop terminated at i = " << i << endl;\n
return 0;\n
}\n
\n </iostream>
Copy code

In this example, the “while” loop starts at 1 and continues until i is no longer less than or equal to 10. Within the loop, there is an “if” statement that checks if i is equal to 5. If i is equal to 5, the “break” statement is executed, terminating the loop and moving on to the next line of code after the loop. If i is not equal to 5, the value of i is printed to the console and i is incremented by 1.

When you run the above code, you will see the following result:

Output:

 
i = 1
i = 2
i = 3
i = 4
Copy code

As you can see, the loop is terminated at i = 5, and the code following the loop is executed.

2. The “continue” statement

The “continue” statement in C++ is a control statement used inside a loop to skip the remaining statements within the loop and continue with the next iteration of the loop.

When a “continue” statement is executed inside a loop, the program skips the remaining statements within the loop and moves on to the next iteration of the loop. The “continue” statement is typically used in conjunction with a conditional statement, allowing certain iterations of the loop to be skipped when a specific condition is met.

Here is an example of using the “continue” statement inside a “for” loop in C++:

 
#include
\n <iostream>\n
\n
using namespace std;\n
\n
int main() {\n
for (int i = 1; i <= 10; i++) {\n
if (i == 5) {\n
continue;\n
}\n
cout << "i = " << i << endl;\n
}\n
return 0;\n
}\n
\n </iostream>
Copy code

In this example, the “for” loop starts at 1 and continues until i is no longer less than or equal to 10. Within the loop, there is an “if” statement that checks if i is equal to 5. If i is equal to 5, the “continue” statement is executed, skipping the remaining statements within the loop and moving on to the next iteration of the loop. If i is not equal to 5, the value of i is printed to the console.

When you run the above code, you will see the following result:

Output:

 
i= 1
i = 2
i = 3
i = 4
i = 5
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10
Copy code

As you can see, the loop skips printing “i = 5” because the “continue” statement is executed when i is equal to 5, and the program moves on to the next iteration of the loop.

3. The “goto” statement

The “goto” statement in C++ is a control statement used to transfer control of a program to a labeled statement.

The “goto” statement allows the program to jump from one part of the program to another part that has been labeled with a specific identifier. This statement is often used when a particular block of code needs to be executed in response to a certain event, and the use of a loop or a conditional statement would be less efficient or less clear.

Here is an example of using the “goto” statement in C++:

 
#include
\n <iostream>\n
\n
using namespace std;\n
\n
int main() {\n
int x = 1;\n
\n
start:\n
if (x == 5) {\n
cout << "Value of x is 5." << endl;\n
goto end;\n
}\n
else {\n
cout << "Value of x is " << x << "." << endl;\n
x++;\n
goto start;\n
}\n
\n
end:\n
cout << "Program complete." << endl;\n
return 0;\n
}\n
\n </iostream>
Copy code

In this example, the program starts by initializing the variable x to 1. The code then jumps to the “start” label, which begins a loop. Within the loop, an “if” statement checks if x is equal to 5. If x is equal to 5, the program jumps to the “end” label and outputs a message indicating that the program is complete. If x is not equal to 5, the program outputs the current value of x, increments x by 1, and jumps back to the “start” label.

When you run the above code, you will see the following result:

Output:

 
Value of x is 1.
Value of x is 2.
Value of x is 3.
Value of x is 4.
Value of x is 5.
Program complete
Copy code

As you can see, the “goto” statement is used to create a loop that repeats until the value of x is 5. Once the value of x is 5, the program jumps to the “end” label and outputs a message indicating that the program is complete.

4. The “return” statement

The “return” statement in C++ is used to exit a function and return a value to the caller.

When a “return” statement is executed, the program stops executing the current function and returns the specified value to the point where the function was called. The value returned by the function can be used by the caller in further calculations or processing.

Here is an example of using the “return” statement in C++:

 
#include <iostream>
using namespace std;
int addNumbers(int a, int b) {
int result = a + b;
return result;
}
int main() {
int x = 5;
int y = 10;
int sum = addNumbers(x, y);
cout << "The sum of " << x << " and " << y << " is " << sum << "." << endl;
return 0;
}
Copy code
 
#include <iostream>
using namespace std;
int addNumbers(int a, int b) {
int result = a + b;
return result;
}
int main() {
int x = 5;
int y = 10;
int sum = addNumbers(x, y);
cout << "The sum of " << x << " and " << y << " is " << sum << "." << endl;
return 0;
}
Copy code

In this example, the program defines a function called “addNumbers” that takes two integer arguments and returns the sum of those integers. The “main” function then initializes two integer variables, x and y, to 5 and 10, respectively. The “addNumbers” function is called with the values of x and y as arguments, and the return value is assigned to a variable called “sum”. Finally, the program outputs a message to the console that displays the values of x, y, and the sum.

When you run the above code, you will see the following result:

Output:

 
The sum of 5 and 10 is 15.
Copy code

As you can see, the “return” statement is used to exit the “addNumbers” function and return the sum of the two arguments to the point where the function was called. The value of the “sum” variable is then used in the “cout” statement in the “main” function to display the sum of x and y.

Endnotes

In conclusion, control statements are an important feature of C++ that allow programmers to control the flow of a program. There are three basic types of control statements in C++: decision-making, iterative, and jump statements. Jump statements, such as “continue,” “break,” “return,” and “goto,” are used to transfer control of a program to a different part of the program, to skip over certain parts of the program, or to exit a function and return a value to the caller. Hope this article helped you grasp the concept of jump statements in C++. Explore our C++ articles to find out more about the language and consolidate your knowledge of the fundamentals.  

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio