C switch statement

The switch statement in C programming is used to execute different branches of code based on the value of a single expression. The syntax for the switch statement is as follows:

1switch (expression) { 2 case value1: 3 // statements to be executed if expression == value1 4 break; 5 case value2: 6 // statements to be executed if expression == value2 7 break; 8 ... 9 default: 10 // statements to be executed if expression does not match any of the cases 11 break; 12}

In the above syntax, expression is evaluated, and its value is compared to the values specified in each case clause. If a match is found, the statements within that case clause will be executed. The break statement is used to break out of the switch statement once a matching case has been found. If none of the cases match, the default case is executed. The default case is optional, and if omitted, the program will simply move on to the next section of code if none of the cases match.

1#include <stdio.h> 2 3int main(void) { 4 int num; 5 printf("Enter a number (1-5): "); 6 scanf("%d", &num); 7 switch (num) { 8 case 1: 9 printf("One\n"); 10 break; 11 case 2: 12 printf("Two\n"); 13 break; 14 case 3: 15 printf("Three\n"); 16 break; 17 case 4: 18 printf("Four\n"); 19 break; 20 case 5: 21 printf("Five\n"); 22 break; 23 } 24 return 0; 25}

In this example, the user is prompted to enter a number. The value of the num variable is then used in a switch statement to print the corresponding number name.

Using break to exit a switch statement

The break statement is a control flow statement used in C programming. It is used to terminate the execution of a loop or a switch statement. The break statement is particularly useful when you want to exit a loop early, before the loop has completed its full iteration.

In a switch statement, the break statement is used to exit the switch block once a case has been executed. If a break statement is not used after a case, the program will continue executing the code in the next case, which may not be intended.

Syntax for the break statement in a switch:

1break;

Using default in switch statement

The default statement is a control flow statement used in C programming. It is used in a switch statement and acts as a catch-all for cases that are not explicitly defined in the switch block. If no case matches the value of the switch expression, the code in the default block is executed.

The default statement is optional in a switch statement, but it is a good practice to include one in case the value of the switch expression does not match any of the cases. It provides a way to handle unexpected or invalid input in a graceful manner.

Syntax for the default statement in a switch:

1default: 2 // statements to be executed 3 break;
1#include <stdio.h> 2 3int main(void) { 4 int num; 5 printf("Enter a number (1-5): "); 6 scanf("%d", &num); 7 switch (num) { 8 case 1: 9 printf("One\n"); 10 break; 11 case 2: 12 printf("Two\n"); 13 break; 14 case 3: 15 printf("Three\n"); 16 break; 17 case 4: 18 printf("Four\n"); 19 break; 20 case 5: 21 printf("Five\n"); 22 break; 23 default: 24 printf("Invalid number.\n"); 25 break; 26 } 27 return 0; 28}

In this example, the default case is used to handle an invalid number that does not match any of the cases. If the user enters a number that is not within the range of 1 to 5, the message "Invalid number" is printed, and the program exits the switch statement. The default case provides a catch-all for unexpected input and can be used to handle such cases in a graceful manner.