Case control statements in C Language


     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 Language:
    • 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;
         default:          statements;
                                break;
}

Example program for switch..case statement in C Language:

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 Language:

    • 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 Language:

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 Language:

    • 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 Language:

 Output:

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


www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

No comments: