C Jumping Statements With Example

What is the Jumping Statements?

We often come across situations where we want to jump out of a loop instantly, without waiting to get back to the condition test.

There are few jumping statements:

  1. break statement
  2. goto statement
  3. continue statement.

goto Statement in C

It is used to alter the program execution sequence by transferring the control to some other part of the program.

Types of goto statement:

Conditional goto statement:

This statement is used to transfer the control of the execution from one part of the program to the other in certain conditional cases.

Example of conditional goto statement:

#include <stdio.h>
#include <stdlib.h>
int main()
{
int x,y;
printf("Enter the value of x:");
scanf("%d", &x);
printf("Enter the value of y:");
scanf("%d", &y);
if(x>y){
    goto label1;
    }
else
    goto label2;
    label1:
        printf("The largest value is x=%d",x);
        goto FINISH;
        label2:
            printf("The largest value is Y=%d",y);
            FINISH:
 
return 0;
}
 

Output:

Enter the value of x:7
Enter the value of y:5
The largest value is x=7

Unconditional goto statement:

This statement is used to transfer the control from one part of the program to other parts without checking any condition.

It may lead to a very complicated problem like a never ending process, so a good programmer will not prefer to use the unconditional goto statement in the programm.

Example of unconditional goto statement:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    a:
    printf("Learn Simply\n");
    goto a;
return 0;
}
 

Output:

Learn Simply
Learn Simply
Learn Simply
Learn Simply
Learn Simply
Learn Simply
Learn Simply
Learn Simply
Learn Simply
Learn Simply
Learn Simply
Learn Simply
Learn Simply
Learn Simply
Learn Simply
_

This execution runs infinity times.

Continue Statement in C

In some programming situations we want to take the control to the beginning of the loop or bypassing the statements inside the loop.

The keyword “continue” allows us to do this. When continue is encountered inside any loop, control automatically passes to the beginning of the inner loop.

Syntax:

continue;

Flowchart of continue statement:

Flowchart-of-continue-statement

Example of continue statement:

#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i;
printf("Enter a number:");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
    if(1%2==0)
        continue;
        printf("Learn Simply: %d\n",i);
}
 
 
return 0;
}
 

Output:

Enter a number:5
Learn Simply: 1
Learn Simply: 2
Learn Simply: 3
Learn Simply: 4
Learn Simply: 5

break statement in C

We often come across situations where we want to jump out of a loop instantly, without waiting to get back to the condition test. The keyword break allows us to do so. When break encountered inside any loop, control automatically passes to the first statement after the loop.


Previous Lecture
:

Loops in C
Operators in C
Data Types in C
Conditional Statements if,if-else

If you have any problem in solving your questions, then join our Telegram group or search @Learnsimply, we will help you.

Please write comments if you find anything incorrect in the Jumping Statements lecture.

Leave a Comment

en English
X