Jump statements in c++ are use to transfer control of program execution Unconditionally from one part of the program to another. There are four kind of jump statements in c++ i.e. break statement, continue statement, goto statement, and return statement are discuss below:
What is Break statements in c++
The use of break statement causes the immediate termination of switch statement and the loop(all type of loop) from the point of break statement. The control then passes to the statement following the switch statement and the loop. It can be use to end and infinite loop, or to force it to end before its nature end. For example we are going to stop the count down before it naturally finishes (an engine failure maybe).
Program to illustrate the (jump statement) use of Break statements in c++
#include<iostream>
using namespace std;
int main ()
{
int n;
for(n=10; n>0; n--)
{
cout<<n<<",";
if(n==3)
{
cout<<"countdown abort!";
break;
}
}
return 0;
}
In the above code loop is terminated immediately after the value of i is 3. In case of nested loop if break statement is in inner loop the inner loop is terminated.
What is continue statement in c++
The continue statement terminate the current iteration of a while, for and do while loop statement. And resumes execution back at the beginning of the loop body with the iteration. Used to bypass the remainder of the current pass through the loop. The loop doesn’t terminate when a continue statement is encountered. Instead remaining loop statement are escaped and the computation proceed directly to the next pass through the loop.
Program to illustrate the (jump statement) use of continue statement in c++
#include<iostream>
using namespace std;
int main ()
{
int n;
for(n=0; n<8; n++)
{
cout<<n<<"n";
if(n%2 == 1)
continue;
cout<<"this is even\n";
}
return 0;
}
Output
n=0 this is even
n=1
n=2 this is even
n=3
n=4this is even
n=5
Th goto statement in c++
It allows making an absolute jump to another point in the program. You should use this feature carefully since its execution ignores any type of nesting in Limitation. The destination point is identify by a label, which is then used as an argument for the goto instruction. A Is made of a valid identifier followed by a colon(:). This instruction does not have or concrete utility in a structure object oriented programming aside from those that low level programming fans may find for it.
write a Program to illustrate (jump statement ), the use of continue statement in c++
#include<iostream>
using namespace std;
int main ()
{
int n=8;
loop:
cout<0)
goto loop;
cout<<"fire!";
return 0;
}
Output
8 7 6 5 4 3 2 1 fire!
The return statement in C++
The return statement is used to transfer the program control back to the caller of the method. There are two forms of Return statement. First With general form return expression; returns the value whereas the second with the form returned; returns no value but only the control to the caller.
Related term in C++
What is macros in c++?Ans: Macros in c++ are designed to reduce function call overhead in case of a small functions. Concept of macros are not new to C++, they are also supported by C language. Macros are defined by using hash sign because….Read more
What is endl and setw manipulators in c++?
Ans:endl and setw manipulators in c++ : Are the operators use in the insertion operator(<<) to modify or manipulate the way data is displayed in I/O stream...Read more