Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Switch Statement | Introduction to Conditional Statements
C Conditional Statements

bookChallenge: Switch Statement

In C programming, the switch statement provides a clear and efficient way to handle multi-way branching, especially when you need to execute different code blocks based on the value of a single variable.

switch_syntax.c

switch_syntax.c

copy
123456789101112
// Syntax of a switch statement in C switch (expression) { case first_value: // Statements for `first_value` break; case second_value: // Statements for `second_value` break; // More cases as needed default: // Default statements if no case matches }

Unlike a series of if...else statements, which can become lengthy and harder to read as the number of conditions grows, a switch statement organizes multiple possible execution paths in a tidy structure.

print_day_of_week_again.c

print_day_of_week_again.c

copy
1234567891011121314151617181920212223242526272829303132333435
#include <stdio.h> void print_day_of_week(int day) { switch (day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 7: printf("Sunday\n"); break; default: printf("Invalid day\n"); } } int main() { print_day_of_week(5); // Should print "Friday" print_day_of_week(9); // Should print "Invalid day" return 0; }

The switch expression is evaluated once, and its result is compared with each case label. If a match is found, the code under that case executes until a break statement is encountered or the switch ends. If no cases match, the default block (if present) runs.

Oppgave

Swipe to start coding

Write a function named getDayType that takes an integer parameter day and returns a character indicating the type of day.

  • Use a switch statement to determine the return value based on the value of day.
  • Return 'W' if day is 1, 2, 3, 4, or 5 (representing a weekday);
  • Return 'H' if day is 6 or 7 (representing a holiday);
  • Return '?' for any other value.

Løsning

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 5
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you show me an example of a switch statement in C?

What happens if I forget to include a break statement in a case?

When should I use a switch statement instead of if...else?

close

bookChallenge: Switch Statement

Sveip for å vise menyen

In C programming, the switch statement provides a clear and efficient way to handle multi-way branching, especially when you need to execute different code blocks based on the value of a single variable.

switch_syntax.c

switch_syntax.c

copy
123456789101112
// Syntax of a switch statement in C switch (expression) { case first_value: // Statements for `first_value` break; case second_value: // Statements for `second_value` break; // More cases as needed default: // Default statements if no case matches }

Unlike a series of if...else statements, which can become lengthy and harder to read as the number of conditions grows, a switch statement organizes multiple possible execution paths in a tidy structure.

print_day_of_week_again.c

print_day_of_week_again.c

copy
1234567891011121314151617181920212223242526272829303132333435
#include <stdio.h> void print_day_of_week(int day) { switch (day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 7: printf("Sunday\n"); break; default: printf("Invalid day\n"); } } int main() { print_day_of_week(5); // Should print "Friday" print_day_of_week(9); // Should print "Invalid day" return 0; }

The switch expression is evaluated once, and its result is compared with each case label. If a match is found, the code under that case executes until a break statement is encountered or the switch ends. If no cases match, the default block (if present) runs.

Oppgave

Swipe to start coding

Write a function named getDayType that takes an integer parameter day and returns a character indicating the type of day.

  • Use a switch statement to determine the return value based on the value of day.
  • Return 'W' if day is 1, 2, 3, 4, or 5 (representing a weekday);
  • Return 'H' if day is 6 or 7 (representing a holiday);
  • Return '?' for any other value.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 5
single

single

some-alt