• Home
  • 2.8 Fall-Through in switch

2.8 Fall-Through in switch

View Categories

2.8 Fall-Through in switch

< 1 min read

Table of Contents

When a case block does not end with break, execution continues into the statements of the next case label. This behavior is called fall-through.

#include <stdio.h>

int main()
{
    int number = 1;

    switch (number)
    {
        case 1:
            printf("One\n");

        case 2:
            printf("Two\n");

        case 3:
            printf("Three\n");
            break;

        default:
            printf("Other number\n");
    }

    return 0;
}

Example Output #

One
Two
Three

Explanation #

number contains 1, so execution begins at:

case 1:

After printing One, there is no break statement. Execution therefore continues into case 2:

case 2:
    printf("Two\n");

There is still no break, so execution continues into case 3 and prints Three.

The break in case 3 terminates the switch.

A case label does not automatically stop execution after its statements. break is required when execution should leave the switch after that case.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *