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.
1 st 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:
// C++ code that shows how to use the continue statement
#include <iostream>
using namespace std;
// main function
int main ()
{
for (int j = 1; j < 15; j++) {
if (j == 7)
continue;
cout << j << " ";
}
return 0;
}
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.
2 nd 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:
// C++ code that shows how to use the break statement
#include <iostream>
using namespace std;
// main function
int main ()
{
for (int j = 1; j < 15; j++) {
// using break Condition
if (j == 7)
break;
cout << j << " ";
}
return 0;
}
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.
3 rd Program:
The following software demonstrates the return statement:
C++ Program:
// C++ code that shows how to use the return statement
#include <iostream>
using namespace std;
// main function
int main()
{
cout << "Start with ";
for (int j = 0; j < 15; j++) {
if (j == 7)
return 0;
cout << j << " ";
}
cout << "end";
return 0;
}
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.
5 th program:
The following program demonstrates the use of the return statement in the void return type in function:
C++ Program:
// C++ code demonstrating the use of the return statement in void return type functions.
#include <iostream>
using namespace std;
// Find the bigger element between p and q.
void findingGreater (int p, int q)
{
if (p > q) {
cout << p << " "
<< "is greater"
<< "\n";
return;
}
else {
cout << q << " "
<< "is greater"
<< "\n";
return;
}
}
// main function
int main ()
{
// calling the Function
findingGreater (25, 35);
return 0;
}
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.
6 th program:
The following programme demonstrates the goto statement:
C++ Program:
// C++ code to show the goto statement
#include <iostream>
using namespace std;
// main function
int main ()
{
int p = 7;
if (p % 2 == 0)
goto label01;
else
goto label02;
label01:
cout << "Even" << endl;
return 0;
label02:
cout << "Odd" << endl;
}
Output:
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.