Table of Contents
Back Button
Back to Chapter
C++ Setting Up
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.

C++ Jump Statements

Jump statements are used to manipulate the course 'of the program, if some conditions are met.

In C++ there are 4 jump statements continue, break, return and goto.

Let us look into each of them in more detail.

Continue

It is used to execute other parts of the loop while skipping some parts declared inside the condition, rather than terminating the loop, it continues to execute the next iteration of the same loop.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4929ee3e-e94f-4f14-a80f-7ecbff49f709/Untitled_Diagram_(2).png

Consider the following example to understand better.

#include using namespace std; // Driver code int main() { for (int i = 1; i < 10; i++) { if (i == 6) continue; cout << i << " "; } return 0; }
Output: 1 2 3 4 5 7 8 9
Caution : continue is used to continue remaining statements of a loop in case an if statement is encountered in a statement. Use it when the loop to go through other else if statements even after the first if condition is met.

Break

It is used to terminate the whole loop if the specified condition is met. Unlike the continue statement once the condition is met, break breaks the loop and the remaining part of the loop is not executed.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/7c81ab3b-15f1-4632-af40-2e7a12fafb5e/Untitled_Diagram_(3).png

Consider the example given below.

#include using namespace std; int main() { for (int i = 1; i < 10; i++) { // Breaking Condition if (i == 6) break; cout << i << " "; } return 0; }
Output: 1 2 3 4 5

Return

It takes control out of the function. It is stronger than break. It is used to terminate the entire function after the execution of the function or after some condition. Every function has a return statement with some returning value except the void()function.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/efdb168d-dfb2-48e9-a749-18d1e70ca862/Untitled_Diagram_(4).png
#include using namespace std; // Driver code int main() { cout << "Begin "; for (int i = 0; i < 10; i++) { if (i == 6) return 0; cout << i << " "; } cout << "end"; return 0; }
Output: Begin 0 1 2 3 4

Consider a second example

#include using namespace std; // Function to find the greater element // among x and y void findGreaterOf(int x, int y) { if (x > y) { cout << x << " " << "is greater" << "\n"; return; } else { cout << y << " " << "is greater" << "\n"; return; } } int main() { findGreaterOf(10, 20); return 0; }
Output: 20 is greater

Goto

This statement is used to directly jump to that part of the program to which it is being called. Every goto statement is associated with a label that takes them to part of the program for which they are called. The label statements can be written anywhere in the program it is not necessary to use before or after the goto statement. This statement makes it difficult to understand the program hence it is considered dangerous must be avoided.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/54a80af9-52a1-4333-a25e-e7d427919b70/Untitled_Diagram_(5).png

Consider this example to better understand goto statements:

#include using namespace std; int main() { int n = 5; if (n % 2 == 0) goto labelI; else goto labelII; labelI: cout << "Even:" << endl; return 0; labelII: cout << "Odd:" <
Output: Odd:5
Caution: goto statement is a very tricky and dangerous statement, using it leads to unwieldy programs. It is considered a poor programming practice