Course Content
History of C Language
0/2
Basic Structure of C Program
0/1
Types of Errors
0/1
Language Fundamentals
0/1
Data Types and Modifiers
0/1
Programming with C Language
About Lesson

Jump Statements

These control statements are used to transfer the control of program execution from one place to another based on a specific condition.

The three Jump control statements of C Language are

  1. break
  2. continue
  3. goto

Break Statement

This is used inside any loop or switch…case block to skip the remaining statements and jump out of current block.  We have already learnt how to use break statement inside a switch…case block.

/*Program to calculate smallest divisor of a number */

#include

main( )

{

int div,num,i;

printf(“Enter any number:\n”);

scanf(“%d”,&num);

for (i=2;i<=num;++i)

{

if ((num % i) == 0)

{

printf(“Smallest divisor for number %d is %d”,num,i);

break;

}

}

}

Continue Statement

The ‘continue’ statement is used inside the loop allows to skip the following lines in the loop body for the current iteration and passes the control to the beginning of the loop to continue the execution with the next iteration.

/* Program to print first 20 natural numbers skipping the numbers divisible by 5 */

#include

main( )

{

int i;

for (i=1;i<=20;++i)

{

if ((i % 5) == 0)

continue;

printf(“%d ”,i);

}

}

goto Statment

The goto statement transfers the control of execution from the current point to some other portion of the program.

The syntax is as follows:

…….

…….

label:

…………

………….

if(cond)

goto label;

……………

………

In the above syntax, label is an identifier that is used to label the statement to which control will be transferred. The targeted statement must be preceded by the unique label followed by colon.

label : statement;

/* Program to print 10 even numbers */

#include

main()

{

int i=2;

while(1)

{

printf(“%d ”,i);

i=i+2;

if (i>=20)

goto outside;

}

outside : printf(“over”);

}

Loop with the combination of if and goto

We can also make a loop with the combination of if and goto, without using any of the loop statements we have discussed.

//c program to show how loop can be achieved with if and goto

#include

void main()

{

    int n,i=1;

    printf(“Enter any number:”);

    scanf(“%d”,&n);

    printf(“\n\nNatural Numbers upto your number:\n”);

    start:

        printf(“%d\n”,i);

        i++;

        if(i<=n)

            goto start;

    printf(“\nEnd of the program”);

}

 

You cannot copy content of this page