Thread: save the day, help a newb.

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    save the day, help a newb.

    Hi folks, thanks for taking out the time to read this post. I NEED YOUR HELP!

    This question is directed towards the command in C++ known as the loop. My question is about the for loop and it's structure.

    Okay, so we all know that the "for" loop structure is this:

    Code:
    for (variable initialization; condition; variable update)
    However, I have a hard time understanding the "varial update". Somone please emphasize the structure of the "varial update" and how it works. I just don't get it.

    According to the tutorial for loops (http://www.cprogramming.com/tutorial/lesson3.html) the altnerative for "varial update" is "c++".

    I don't understand what "c++" is or what it does.

    Thanks for taking out the time to read this. Looking forward to your answers.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    c++ is shorthand for c = c + 1. so the following loop starts at 0 and counts up to, but not including 10.
    Code:
    for(int c = 0; c < 10; c++)
    The above could also be written like this
    Code:
    int c;
    for(c = 0; c < 10; c = c + 1)
    There are a couple differences between the two examples above, but for now you can just ignore them. The most important thing about c++ is that it just increments variable c by 1.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Look at this example:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	for(int i = 0; i<10; i++)
    	{
    		cout<<i<<" ";
    	}
    	
    	cout<<endl;
    	return 0;
    };
    
    Output:
    
    0 1 2 3 4 5 6 7 8 9
    The first thing that happens in that for-loop is that the variable i is created and assigned the value 0:
    Code:
    for(int i = 0; i<10; i++)
    That assignment only executes the first time through the for loop.

    Next, the conditional is tested to see whether the statements inside the body of the for-loop should be executed:
    Code:
    for(int i = 0; i<10; i++)
    If the conditional evaluates to true, then the statements inside the body of the for-loop are executed:
    Code:
    for(int i = 0; i<10; i++)
    {
    	cout<<i<<" ";
    }
    If the conditional evaluates to false, then the statements inside the for-loop are skipped, and execution continues with the next statement after the for loop:
    Code:
    for(int i = 0; i<10; i++)
    {
    		cout<<i<<" ";
    }
    	
    cout<<endl;
    The conditional is tested every time through the for-loop.

    After the statements inside the for loop execute, the third piece of code in the for-loop header is executed:
    Code:
    for(int i = 0; i<10; i++)
    That says to increment the variable 'i' by one. That executes at the end of every loop.

    So, the first time through the loop, i is set equal to 0. Then the conditional is evaluated: since 0 is less than 10, the statements inside the body of the for loop are executed. After all the statements inside the for-loop execute, i is incremented by 1, so i is then equal to 1. Thereafter, execution jumps up to the for-loop header again. The assignment where i is set equal to 0 is skipped(remember that only executes the first time through the loop), so the conditional is evaluated. Since 1 is less than 10, the statements inside the for loop execute again. At the end of the loop, i is incremented by 1 again, so i is then equal to 2. Once again execution jumps up to the for-loop header, and the conditional is evaluated. Since i is equal to 2 and 2 is less than 10, the statements inside the for loop execute again...etc., etc.

    See if you can guess the output of this code:
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
    	for(int i = 5; i<10; i=i+2)
    	{
    		cout<<i<<" ";
    	}
    
    	cout<<endl;
    
    	return 0;
    };
    Last edited by 7stud; 03-04-2006 at 01:28 AM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    2
    Thank you both for your feedback, your input did assit me greatly and I'm pretty confident I udnerstand the "for" loop and it's tructure.

    In reference to 7stud's request concerning the "for" loop:

    "i" is the declared variable which is given a value of 5.

    5 is less than 10 according to the condition which is evaluated. After the condition is evaluated and found to be true. The third command, "i=i+2" is initiated. 5 is substituted for "i" therefore 5+2 comes out to be 7.

    7 is less than 10 so the loop repeats; then again once more.

    Thanks man, I appreciate it.

    I guess the "++" threw me off, but now I understand that "++" is shorthand for add 1 to.

    You guys rock, thanks again.

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    i think 7stud's explaination could be used as a tutorial
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    In reference to 7stud's request concerning the "for" loop:

    "i" is the declared variable which is given a value of 5.

    5 is less than 10 according to the condition which is evaluated. After the condition is evaluated and found to be true. The third command, "i=i+2" is initiated. 5 is substituted for "i" therefore 5+2 comes out to be 7.
    That is incorrect. The output would be different if that were true.
    Last edited by 7stud; 03-04-2006 at 01:19 AM.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by subteranneous
    5 is less than 10 according to the condition which is evaluated. After the condition is evaluated and found to be true. The third command, "i=i+2" is initiated. 5 is substituted for "i" therefore 5+2 comes out to be 7.
    It's important to understand when each statement in the for loop is resolved. The first statement (the initialization) is done first, it's done once and only one. Then the condition is checked. If it returns true, then it goes through the looping statement. After it completes that, it goes to the top, does the update statement, then it checks the condition again. It's important to understand that the loop is done BEFORE the condition is updated.
    Code:
    for( int i = 0;// Variable initialization. This is done first.
         i < 10;   // Condition. This is done second.
         i++ ) {   // Variable Update. This is done last.
         // The Loop. This is done third.
         std::cout << i << '\n';  // The first time through, this will output 0.
         // End of loop.
         }
    Last edited by SlyMaelstrom; 03-04-2006 at 01:28 AM.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Quote Originally Posted by The Brain
    i think 7stud's explaination could be used as a tutorial
    Agreed, it was very informal and explained it quite well ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Should I learn a FULL language first?
    By Raeliean in forum Game Programming
    Replies: 8
    Last Post: 07-16-2005, 06:59 PM
  2. save webpage? some save some not
    By kryptkat in forum Tech Board
    Replies: 3
    Last Post: 06-07-2005, 09:21 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 11:50 PM
  5. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 03:50 PM