In C language there are 4 types of jump statements:
- Break Statement
- Continue Statement
- Goto Statement
- Return Statement
Break Statement: Break statement is used to terminate the loops like for, while, do-while from the subsequent execution, it also terminates switch statement.
syntax:
break;
Example:
// Program to demostrate break statement.
# include <stdio.h>
# include <conio.h>
void main()
{
int num=5; // initializing the variable
while(num>0)
{
if(num==3)
break;
printf("%d",num);
num--;
}
}
Output:
5 4
Continue Statement: Continue statement is used to continue the next iteration of a loop, so the remaining statements are skipped within the loop.
Syntax:
continue;
Example:
// Program to demostrate continue statement.
# include <stdio.h>
# include <conio.h>
void main()
{
int num=1; // initializing the variable
for(i = 0; i<= 10; i++)
{
if(i==5 || i==6)
{
printf("\n Skiping %d\n",i);
continue;
}
printf("%d",i);
}
}
Output:
0 1 2 3 4
Skiping 5
Skiping 5
7 8 9
Goto Statement: goto statement is used to transfer the normal flow of a program to the specified label in the program.
Note => The use of goto statement should be avoided as it usually violets the normal flow of execution.
Syntax:
{
......
goto sos
......
......
sos:
statements;
}
Example:
// Program to demostrate goto statement.
# include <stdio.h>
# include <conio.h>
void main()
{
int num=1; // initializing the variable
for(i = 0; i<= 10; i++)
{
if(i==5)
{
goto sos;
}
printf("%d",i);
}
sos:
printf("\n Now, this is 5");
}
Output:
0 1 2 3 4
Now, this is 5
Return Statement:
Return Statement: Return statement is used to end or terminate a function immediately with or without value and continues the flow of program execution from where it was called.
The function declared with void type does not return any value.
Syntax:
return;
or
return value;
Example:
// Program to demostrate return statement.
# include <stdio.h>
# include <conio.h>
void main()
{
int sum = sum();
printf("Sum of digits from 1 to 10 = %d\n",sum);
}
int sum()
{
int sum = 0;
int i;
for( i = 0; i <= 10 ; i++) {
sum += i;
}
return sum;
}
Output:
Sum of digits from 1 to 10 = 55