Thread: OUTput Q

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    48

    OUTput Q

    I thought the answer for this prob was 5, but it's 5432... why is that?

    *9. What output does the following 'FOR' statement produce?

    for (i = 5, j = i - 1; j = i - 1; i > 0, j > 0; --i, j = i - 1)
    printf("%d", i);

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    that's an invalid for loop.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    why is it an invalid for a loop?

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    for( ; ; ) //valid - notice there are 2 semi-colons

    for( ; ; ; ) //invalid - 3 semi-colons

    that's why

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    4
    even though the loop is invalid considering its a loop it would print more then one result (since the arguments in it allow it to loop more then one)

    thats why the result will never be 5 only

    just out of curiosity , even if that code was right , whats the purpose of "j" in there , since it doesnt do anything

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I can't answer you.

    That loop won't even compile!!

  7. #7
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    a while loop with and statements should compile that. What is the point of j?

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    sorry, i mistyped the statement... it was:
    for (i = 5, j = i - 1; i > 0, j > 0; --i, j = i - 1)
    printf("%d", i);

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >I thought the answer for this prob was 5, but it's 5432... why is that?
    Because it's a loop, where i starts at 5 and is decremented each iteration. printf() is called within the loop, so it starts by outputing a 5, then a 4, and so on. It stops at 2 because of j being equal to zero when i becomes 1.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    wherethehellismypwd
    Guest
    That loop won't even compile!!
    It does under mingw. What compiler are you using?

    fyodor

  11. #11
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by wherethehellismypwd

    It does under mingw. What compiler are you using?

    fyodor
    a for statement with three semicolons will not compile under mingw.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM