Thread: help with loops - super n00b

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    1

    help with loops - super n00b

    first post
    i am 17 and have always liked computers and have built my own pc etc...
    i am currently trying out programming while i decide if i should do a computer science degree... i want to be sure programming is 'for me' before i dive into a computer science which is mainly software engineering.
    I am following the tutorial on this site and am up to page 3.
    Cprogramming.com Tutorial: Loops

    here is what i am having problems with:

    Code:
    #include <iostream>
    
    using namespace std; // So the program can see cout and endl
    
    int main()
    {
      // The loop goes while x < 10, and x increases by one every loop
      for ( int x = 0; x < 10; x++ ) {
        // 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.    
        cout<< x <<endl;
      }
      cin.get();
    }

    i can get the code to work, and i am trying to get my head around this line:

    Code:
    for ( variable initialization; condition; variable update ) {
      Code to execute while the condition is true
    }
    so
    variable initialization is where you want to start counting (in this case 0 )
    Condition is what you want to count up to? ( in this case 10, not including 10)
    Then what is variable update? in this case it is x++ but what is this?

    if someone could please help by confirming my understanding, and helping me on the x++ bit?


    i apologise if this is a very basic question to be asking on such an advanced forum, bare with me

  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
    > Then what is variable update? in this case it is x++ but what is this?
    Well it's anything you like, so long as it makes progress from your initial state (x=0) to the final state (x >= 10).

    Without some kind of change to your conditional expression (say incrementing x), the loop would loop forever.
    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
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Quote Originally Posted by antipesto93
    variable initialization is where you want to start counting (in this case 0 )
    Condition is what you want to count up to? ( in this case 10, not including 10)
    Then what is variable update? in this case it is x++ but what is this?
    This is where we count.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. n00b Question- Loops
    By asmitzer in forum C Programming
    Replies: 3
    Last Post: 02-16-2010, 04:53 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. With super powers would you:
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 09-17-2003, 11:27 PM
  4. super n00b:(
    By Pheonix in forum C++ Programming
    Replies: 17
    Last Post: 07-24-2003, 04:35 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM