Switch, Break
Using the switch Statement
Imagine you're buying a soda from a vending machine. After you select your desired drink, deep within the vending machine's computer, the user_input variable takes on one of several predefined values. Each of these options is termed a case, and this is where the switch statement comes into play.
Think of the switch statement as another version of the if-else statement. It acts in response to specific values you've previously defined.
Here's how the structure of a switch statement looks:
main.c
1234567891011121314151617switch (integer_expression) { case first_case_value: // Instruction for this case break; case second_case_value: // Instruction for this case break; case third_case_value: // Instruction for this case break; default: // Default instruction }
Consider a vending machine example. Suppose there are three buttons to select different types of chips:
- Cheese-flavored chips;
- Bacon-flavored chips;
- Chili-flavored chips.
Main.c
123456789101112131415161718192021222324#include <stdio.h> int main() { int userInput = 2; switch (userInput) { case 1: printf("You selected cheese-flavored chips.\n"); break; case 2: printf("You selected bacon-flavored chips.\n"); break; case 3: printf("You selected chili-flavored chips.\n"); break; default: printf("You selected another item.\n"); } return 0; }
If the tested expression doesn't match any of the listed cases, the default case is executed. If there's no default case provided, the program simply continues its flow.
The tested expression in a switch statement can only be of integer or char type. You can't use strings or non-integer data types as cases.
Main.c
12345678910111213141516171819202122232425#include <stdio.h> int main() { char userInput = 'y'; switch (userInput) { case 'a': printf("You entered 'a' character\n"); break; case 'b': printf("You entered 'b' character\n"); break; case 'c': printf("You entered 'c' character\n"); break; default: printf("You entered unknown character\n"); } return 0; }
The Role of break
The break command stops the current block's execution and moves on to the next segment of code. Essentially, once the relevant case is complete, you exit that block and continue with your program.
Without the break command, the switch statement would run continuously, and you'd likely end up with unintended results.
Main.c
123456789101112131415161718192021222324#include <stdio.h> int main() { char userInput = 'b'; switch (userInput) { case 'a': printf("You entered 'a' character\n"); // without break case 'b': printf("You entered 'b' character\n"); // without break case 'c': printf("You entered 'c' character\n"); // without break default: printf("You entered unknown character\n"); } return 0; }
In the absence of the break command, the program starts executing immediately after finding a matching case and continues until it finds a break or reaches the end of the switch.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Can you show me an example of a switch statement in code?
What happens if I forget to include a break statement in one of the cases?
Can you explain how the default case works in more detail?
Awesome!
Completion rate improved to 2.63
Switch, Break
Pyyhkäise näyttääksesi valikon
Using the switch Statement
Imagine you're buying a soda from a vending machine. After you select your desired drink, deep within the vending machine's computer, the user_input variable takes on one of several predefined values. Each of these options is termed a case, and this is where the switch statement comes into play.
Think of the switch statement as another version of the if-else statement. It acts in response to specific values you've previously defined.
Here's how the structure of a switch statement looks:
main.c
1234567891011121314151617switch (integer_expression) { case first_case_value: // Instruction for this case break; case second_case_value: // Instruction for this case break; case third_case_value: // Instruction for this case break; default: // Default instruction }
Consider a vending machine example. Suppose there are three buttons to select different types of chips:
- Cheese-flavored chips;
- Bacon-flavored chips;
- Chili-flavored chips.
Main.c
123456789101112131415161718192021222324#include <stdio.h> int main() { int userInput = 2; switch (userInput) { case 1: printf("You selected cheese-flavored chips.\n"); break; case 2: printf("You selected bacon-flavored chips.\n"); break; case 3: printf("You selected chili-flavored chips.\n"); break; default: printf("You selected another item.\n"); } return 0; }
If the tested expression doesn't match any of the listed cases, the default case is executed. If there's no default case provided, the program simply continues its flow.
The tested expression in a switch statement can only be of integer or char type. You can't use strings or non-integer data types as cases.
Main.c
12345678910111213141516171819202122232425#include <stdio.h> int main() { char userInput = 'y'; switch (userInput) { case 'a': printf("You entered 'a' character\n"); break; case 'b': printf("You entered 'b' character\n"); break; case 'c': printf("You entered 'c' character\n"); break; default: printf("You entered unknown character\n"); } return 0; }
The Role of break
The break command stops the current block's execution and moves on to the next segment of code. Essentially, once the relevant case is complete, you exit that block and continue with your program.
Without the break command, the switch statement would run continuously, and you'd likely end up with unintended results.
Main.c
123456789101112131415161718192021222324#include <stdio.h> int main() { char userInput = 'b'; switch (userInput) { case 'a': printf("You entered 'a' character\n"); // without break case 'b': printf("You entered 'b' character\n"); // without break case 'c': printf("You entered 'c' character\n"); // without break default: printf("You entered unknown character\n"); } return 0; }
In the absence of the break command, the program starts executing immediately after finding a matching case and continues until it finds a break or reaches the end of the switch.
Kiitos palautteestasi!