Thread: Lesson 3: Loops

  1. #1
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62

    Question Lesson 3: Loops

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      for ( int x = 0; x < 10; x++ )
      { 
        cout << x << endl;
      }
      cin.get();
    }
    it says in the program:
    x is updated before the condition is checked.

    When I debug this code, I don't see any change when I
    use ++x or x++ ?
    It seems that whatever I use, when I enter the 'for statement',
    it increments like this:

    After 'for statement' x is: 0
    Second time x is: 1
    and so on...
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > x is updated before the condition is checked.
    Yes, that's wrong.
    The condition is evaluated first
    Then the body of the loop is executed, and if there is no break; statement then the x++ will happen at the end.

    for ( a ; b ; c ) d;
    is pretty much like this in the absence of break or continue statements.
    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.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    ... what's the question?

    In a standalone statement like that, there is no difference between x++ and ++x other than maybe some nanoseconds. By the time it resolves and the statement return goes nowhere, it's resulted in the same thing.
    Sent from my iPadŽ

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Salem
    Yes, that's wrong.
    It's not really wrong, just taken out of context. If anything, I'd say it was poorly worded.
    // Keep in mind that the loop condition checks
    // the conditional statement before it loops again.
    // consequently, when x equals 10 the loop breaks.
    // x is updated before the condition is checked.
    What this is saying is, x is updated before the condition is checked, again. Meaning to say, if you do a statement where the condition is x<10, then you'll never get a loop body where x is equal to 10, because the condition is checked before it loops into the body, again.

    I would have to say, though, a rewording is called for in that tutorial.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  2. newbie needs help with lesson 3: loops
    By InsaneLampshade in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2003, 03:33 PM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  4. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM