Thread: C++ Programming Quiz - Loops

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    Question C++ Programming Quiz - Loops

    Hello.

    I just started learning C++ today from your wonderful tutorials on the website, thank you very much for those by the way. I would like to get this sorted because I'm finding it fun to learn here and it's easier that way. I've had some bad experiences with a book previously and I don't want to go through that again until I learned at least the basics. I hope this is the right section of the forum to post my question.

    Thing is, I ran into a tiny problem at the Loops Quiz, the very first question.


    1. What is the final value of x when the following code is run?

    A. 10
    B. 9
    C. 0
    D. 1
    Code:
    int x;
    for (x=0; x<10; x++)
    {}
    I answered 9, but the correct one is 10.

    Did I misunderstand the question? The final value for which the loop will occur is clearly 9, I also added a cout << x to the code above just to be sure and it showed me the same thing. For x=10, the loop will not happen because the condition is checked at the beginning, so why is the final value 10?

    I'm sorry I realize this is a very beginner question but I don't want to mess anything up now and find out that it adds up later on to a larger list of misunderstandings that I'll have to sort out then.

    Take your time with the reply and thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But the x++ happens at the end of the loop.

    x always ends up being the first value which FAILS the test, not the last value to PASS the test.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    So, the correct order is value -> check condition -> execute block -> increment -> repeat?

    That's how I understand it, x=9 goes through the condition and becomes 10 at the end of the block. It also explains why my cout << x in the block would be useless, since it won't let 10 back in the loop, let alone print it.
    Last edited by Sirion; 07-21-2010 at 01:12 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    print x inside the loop, and print x outside the loop.

    Apart from some issues with break and continue, a for loop is basically a while loop.

    Code:
    for ( a ; b ; c ) { 
      d; 
    }
    can be written as
    Code:
    a;
    while ( b ) {
      { d; }
      c;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    Oh yeah. Should have seen that one. Guess I better learn these without distractions, thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. Hi, Quiz C program
    By Eman in forum C Programming
    Replies: 0
    Last Post: 11-11-2009, 04:12 PM
  3. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM