x

C – Case control statements

Prev     Next

The statements which are used to execute only specific block of statements in a series of blocks are called case control statements.


There are 4 types of case control statements in C language. They are,

  1. switch
  2. break
  3. continue
  4. goto

1. switch case statement in C:

  • Switch case statements are used to execute only specific case statements based on the switch expression.
  • Below is the syntax for switch case statement.

switch (expression)
{
     case label1:   statements;
                           break;
     case label2:   statements;
                           break;
     case label3:   statements;
                           break;
     default:      statements;
                           break;
}


Example program for switch..case statement in C:

Output:

Value is 3

2. break statement in C:

  • Break statement is used to terminate the while loops, switch case loops and for loops from the subsequent execution.
  • Syntax: break;

Example program for break statement in C:

Output:

0 1 2 3 4
Coming out of for loop when i = 5

3. Continue statement in C:

  • Continue statement is used to continue the next iteration of for loop, while loop and do-while loops.  So, the remaining statements are skipped within the loop for that particular iteration.
  • Syntax : continue;

Example program for continue statement in C:

Output:

0 1 2 3 4
Skipping 5 from display using continue statement
Skipping 6 from display using continue statement
7 8 9

4. goto statement in C:

  • goto statements is used to transfer the normal flow of a program to the specified label in the program.
  • Below is the syntax for goto statement in C.

{
         …….
         go to label;
         …….
         …….
         LABEL:
         statements;
}

Example program for goto statement in C:

 

Output:

0 1 2 3 4
We are using goto statement when i = 5
Now, we are inside label name “hai”

Prev     Next



Like it? Please Spread the word!