Skip to content

Instantly share code, notes, and snippets.

@matburt
Last active September 1, 2023 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matburt/6cb95c8b5c8979717aed4b57c11a9f3f to your computer and use it in GitHub Desktop.
Save matburt/6cb95c8b5c8979717aed4b57c11a9f3f to your computer and use it in GitHub Desktop.
// For compilers that support ranges in case statements
// remember, if a case doesn't have a break then it will fall through to the next one.
switch (age) {
case 21:
printf("You are old enough to drink");
case 18 ... 20:
printf("You are old enough to vote");
case 16 ... 17:
printf("You are old enough to drive");
}
// For compilers that don't support range you need to convert the range to an integer
int age_range = 0;
if (age > 20) {
age_range = 1;
} else if (age > 18) {
age_range = 2;
} else if (age > 16) {
age_range = 3;
}
switch (age) {
case 1:
printf("You are old enough to drink);
case 2:
printf("You are old enough to vote");
case 3:
printf("You are old enough to drive");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment