Jump statements are utilized to control the progression of the program assuming that a few conditions are met. It is utilized to ending or continues the loop inside a program or to stop the execution of a function. In C++ there is four jump statements: continue, break, return, and goto.
Continue Statement in C++ :
It is utilized to execute different parts of the loop while skipping a few sections declared inside the condition, as opposed to terminating the loop, it continues to execute the following iteration of a similar loop. It is utilized with a decision-making statement which should be available inside the loop. This statement can be involved inside for loop or while or do-while loop.
Syntax:
continue;Think about a situation where every one of the numbers somewhere in the range of 1 and 10 except number 5. So for this situation, the idea is to use the proceed with statement after the value of i is 5. The following is the program for the something very similar:
// 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 9Break Statement in C++ :
It is utilized to end the whole loop in the event that the condition is met. Dissimilar to the continue statement after the condition is met, it breaks the loop and the it isn't executed to remain part of the loop. Break statement is utilized with decision-making statements for example if, if-else, or switch statement which is inside the for loop which can be for loop, while loop, or do-while loop. It powers the loop to stop the execution of the further emphasis.
Syntax:
break;Think about a situation where the series of a number is to be printed but not after a certain value K. So for this situation, the idea is to utilize the break statement after the value of i is K. The following is the program for the something very similar:
// 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:
1 2 3 4Return Statement in C++ :
It removes control from the function itself. It is more grounded than a break. It is utilized to end the whole function after the execution of the function or after some condition. Every function has a return statement with some returning value aside from the void() function. Although void() function can likewise have the return statement to end the execution of the function.
Syntax:
return expression;The following is the program to demonstrate the return statement:
// 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 4Explanation:
The above program begins execution by printing “Begin” then, at the point,the for loop starts to print the value of, it will print the worth of i from 0 to 4 however when i becomes equal to 5 it will end the entire function i.e., it won't ever go to print the “end” statement of the program.
The return in void() functions can be utilized without any return type.
Syntax:
return;The following is the program to demonstrate the return statement in void return type in function:
// C++ program to demonstrate the return
// statement in void return type function
#include <iostream>
using namespace std;
// Function to find the greater element
// among x and y
void findGreater(int x, int y)
{
if (x > y) {
cout << x << " "
< "is greater"
<< "\n";
return;
}
else {
cout << y << " "
<< "is greater"
<< "\n";
return;
}
}
// Driver Code
int main()
{
// Function Call
findGreater(10, 20);
return 0;
}
Output:
20 is greaterGo to Statement in C++ :
This statement is utilized to jump directly to that piece of the program to which it is being called. Each goto statement is related with the label which takes them to part of the program for which they are called. The label statements can be composed anywhere in the program it isn't important to use before or after goto statement. This statement makes it hard to understand the progression of the program therefore it is avoided to use it in a program.
Syntax:
goto label_name; . . . label_name:Below is the program to demonstrate the goto statement:
// 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:
EvenExplanation:
The above program is utilized to check whether the number is even or odd assuming the number pressed by the user says it is 4 so the condition is met by the if statement and control go to label1 and label1 prints that the number is even. Here it isn't important to write a label statement after the goto statement we can write it before goto statement likewise it will work fine.
0 Comments
🙏🙏please don't enter any spam links on the comment box.....