jump-statements-cpp

C++ | Jump Statements

In this article, we will learn about the Jump statements and their working in the C++ programming language with the help of examples.

Jump Statements

The Jump Statements are used to manipulate the flow of a program if some conditions are met. There are statements that can be used to terminate or continue a loop or to stop the execution of a function.

In C++ there are four Jump Statements:

  • continue
  • break
  • return
  • goto

continue statement: It is used to skip the current iteration of a loop. Rather than terminating the loop, it continues to execute the next iteration of the same loop. It must be used with a condition that is a decision-making statement inside the loop. This statement can be used with a for loop or while or do-while loop.

Syntax:

continue;

Example:

// C++ program to demonstrate the
// continue statement
#include <iostream>
using namespace std;
  
// Driver code
int main()
{
    for (int i = 1; i < 10; i++) {
  
        if (i == 5)
            continue;
        cout << i << " ";
    }
    return 0;
}

Output:

1 2 3 4 6 7 8 9

Flow Chart:

break Statement:

It is used to terminate the whole loop if the condition is met. Unlike the continue statement after the condition is met, the break statement terminates the loop and the remaining part of the loop is not executed. The break statement is used along with if, if-else, or switch statement.

Syntax:

break;

Example:

// C++ program to demonstrate the
// break statement
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    for (int i = 1; i < 10; i++) {
  
        // Breaking Condition
        if (i == 5)
            break;
        cout << i << " ";
    }
    return 0;
}

Output:

See also  ABSTRACT CLASSES IN C++

1 2 3 4

Flow Chart:

return statement:

This is used to take control out of a function. It helps to terminate the execution of the function after a certain condition is met or after completely executing the function. Every function except the void functions has a return statement at the end after all the statements inside the function, sometimes it might also be used along with a condition in the middle of the function code.

Note: We use the return statement in functions, we will know about functions in future articles. So, no worries

Syntax:

return expression; // to return a value

return; // to just terminate the execution of the function

Example:

// C++ program to demonstrate the
// return statement
#include <iostream>
using namespace std;
  
// Driver code
int main()
{
    cout << "Begin ";
  
    for (int i = 0; i < 10; i++) {
  
        // Termination condition
        if (i == 5)
            return 0;
        cout << i << " ";
    }
    cout << "end";
  
    return 0;
}

Output:

Begin 0 1 2 3 4

Diagrammatic Representation to understand the return statement:

goto statement:

The goto statement is used to directly jump to a certain part of the code that might be forward or backward. Every goto statement is associated with a label that tells the goto statement to jump to that part of the code. But because of this, understanding the flow of the execution becomes much more difficult. So, goto is not widely used in programs.

Syntax:

goto label_name;

.

.

.

label_name:

Example:

// C++ program to demonstrate the
// goto statement
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    int n = 4;
  
    if (n % 2 == 0)
        goto label1;
    else
        goto label2;
  
label1:
    cout << "Even" << endl;
    return 0;
  
label2:
    cout << "Odd" << endl;
}

Output:

Even

See also  C++ Vectors

Flow Chart:

That is all for this article in the next one we will learn about switch statements and an application using the switch statement.

Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.