Jump statements are incorporated to alter the program's flow under specific conditions. They are employed within a program to either terminate or proceed through a loop, or to temporarily halt the execution of a function. C++ encompasses four jump statements: continue, break, return, and goto.
Continue:
Instead of halting the loop, it proceeds to the subsequent iteration of the loop while excluding the sections defined by the condition. It is essential to pair it with a conditional statement within the loop, which can be employed in a for, while, or do-while loop.
1 st Program:
Consider a scenario where all numbers from 1 to 15 are included except for 7. In this case, when the value of j reaches 7, the objective is to implement the continue statement. Below is the code snippet for achieving this:
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 specified condition is satisfied, the loop is concluded using the break directive. Upon meeting the condition, the loop is interrupted, skipping the remaining iterations, which differs from the behavior of the continue statement. Break commands are typically employed alongside conditional statements like if, if-else, or switch within loops, which can be any of for, while, or do-while loops. This action results in the termination of further loop executions.
2 nd Program:
Consider a situation where a numerical sequence needs to be exhibited but only up to a certain value, denoted as p. In such cases, the break statement comes into play when the value of j reaches the specified limit of p. Below is the program illustrating this concept:
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 transfers control out of the function. More potent than a break statement, it halts the function entirely once it finishes executing or meets a specific condition. With the exception of the void function, all functions include a return statement that sends back a value. Even the void function can incorporate a return statement to wrap up its execution.
3 rd Program:
The software below illustrates the utilization of 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 program begins execution by outputting "Begin." Following this, the for loop initiates printing the values. It will sequentially display the values of I starting from 0 up to 6. However, once I equals 7, the program will terminate abruptly without reaching the "end" statement.
The return statement in functions with void return type can be utilized without explicitly defining a return value.
5 th program:
The upcoming code example showcases the implementation of the return statement within a function with a void return type:
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 command allows us to navigate instantly to the specific part of the program mentioned. Every goto command is associated with a label that guides it to the corresponding section of the program. Labels can be placed at any position within the program without the necessity of being positioned before or after goto commands. Usage of this command can complicate the comprehension of the program flow, which is why it is typically discouraged in programming practices.
6 th program:
The upcoming code example showcases the utilization of 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:
In this code snippet, the purpose is to differentiate between even and odd numbers. When the user inputs the number 7, the if statement evaluates to true, directing the program flow to label01, where the message indicating that the number is odd is displayed. Placing the label statement after the goto command is optional in this scenario; the code functions equally effectively if the label statement precedes the goto command.