Switch Keyword


In C programming, the 'switch' keyword is used to create a decision-making structure called a switch statement.

The switch statement allows you to select one of many code blocks to be executed based on the value of an expression or variable.

Here's an example of using the 'switch' keyword to create a switch statement:


int day = 3;

switch (day) {
  case 1:
    printf("Monday");
    break;
  case 2:
    printf("Tuesday");
    break;
  case 3:
    printf("Wednesday");
    break;
  default:
    printf("Invalid day");
    break;
}

    

In this example, the 'switch' keyword is used to create a switch statement based on the value of the 'day' variable.

Depending on the value of 'day', one of the corresponding case blocks will be executed.

If none of the cases match, the code in the 'default' block will be executed. The 'break' keyword is used to exit the switch statement after each case.

The 'switch' statement provides a concise way to handle multiple cases and perform different actions based on the value of a variable or expression.

It is commonly used when you have a set of options or conditions to evaluate.

Loading...

Search