Jump statements in c++
These are the statements which change the flow of execution. Mainly, it transfers the code flow into another part of the program.
For example, in switch case, it uses break.
The list of jump statements is given below...
- 1. ‘break’
- 2. ‘continue’
- 3. ‘return’ and ‘goto’
Let us create programs using these statements.
1.‘break’ statement:
This statement
is used to make the program stop the current flow.
C++ Program:
#include <iostream>
using namespace std;
int main() {
int a=2;
switch(a)
{
case 1:
cout<<"The value is one";break;
case 2:
cout<<"The value is two";break;
default:
cout<<"The value is not in range";
}
return 0;
}
Output:
The value is two
2.‘continue’ statement:
This statement
makes the program flows to continue the process.
C++ program:
#include <iostream>
using namespace std;
int main() {
int i;
for (i = 0; i <
5; i++) {
if (i == 3)
continue;
printf("%d
", i);
}
return 0;
}
Output:
0 1 2 4
3.‘goto’ statement and ‘return’:
‘goto’
statement is used to make the program control to another location. ‘return’ is
used to return the value from function.
C++ Program:
#include <iostream>
using namespace std;
int main() {
int i = 2;
loop:
printf("%d ", i++);
if (i <= 6) goto loop;
return 0;
}
Output:
2 3 4 5 6
These are the types of jump statements in c++. Hope, you
understood the sample programs. Keep coding!!
Comments
Post a Comment