Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How can i make a print that can say one thing or the other?

0 votes
asked Sep 14, 2023 by Vincent Snowburg (120 points)
Im trying to make a thing where it can say multiple diffrent things but i cant figure out how i even tried just searching it up how can i make this work?

1 Answer

0 votes
answered Sep 15, 2023 by Peter Minarik (86,240 points)

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!

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...