C programming - switch...case

In C programming, the switch...case statement is used for decision-making purposes. It allows the program to execute different statements based on a variable's value. The basic syntax of the switch...case statement is as follows:

switch (variable)
{
    case value1:
        // execute these statements if variable equals value1
        break;
    case value2:
        // execute these statements if variable equals value2
        break;
    case value3:
        // execute these statements if variable equals value3
        break;
    default:
        // execute these statements if variable does not equal any of the above values
        break;
}
Source:‮igi.www‬ftidea.com

Here, the variable is the expression whose value is being tested against the different cases. The case statements define the different values that the variable may take. If the variable matches one of the case values, the statements within that case block are executed.

It's important to note that after executing the statements within a case block, the break statement is used to terminate the switch statement. If the break statement is not used, the execution will "fall through" to the next case block, executing its statements as well.

The default case is optional and defines the statements that should be executed if the variable does not match any of the case values.

Here's an example of using the switch...case statement:

int dayOfWeek = 3;

switch (dayOfWeek)
{
    case 1:
        printf("Monday");
        break;
    case 2:
        printf("Tuesday");
        break;
    case 3:
        printf("Wednesday");
        break;
    case 4:
        printf("Thursday");
        break;
    case 5:
        printf("Friday");
        break;
    default:
        printf("Invalid day of the week");
        break;
}

In this example, the dayOfWeek variable is being tested against different case values. Since dayOfWeek equals 3, the statements within the third case block are executed, printing "Wednesday" to the console.