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, 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


switch...case is another way to control and decide the execution of statements other than if/else. This is 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 any of the case, the statement(s) corresponding to that case are 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 a 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 execution without checking the rest of the cases.

If there is no break in any statement, then after execution of the correct case, every case will also be executed. Look at the following code for an example.

#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' also get executed.

As you can see, all the cases after case D have been 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

Using break with loops


We can also terminate a loop in the middle of its execution using break. 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 first iteration of the loop, a++ increases the value of 'a' to 2 and 'Hello World' got printed. Since the condition of if satisfies this time, break will be executed and the loop will terminate.

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, skipping the rest of the statements of 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

Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.