While, For loops

This is a discussion on While, For loops within the C++ Programming forums, part of the General Programming Boards category; Which one is better to use a while loop or a for loop?...

  1. #1
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227

    While, For loops

    Which one is better to use a while loop or a for loop?
    Keyboard Not Found! Press any key to continue. . .

  2. #2
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    That completely depends on what you need the loop for, in any case, each will have it's advantages and disadvantages.

    A while loop is good for cases where you don't exactly know when it is you'll want to break out of the loop. Say I'm reading the contents of a file, and I don't know how many lines there are, I would use a while loop that reads in line by line until the end of the file is reached. Once that happens, the condition in my while loop would fire and break me out.

    For loops are good for initializing arrays:
    I.e.
    Code:
    int main(void)
    {
    	int MyArray[10000]; //It'll be a pain to set each value to 0
    
    	for (int x = 0; x < 10000; x++)
    	{
    		MyArray[x] = 0;
    	}
    return 0; //Added this in after, otherwise it would be indented
    }
    Note though, that this can also be accomplished with a while loop:
    Code:
    int main(void)
    {
    	int MyArray[10000]; //It'll be a pain to set each value to 0
    	int x = 0;
    
    	while(x < 10000)
    	{
    		MyArray[x] = 0;
    		x++;
    	}
    	return 0;
    }
    A for loop just puts it all together into a nice neat package. For are good for running through a loop when you know a certain amount of times you want to repeat code.

    Any time you could use a for loop, you can use a while loop, and vice versa (I stand by this, unless there are any challenges?) It's just that at times, one is more efficient or logical to use, but it completely depends on the situation you need it for.

    Note: These are just a few examples, initializing Arrays is not the only use for for loops, and reading files is not the only use for while loops. It's best to look at the situation you're in, and make a judgement call based on what you think you'll need.
    Last edited by Epo; 10-11-2004 at 10:23 PM. Reason: Added Note

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    worth a quick look

    http://www.cprogramming.com/tutorial/lesson3.html

    The loop you use, as said above, is entirely dependant on where, when, and why it is used. The loop that requires the least code/variables/etc while still remaining clear is ideal in any situation.

  4. #4
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Quote Originally Posted by RoD
    The loop that requires the least code/variables/etc while still remaining clear is ideal in any situation.
    Nicely said.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21