Thread: Quiz question

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Question Quiz question

    Here's one of the quiz questions from this site:

    "What is the final value of x when the code for(int x=0; x<10; x++) is run?"

    Why is the answer 10? It clearly says "x < 10" in the for loop. I'm pretty sure 10 isn't less than 10.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Here's a walkthrough.

    Code:
    x=0;
    x<10? yes
    add 1 to x
    x<10? yes(x=1)
    add 1 to x
    x<10? yes(x=2)
    ...
    add 1 to x
    x<10? yes(x=9)
    add 1 to x
    x<10? no(x=10)
    loop done
    Thus, at the end of the loop, x == 10.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    from 0 to 9 = 10.

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    because the loop doesnt stop until the midlle condition is no longer true. At 9, the condition is still true so it iterates and then increments x (so x is now 10), then checks the condition, sees that x is no longer less than 10, and exits the loop. Get it?

  5. #5
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Talking

    Oh...um...ok

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Quiz on C
    By ganesh bala in forum C Programming
    Replies: 18
    Last Post: 02-10-2009, 03:49 PM
  3. Memory quiz, question 2
    By pheres in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2008, 05:46 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM