C Switch
Use switch for discrete command codes and make fall-through an explicit decision.
How it works
A switch compares an integer controlling value with constant case labels. It is useful for named commands or enum states, rather than arbitrary numeric ranges. Execution starts at the matching label and continues until a break, return or the end of the statement. A default handles values not listed in a case. Deciding how to reject an unknown command is part of the interface, even when a classroom example starts with a fixed valid value.
Run it locally
Use a hosted C17 compiler in a separate lab folder. The command assumes a driver named cc; substitute gcc, clang or zig cc as appropriate. C does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed for these C programs.
cc -std=c17 -Wall -Wextra -Wpedantic -Werror switch-states.c -o lessonOn Windows, use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Math-library linking differs by toolchain; GCC on many Unix systems needs -lm after the source.
Download the complete C source. File-writing labs must run in an empty private directory, away from existing reports.
Example
#include <stdio.h>
int main(void) {
char command = 'r';
switch (command) {
case 'r':
puts("Record a reading");
break;
case 'q':
puts("Finish the survey");
break;
default:
puts("Unknown command");
break;
}
return 0;
}
Expected result
Record a reading
The character value selects the record action. break exits the switch, so the quit message is not printed afterwards. Several adjacent labels may intentionally share one action, but separate actions need an intentional exit. Later a parser will convert outside text into a validated command before dispatching it.
For language and library rules, consult the WG14 C working group and its public C11 committee draft, which describes the core facilities used here; C17 incorporates corrections. Check your compiler and runtime documentation for implementation details.