Nothing Special   »   [go: up one dir, main page]

Open In App

Jump statements in C++

Last Updated : 12 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Jump statements are used to manipulate the flow of the program if some conditions are met. It is used to terminate or continue the loop inside a program or to stop the execution of a function.

In C++, there is four jump statement:

continue Statement

The C++ continue statement 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. It is used with a decision-making statement which must be present inside the loop.

This statement can be used inside for loop or while or do-while loop. Let’s take a look at an example:

C++
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i < 10; i++) {

          // Skip the execution for i = 5
        if (i == 5)
            continue;
        cout << i << " ";
    }
    return 0;
}

Output
1 2 3 4 6 7 8 9 

Explanation: Loop prints all the numbers from 1 to 10 except number 5. So, in this case, the idea is to use the continue statement after the value of i is 5.

Flowchart of continue Statement

flowchart of continue in c++

break Statement

The C++ break statement is used to terminate the whole loop if the condition is met. It is used with decision-making statements such as if, if-else, or switch statement to break out of the block. It forces the loop to stop the execution of the further iteration.

Let’s take a look at an example:

C++
#include <iostream>
using namespace std;

int main() {
  
      // Loop to print digits from 1 to 4
    for (int i = 1; i < 10; i++) {

        // Breaking Condition
        if (i == 5)
            break;
        cout << i << " ";
    }
  
    return 0;
}

Output
1 2 3 4 

Explanation: In this case, the break statement is used after the value of i is 5.

Flowchart of break Statement

flowchart of break in c++

return Statement

The return statement takes control out of the function itself. It is stronger than a 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. Although void() function can also have the return statement to end the execution of the function.

Let’s take a look at an example:

C++
#include <iostream>
using namespace std;

void findNum (int n) {
      for (int i = 0; i <= 100; i++) {
          if (i == n) {
              cout << "Number in Range [0, 100]";
              
              // This will stop the function here
              return;
        }
    }
      cout << "Number not in Range [0, 100]";
      
      // Final return if the above one is not reached.
      return;
}

int main() {
    int n = 10;

      // Caaling the function
    findNum(n);

    return 0;
}

Output
Number in Range [0, 100]

Explanation: The first return statement in the function findNum() will be executed as n = 10 which is the range [0, 100]. So, the function execution will be stopped then and there and will return to the main function.

goto Statement

The C++ goto statement is used to jump directly to that part of the program to which it is being called.  Every goto statement is associated with the label which 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 them before or after the goto statement.

Let’s take a look at an example:

C++
#include <iostream>
using namespace std;

int main() {
    int n = 4;

    if (n % 2 == 0) {
      
          // Skipping to label1
        goto label1;
    }
    else {
      
          // Skipping to label1
        goto label2;
    }

label1:
    cout << "Even" << endl;
    return 0;

label2:
    cout << "Odd" << endl;
  
    return 0;
}

Output
Even

Explanation: The above program is used to check whether the number is even or odd if the number pressed by the user says it is 4 so the condition is met by the if statement and control go to label1 and label1 prints that the number is even. Here it is not necessary to write a label statement after the goto statement we can write it before goto statement also it will work fine.

Flowchart of goto Statement

flowchart of goto in c++

Note: The goto statement makes it difficult to understand the flow of the program therefore it is avoided to use it in a program.



Next Article

Similar Reads

three90RightbarBannerImg