7.6 – Jumping statements in C language

by subbu on October 7, 2013

break jumping statement in C language:

We have already used the break statement once in the switch selection statement. The purpose of using break was to jump out of switch-case selection statement after the execution of any single case.

The break is also used to jump out of any iteration statement (loop) instantly without going back to test the condition. Whenever break encounters with in a loop, control automatically jumps to the first statement after the loop. It is generally used with an if selection statement to come out of an infinite loop.

Now let us see the functionality of break in iteration statements.

#include<stdio.h>
int main()
{
int x=1;
while(1)    /*infinite loop*/
{
   if(x==5)
      break;
   printf("\t%d",x++);
}
return 0;
}

Output:
1        2        3        4

Actually here the loop is an infinite loop but, terminates when x is 5 and the break statement is executed.

#include<stdio.h>
int main()
{
int x=1;
for(;;)
{
   if(x>5)
      break;
   printf("\t%d",x++);
}
return 0;
}

Output:
1        2        3        4        5

Here, the for loop without a condition is an infinite loop, terminates only when the value of x is more than 5 and the break statement is executed.

continue jumping statement in C language:

It is a jumping statement only used with the iteration statements. It is used to jump to the conditional statement of any iteration statement by bypassing the remaining loop.
It can’t be used with switch-case selection statement.

#include<stdio.h>
int main()
{
int x;
for(x=1;x<=10;x++)
{
    if(x%2==1)  /*checking for odd number */
        continue;
    printf("\t%d",x);
}
return 0;
}

Output:
2        4        6        8        10

Here, the loop generates 1 to 10 natural numbers. continue directly sends the control to the increment/decrement statement whenever x is an odd number. printf() is executed only when x is an even number and continue statement is not executed.

Specification: Accept any 5 positive integers and print the sum of them.

#include<stdio.h>
int main()
{
int x,n,sum;
x=1;
sum=0;
printf("Enter the positive integers:\n");
while(x<=5)
{
  scanf("%d",&n);
  if(n<=0)         /*Not a positive integer*/
  {
     printf("Not a valid number..\n");
     continue;
  }
  sum=sum+n;
  x++;
}
printf("Sum of integers %d",sum);
return 0;
}

Execution:
Enter the positive integers:
12
34
0
Not a valid number..
89
55
-12
Not a valid number..
33
Sum of integers 223

Quiz:
1) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=5;
switch(x*2)
{
   case 15:
      printf("One");
      break;
   case 10:
      continue;
      printf("Two");
   default:
      printf("Three");
}
return 0;
}
Show Answer
Output:
Error: continue can’t be used within switch-case

2) What would be the output of following program?

#include<stdio.h>
int main()
{
int x;
for(x=1;x<=5;x++)
{
   switch(x)
   {
      case 1:
         printf("Coding");
         continue;
      case 2:
      case 3:
      case 4:
         continue;
      case 5:
         printf("fox");
    }
    printf(".com");
}
return 0;
}
Show Answer
Output:
Codingfox.com

Here the continue statement used in switch-case sends the control straight away to the increment/decrement section of the loop.

Previous post:

Next post: