• Home
  • 2.6 case Labels

2.6 case Labels

View Categories

2.6 case Labels

< 1 min read

Table of Contents

A case label defines a value that can be matched against the expression of a switch statement. When a match is found, execution continues from that case label.

#include <stdio.h>

int main()
{
    int number = 2;

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

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

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

    return 0;
}

Example Output #

Two

Explanation #

The switch expression is:

switch (number)

Since number contains 2, it matches:

case 2:

Execution begins at that label and the associated statement is executed:

printf("Two\n");

The break statement terminates the switch.

A case label must have a constant expression as its value. Two case labels within the same switch cannot have the same value.

For example:

case 1:
case 2:
case 3:

defines three distinct case values. The behavior when multiple labels intentionally lead to the same statements is covered by fall-through.

Powered by BetterDocs

Leave a Reply

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