In this article, we'll describe the jump statements in the programming language.
What are the jump statements in the programming language?
Jump statements in programming language are used to escape the particular section of the program. The jump statements used within loops such as for loop,while loop, do-while loop, switch statements, and function.
Type of Jump Statements
- Break Statement
- Continue Statement
- Return Statement
- Goto Statement
Here all jump statements are described in detail.
1. Break Statement:
Break statement is used to exit the loop such as for loop, while loop, do-while loop, and switch statement and start executing the next block of the code. It also terminates the function.
Syntax of break:
break;
C Code:#include<stdio.h>
void main()
{
int n=1;
while(n<=15)
{
if(n==5)
break;
printf("Print=%d \n",n);
n++;
}
printf("Outside the loop");
}
Output:
Print=1
Print=2
Print=3
Print=4
Outside the loop
#include <iostream>
int main() {
int n=1;
while(n<=10)
{
if(n==5)
break;
std::cout<<"\nPrint="<<n;
n++;
}
std::cout<<"\nOutside the loop";
return 0;
}
Output:
Print=1
Print=2
Print=3
Print=4
Outside the loop
class HelloWorld {
public static void main(String[] args) {
int n=1;
while(n<=10)
{
if(n==5)
break;
System.out.println("Print="+n);
n++;
}
System.out.println("Outside the loop");
}
}
}
Output:Print=1 Print=2 Print=3 Print=4 Outside the loop
Print=1
Print=2
Print=3
Print=4
Outside the loop
2. Continue Statement:
Continue Statement is used to skip the specific iteration in the loop and continues from the next iteration in the same loop
Syntax of break:
continue;
C Code:
#include<stdio.h>
void main()
{
int n;
for(n=1;n<=5;n++)
{
if(n==3)
continue;
printf("Print=%d \n",n);
}
}
Output:
Print=1
Print=2
Print=4
Print=5
#include <iostream>
int main() {
int n; for(n=1;n<=5;n++)
{
if(n==3)
continue;
std::cout<<"\nPrint="<<n;
}
return 0;
}
Output:
Print=1
Print=2
Print=4
Print=5
class HelloWorld {
public static void main(String[] args) {
int n; for(n=1;n<=5;n++)
{
if(n==3)
continue;
System.out.println("Print="+n);
}
}
}
Output:
Print=1
Print=2
Print=4
Print=5
3. Return Statement:
The return statement is a type of jump statement which is used to terminate the function with the value or without the value.
Syntax of break:
return;
or
return expression;
C Code:
#include<stdio.h>
int main()
{
int n;
for(n=1;n<=5;n++)
{
printf("Print=%d \n",n);
}
return 0;
}
Output:
Print=1
Print=2
Print=3
Print=4
Print=5
#include <iostream>
int main() {
int n;
for(n=1;n<=5;n++)
{
std::cout<<"\nPrint="<<n;
}
return 0;
}
Output:
Print=1
Print=2
Print=3
Print=4
Print=5
class HelloWorld {
public static void main(String[] args) {
int n;
for(n=1;n<=5;n++)
{
if(n==3)
continue;
System.out.println("Print="+n);
return 0;
}
}
}
Output:
Print=1
Print=2
Print=3
Print=4
Print=5
4. Goto Statement:
Goto statement is similar to the continue statement and goto is a type of jump statement. Continue statement is only used in loops but the goto statement is used anywhere in a program.
The label is used in the goto statement to tell the program control where to go.
Syntax of break:
goto label;
.
label:
C Code:
#include<stdio.h>
int main()
{
int n=1;
label:
printf("Print=%d\n",n);
n++;
if(n<=5)
{
goto label;
}
return 0;
}
Output:
Print=1
Print=2
Print=3
Print=4
Print=5
#include<iostream>
int main()
{
int n=1;
label:
std::cout<<"\nPrint="<<n;
n++;
if(n<=5)
{
goto label;
}
return 0;
}
Output:
Print=1
Print=2
Print=3
Print=4
Print=5
Java does not support goto statement;
Hope this article is helpful for you!
0 Comments