When encountered, jump statements are used to shift program control from one part of the program to any other part of the program unconditionally. In C++, jump statements are implemented using :
Within loops or iterative statements, continue statements are used. The continue statement’s goal is to skip the remaining statements of the current iteration of the loop and proceed to the next iteration.
// 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%2 == 0)
continue;
cout << i << " ";
}
return 0;
}
1 3 5 7 9
The break statement allows us to jump out of the loop instantly, without waiting to get back to the conditional test. When the break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if.
#include <iostream>
using namespace std;
// Driver code
int main()
{
for (int i = 1; i < 10; i++) {
if (i > 6)
break;
cout << i << " ";
}
return 0;
}
1 2 3 4 5 6
Return statements can be used anywhere in the function. The return statement’s goal is to halt the current function’s execution and return program control to the point from which it was called. If a return statement is used in the main function, then it will stop the compilation process of the program.
// 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;
}
Begin 0 1 2 3 4
This statement is used to transfer control to the tagged statement in the program. The tag is the valid identifier and placed just before the statement from where the control is transferred. The usage of the goto keyword should be avoided as it usually violates the normal flow of execution.
#include <iostream>
using namespace std;
int main()
{
int n = 4;
if (n % 2 == 0)
goto label1;
else
goto label2;
label1:
cout << "Even" << endl;
return 0;
label2:
cout << "Odd" << endl;
}
Even
Note: also read about Loops in C++
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…