Thread: how does this for statement work??

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    how does this for statement work??

    this is only part of a code. When the for loop executes the first time, it asks
    Do you want to enter the detials of a horse

    But when it executes a 2nd and 3rd time etc it says
    Do you want to enter the details of ANOTHER horse?

    Code:
    for (hcount=0;hcount<50;hcount++)
    		{
    			printf("\nDo you want to enter details of a%s horse (Y or N)?",hcount?"nother ":"");
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The expression
    Code:
    hcount? "nother" : ""
    gives the result "nother" if hcount is non-zero, and "" if hcount is zero.

    The %s in the format string tells printf() to output the string (since that is the first argument following the format string).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's from Ivor Horton's Book!

    This part
    Code:
    hcount?"nother ":""
    at the end of the printf() line of code, is an if statement. If horsecount (hcount) is TRUE (non-zero), then %s which is after the a in the print string, becomes "nother", otherwise, nother is set to "" (the empty string).

    So you see a"nother", (but without the quotation marks), after the first horse details, are entered.

    He does that a few different ways in the book.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by Adak View Post
    That's from Ivor Horton's Book!

    This part
    Code:
    hcount?"nother ":""
    at the end of the printf() line of code, is an if statement. If horsecount (hcount) is TRUE (non-zero), then %s which is after the a in the print string, becomes "nother", otherwise, nother is set to "" (the empty string).

    So you see a"nother", (but without the quotation marks), after the first horse details, are entered.

    He does that a few different ways in the book.
    ok thanks for the replies! LOL yeah its from his book. I borrowed it due to someones recommendation from this website.. maybe it was you

    very good book might i add!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM