Last active
September 1, 2023 15:41
-
-
Save matburt/6cb95c8b5c8979717aed4b57c11a9f3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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