Jump Statements in C++

Jump statements are implemented to change the flow of the program when particular conditions are satisfied. It is used within a program to end or continue a loop or to pause the execution of a function. C++ has four jump statements: continue, break, return, and goto.

Continue:

Instead of terminating the loop, it performs the next iteration of the same loop but ignoring the parts specified by the condition. Within the loop, it must be used in conjunction with a decision-making statement. This statement can be used within a for, while, or do-while loop.

1st Program:

Consider a situation in which all of the numbers between 1 and 15 except 7 are present. So, after the value of j is 7, the goal is to use the continue statement. The program for the same is as follows:

C++ Program:

Output:

1 2 3 4 5 6 8 9 10 11 12 13 14

Break:

If the condition is met, the loop is terminated with the break command. When the condition is met, the loop is broken and the remainder of the loop is skipped, unlike the continue statement. Break statements are used in conjunction with decision-making statements such as if, if-else, or switch statements in a for loop, which can be a for loop, while loop, or do-while loop. It causes the loop to stop executing future iterations.

2nd Program:

Consider the case when a number series is to be displayed but not after a specified value p. In this scenario, the break statement is used once the value of j is p. The program or the same is as follows:

C++ Program:

Output:

1 2 3 4 5 6

Return:

It removes control from the function. It is more powerful than a break. It is used to end the entire function after the function has completed or after a condition has been met. Except for the void() function, every function contains a return statement that returns some value. Although the void() function can also have a return statement to conclude the function's execution.

3rd Program:

The following software demonstrates the return statement:

C++ Program:

Output:

Start with 0 1 2 3 4 5 6

Explanation:

The preceding programme starts run by printing "Begin," then the for loop begins to print the value of, it will display the value of I from 0 to 6, but as soon as I reaches equal to 7, it will end the entire function, i.e., it will never output the program's "end" line.

The return keyword in void() functions may be used without specifying a return type.

5th program:

The following program demonstrates the use of the return statement in the void return type in function:

C++ Program:

Output:

35 is greater

Goto:

This statement enables us to jump directly to the section of the program that is being referenced. Each goto statement is connected with a label that directs them to the section of the programme for which they are named. Label statements can be written anywhere in the program; no before or after goto statements are required. This statement makes understanding the flow of the program difficult, hence it is avoided in program.

6th program:

The following programme demonstrates the goto statement:

C++ Program:

Output:

Odd

Explanation: The shown code is used to determine if a number is even or odd. If the number entered by the user is 7, the condition is satisfied by the if statement, and control is passed to label01, which displays the number is odd. It is not required to put a label statement after the goto statement in this case; it will work just as well if we write it before the goto statement.