logo codesdope

Switch, Break and Continue in C


In the last topic, we studied different types of loops. We also saw how loops can be nested.

Normally, if we have to choose one case among many choices, nested if-else is used. But if the number of choices is large, switch..case makes it a bit easier and less complex.

Let's control Case Wise


Another way to control and decide is using switch...case statement. These are used when we are given a number of choices(cases) and we want to perform a different task for each choice.

Let's first have a look at its syntax.

switch(expression)
  {
    case constant1:
        statement(s);
        break;
    case constant2:
        statement(s);
        break;
    /* you can give any number of cases */
    default:
        statement(s);
  }

In switch...case, the value of expression enclosed within the brackets ( ) following switch is checked. If the value of the expression matches the value of constant in case, the statement(s) corresponding to that case will be executed.

If expression does not match any of the constant values, then the statements corresponding to default are executed.

Let's see an example.

#include <stdio.h>
int main()
{
    char grade ;
    printf ( "Enter your grade\n" )   ;
    scanf ( " %c" , &grade ) ;
    switch ( grade )
    {
        case   'A':
            printf ( "Excellent !\n" )   ;
            break ;
        case   'B':
           printf ( "Outstanding !\n" )   ;
           break ;
        case   'C':
           printf ( "Good !\n" )   ;
           break ;
        case   'D':
           printf ( "Can do better\n" )   ;
           break ;
        case   'E':
           printf ( "Just passed\n" )   ;
           break ;
        case   'F':
           printf ( "You failed\n" )   ;
           break ;
        default :
           printf ( "Invalid grade\n" )   ;
    }
    return 0;
}
Output
Enter your grade
D
Can do better !

break is used to break or terminate the loop whenever we want and is also used with switch.

In this example, the value of grade is D. Since the value of the constants of the first three cases is not 'D', so case 'D' will be executed and 'Can do better' will be printed. Then break statement will terminate the loop without checking the rest of the cases. This is how it works.
If there is no break in any statement, then after execution of the correct case, every case will also be executed. E.g.-

#include <stdio.h>
int main()
{
    char grade ;
    printf ( "Enter your grade\n" )   ;
    scanf ( " %c" , &grade ) ;
    switch ( grade )
    {
        case   'A':
            printf ( "Excellent !\n" )   ;

        case   'B':
            printf ( "Outstanding !\n" )   ;

        case   'C':
            printf ( "Good !\n" )   ;

        case   'D':
            printf ( "Can do better\n" )   ;

        case   'E':
            printf ( "Just passed\n" )   ;

        case   'F':
            printf ( "You failed\n" )   ;

        default :
            printf ( "Invalid grade\n" )   ;
    }
    return 0;
}
Output
Enter your grade
D
Can do better !
Just passed
You failed

In the above example, value of grade is 'D', so the control jumped to case 'D'. Since there is no break statement after any case, so all the statements after case 'D' will also be executed.

As you can see, all the cases after case D have been executed.

No need to write break in default case because the loop automatically terminates once default statements are executed.
If you want to execute only that case whose constant value equals the value of expression of the switch statement, then use the break statement.
Always enclose the character values within   '   '

Now let's see an example with the expression value as an integer.

#include <stdio.h>
int main()
{
    int i = 2 ;
    switch ( i )
    {
        case   1:
            printf ( "Number is 1\n" )   ;
            break ;
        case   2:
            printf ( "Number is 2\n" )   ;
            break ;
        default :
            printf ( "Number is greater than 2\n" )   ;
    }
    return 0;
}
Output
Number is 2

Let's break the loop when we want


Just type break; after the statement after which you want to break the loop. As simple as that !

Let's consider an example.

#include <stdio.h>
int main()
{
    int a ;
    for ( a = 1 ; a <= 10 ; a ++ )
    {
        printf ( "Hello World\n" ) ;
        if ( a == 2 )
        {

            //loop will now stop
            break ;

        }
    }
    return 0;
}
Output
Hello World
Hello World

In this example, after the execution of the first loop, a++ increases the value of 'a' to 2. Again 'Hello World' is printed and since the condition of if satisfies, so, break will be executed and the loop will terminates.

Continue


The continue statement works similar to break statement. The only difference is that break statement terminates the loop whereas continue statement passes control to the conditional test i.e. where the condition is checked.

In short, it passes control to the nearest do in do...while loop, or while statements when it is used in the while loop, or for in for loops, skipping the rest of the statements in the loop.

#include <stdio.h>
int main()
{
    int a ;
    for ( a = 1 ; a <= 10 ; a ++ )
    {
        printf ( "Hello World\n" ) ;
        if ( a == 2 )
        {

            //this time further statements will not be executed. Control will go to for
            continue ;
        }
        printf("a is not 2\n");
    }
    return 0;
}
Output
Hello World
a is not 2
Hello World
Hello World
a is not 2
Hello World
a is not 2
Hello World
a is not 2
Hello World
a is not 2
Hello World
a is not 2
Hello World
a is not 2
Hello World
a is not 2
Hello World
a is not 2

Notice that at the second time 'a is not 2' is not printed. It means that when 'a' was 2, then 'continue' got executed and control went to for loop without executing further codes.

Knowing is not enough, we must apply. Willing is not enough, We must do.
-Bruce Lee

Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.