C - Switch, Break and Continue Statement

Switch Statement The switch statement is also called the constant multiway conditional statement. The program encounter the situation to choose the option particular option from the many kind of statement. To solve this problem you can use if else statement also but that is bit complex then the switch statement. The general format of switch statement is given below.

Switch Statement

The switch statement is also called the constant multiway conditional statement. The program encounter the situation to choose the option particular option from the many kind of statement. To solve this problem you can use if else statement also but that is bit complex then the switch statement. The general format of switch statement is given below.

C Code

switch (expression){
 case value1;
  statement1;
  break;
 case value2;
  statement2
  break;
 case value3;
  statement3;
  break;
   .
   .
   .
 case value n;
  satementn;
  break;
 default:
  statement4;
  break;  
}

C Code

#include<stdio.h>
int main(void){
 int a;
 printf("Enter number");
 scanf("%d",&a);
 switch(a){
  case 1:
   printf("apple");
   break;
  case 2:
   printf("ball");
   break;
  case 3:
   printf("cat");
   break;
  default:
   printf("Invalid");
 }
 return 0;
}

Break Statement

A break statement is used to terminate the execution of the loop (while or do while or for) and the control is transferred to next statement or break. When break encountered in a loop, remaining part of the block is skipped and the loop is terminated passing control out of the loop.
FlowChart of Break Statement
FlowChart of Break Statement

C Code

for(i=0 ;i<5; i+)
{
    if(i==2)
              break;
    printf("%d\t",i);
}
In the above program 0 and 1 are printed. When value o i becomes 2, then the break statement is encountered and control passed out of the loop.

Continue Statement

Unlike break statement, the continue statement does not terminate a loop (while or do-while or for). It only interrupts a particular iteration. Whenever the continue statement is encountered, the remaining loop statement are skipped and the computation process directly goes to the next pass of the loop.
Continue Statement
Continue Statement

C Code

for(i=0; i<5; i++){
   if(i==2)
      continue;
   printf("%d\t",i);
}
This program displays numbers 0, 1, 3, and 4 but not 2. When the value of i becomes 2 the continue statement is encountered. The statement after continue is skipped and loop repetition continues.

You May Also Like...

Socialize with Us