Jan 16

The goto statement is used to alter the normal sequence of program instructions by transferring the control to some other portion of the program.

Syntax

goto label;

label : statement;

label is an identifier that is used to label the statement to which control will be transferred.

Example:

Program to print first 10 even numbers.

#include<stdio.h>

void main()

{

int i=2;

while(1)

{

printf(”%d  “,i);

i=i+2;

if(i>=20)

goto outside;

}

outside : printf(”over”);

}

Output:

2  4  6  8  10  12  14  16  18  20  over

Applications

  1. To branch around statements under certain conditions in place of use of if-else statement.
  2. To jump the end of the loop under certain conditions by passing the rest of the statements inside the loop in place of continue statement.
  3. To jump out of the loop avoiding the use of break statement.

Note

  • goto can never be used to jump into the loop from outside and it should be preferably used for forward jump.
  • The usage of the goto keyword should be avoided as it usually violates the normal flow of execution.
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Shweta

Jan 16

Continue statement used inside the loop help to bypass the section of loop  and passes the control to the beginning of the loop to continue the execution with the next loop iteration.

Syntax

continue;

Example:

#include<stdio.h>

void main()

{

int i;

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

{

if((i%5)==0)

continue;

printf(”%d “,i);

}

}

Output:

1  2  3  4  6  7  8  9   11  12  13  14  16  17 18 19

Here,the printf statement is bypassed each time when the value stored in i is divisible by 5.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Shweta

Sep 28

Sometimes,it is required to jump out of a loop irrespective of the conditional test value. Break statement is used inside any loop to allow the control jump to the immediate statement following the loop.

Syntax:

break;

When nested loops are used, then break jumps the control from the loop where  it has been used. Break statement can be used inside any loop i.e. while,do-while, for loop  and also in switch statement.

Example

program to calculate the first smallest divisior of a number

#include<stdio.h>

void main()

{

int div,num,i;

printf(”enter any no.\n”);

scanf(”%d”,&num);

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

{

if(num%i==0)

{

printf(”smallest divisior is %d”,i);

break;

}

}

}

Output

enter any no.

9

smallest divisior is 3

In above program,we divide the input number with the integer starting from 2 onwards, and print the smallest divisor as soon as remainder comes out to be zero. Since we are interested in first smallest divisor and not all divisor of a given number so jump out of for loop using break statement without further going for next iteration of for loop.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Shweta