What language do you use?
Normally you can use the if and switch statements, or the ternary condition ([condition] ? [trueCase] : [falseCase]). All are equally good. The difference is mostly a matter of taste.
In C, you could do something, like below. The base code is provided, where you can replace the highlighted region with one of the 3 scenarios listed after (if/switch/ternary condition).
#include <stdio.h>
int main()
{
int number;
printf("What's your lucky number (5/7/13)? ");
scanf("%d", &number);
[DECISION-MAKING LOGIC COMES HERE]
return 0;
}
The if statement
The decision-making logic with the if statement would look something, like this:
if (number == 5)
{
printf("5, huh? Like how many fingers you have on one hand.\n");
}
else if (number == 7)
{
printf("7? Dragons have 7 heads!\n");
}
else if (number == 13)
{
printf("13? Interesting, so you turn someone else's bad luck to your own good luck.\n");
}
else
{
printf("None of these? How original!\n");
}
The switch statement
The above code with the switch statement would look like this:
switch (number)
{
case 5:
printf("5, huh? Like how many fingers you have on one hand.\n");
break;
case 7:
printf("7? Dragons have 7 heads!\n");
break;
case 13:
printf("13? Interesting, so you turn someone else's bad luck to your own good luck.\n");
break;
default:
printf("None of these? How original!\n");
break;
}
It looks more neat than the if-else. But the compiler will turn it into an if-else in the end, so it's just really for us humans to make our code look better.
The ternary condition
The ternary condition looks like this: [condition] ? [true case] : [false case], where based on the evaluation of [condition] either the [true case] or the [false case] is executed. You can embed multiple ternary conditions like below.
printf(
number == 5
? "5, huh? Like how many fingers you have on one hand.\n"
: number == 7
? "7? Dragons have 7 heads!\n"
: number == 13
? "13? Interesting, so you turn someone else's bad luck to your own good luck.\n"
: "None of these? How original!\n");
The interesting thing about the ternary condition is that you can put the condition inside the printf()'s argument, while with the if and switch you need to repeat the printf() function all the time.
I hope this helps. Good luck!