Thread: Newbie Question, Need clarification on for loop

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

    Newbie Question, Need clarification on for loop

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    for(int x = 0; x < 10; x++)
    cout << x <<endl;
    cin.get();
    return 0;
    }

    BTW, I am using the tutorial Cprogramming.com Tutorial: Loops

    I understand how for loops work. (mostly) I know this prints x while x<10(in this case). Which I have tested and worked fine.

    As I was writing this I answered my own question I believe, just want to make sure.


    For example this is what I see:
    Code:
    for(int x = 0; x < 10; x++)
    Is this the same as?
    Code:
    int x = 0;
    for( x <10; x++)
    The reason I ask this, originally in the first example my mind tells me that it will set x = 0 everytime it gets evaluated. But as I was writing this I realized x is just a variable that is assigned (0) so the compiler skips this part of the for statement correct?



    I know there are better ways to write this piece of code I am just trying to grasp everything in the tutorial and practice it.

  2. #2
    Registered User
    Join Date
    Oct 2010
    Location
    Louisville Ky
    Posts
    6
    A for loop does require 3 variables, so that is not quite the same, but your basic understanding is correct.

    It would be the equivalent of saying something like:

    Code:
    int x = 0;
    
    while (x <= 10)
    {
        cout << x << endl;
        x++;
    }

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by mlabcdzs View Post
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    for(int x = 0; x < 10; x++)
    cout << x <<endl;
    cin.get();
    return 0;
    }

    BTW, I am using the tutorial Cprogramming.com Tutorial: Loops

    I understand how for loops work. (mostly) I know this prints x while x<10(in this case). Which I have tested and worked fine.

    As I was writing this I answered my own question I believe, just want to make sure.


    For example this is what I see:
    Code:
    for(int x = 0; x < 10; x++)
    Is this the same as?
    Code:
    int x = 0;
    for( x <10; x++)
    The reason I ask this, originally in the first example my mind tells me that it will set x = 0 everytime it gets evaluated. But as I was writing this I realized x is just a variable that is assigned (0) so the compiler skips this part of the for statement correct?



    I know there are better ways to write this piece of code I am just trying to grasp everything in the tutorial and practice it.
    It is the same, but you must keep the structure of the for.
    Thus, you must include a ";" to signal that initialization is done outside the for.

    Code:
    for( ; x < 10 ; x++);
    Only the condition statement must ALWAYS be present in a for loop. You can even remove the iteration step and move it somewhere else:

    Code:
    for(; x < 10 ; ){
     x++;
    }
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    [QUOTE=mlabcdzs;977292]
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    for(int x = 0; x < 10; x++)
    cout << x <<endl;
    cin.get();
    return 0;
    }

    BTW, I am using the tutorial Cprogramming.com Tutorial: Loops

    I understand how for loops work. (mostly) I know this prints x while x<10(in this case). Which I have tested and worked fine.

    As I was writing this I answered my own question I believe, just want to make sure.


    For example this is what I see:
    Code:
    for(int x = 0; x < 10; x++)
    Is this the same as?
    Code:
    int x = 0;
    for( x <10; x++)
    [\QUOTE]
    Yep, you are right. Except you forgot to put the semicolon before x<10 like so:
    Code:
    for(;x<10;x++)
    The reason I ask this, originally in the first example my mind tells me that it will set x = 0 everytime it gets evaluated. But as I was writing this I realized x is just a variable that is assigned (0) so the compiler skips this part of the for statement correct?



    I know there are better ways to write this piece of code I am just trying to grasp everything in the tutorial and practice it.
    you are partly right.

    when you declare the statement
    Code:
    for(i=0;i<10;i++)
    The compiler initialises i to 0. After that, the compiler would skip the assignment part and only execute the 2nd and first statement. The conditional part, and the incremental part.

    so that is why in some compiler if you do this.
    Code:
    int i =0 ; 
    
    //or 
    
    int i ;
    i =0 ;
    
     // in for loop
    for(i=0;i<10;i++){}
    Some compilers will give you a warning that you have initialized a variable twice or the value of one will not be used..something like that anyways. Can't remember.

    Although you can use a for loop. I find a while loop so much easier and cleaner.
    I dunno, it depends.
    this cool no? :
    Code:
    #include <iostream> 
    using namespace std ;
    void main()
    
    {
        int i=0; 
        
        while(i++<10) 
        {
            cout << i <<endl ; 
        }
        cout << endl ;
        system("pause") ; 
        
    }
    just preference i guess . Don't worry about it.
    Loops will be an automatic process before you know it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  2. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  3. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  4. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM