Thread: Ternary inside If

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    7

    Ternary inside If

    Hi there,
    I hope it's okay I keep asking newbie's quesitons... I really appreciate the great answers and it really helps me understand better.

    I'm trying to mix a Ternary inside an If statement and I can't understand what's the problem in my synthax as the result is somehow distorted...

    I know it's easier with instead of the Ternary, just to use If Else but I'm trying to play with to learn...
    insert
    Code:
    #include<stdio.h>
    
    int main()
    {
    
        int num;
        int i;
        int elementsNum;
        int even;
    
    
            printf("How many numbers would you like to test?\n");
            scanf(" %d", &elementsNum);
    
            for(i=1 ; i <= elementsNum ; ++i ){
                    printf(" %d is %s even number\n", i , (i % 2 == 0) ? ("an even number") : ("an odd number") );
                }
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The code that you posted does not appear to have an if statement.

    As for what you did post, you might find it prints gibberish like "2 is an even number even number" because you didn't write the format string to match up.
    Last edited by laserlight; 10-13-2020 at 10:35 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    7
    You're right, sorry, I meant Ternary instead of If statement... I was trying to avoid the If by putting this Ternary inside the printf...
    What you wrote is indeed the gibberish that it gives me... What do you mean by not writing format strings to match up?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, let's play a "fill in the blanks" game.

    Here's the sentence with the blanks:
    ____ is _____ even number

    The two phrases that you can use to fill in the blanks are: 2 and an even number.

    So you fill in the blanks:
    2 is an even number even number

    Can't you see that the problem is that the game is set out such that this is the result? If you want a different result, you need to change either the sentence with the blanks, or the phrases to fill in the blanks (or both).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2020
    Posts
    7
    Yes, I now certainly see it and understand it... Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ternary operator
    By johngav77 in forum C Programming
    Replies: 1
    Last Post: 10-22-2018, 03:28 AM
  2. Ternary operation
    By So Friend in forum C Programming
    Replies: 13
    Last Post: 09-15-2012, 03:27 PM
  3. bug in ternary operator ?:
    By wiglaf in forum C Programming
    Replies: 14
    Last Post: 03-31-2011, 10:26 PM
  4. Ternary and commas
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 01-04-2009, 11:28 PM
  5. ternary operator
    By abhijith gopal in forum C Programming
    Replies: 37
    Last Post: 07-10-2006, 11:58 AM

Tags for this Thread