View Single Post
XINLEI
me too
Join Date: Jun 2011
Location: Colombian Coffee storage.
Old 02-21-2012 , 21:39   Re: for loop question
Reply With Quote #12

Quote:
Originally Posted by Dr. McKay View Post
But say you're writing a function that checks if a value is present in an array, and it returns true or false. You know how many times you want to loop, the size of the array. However, if the value is found, there's no point in continuing, so you can return.
Yes, i'm ok with that, and sometimes i do this. This is just to clarify concepts.

Quote:
For the record, he was saying that for this loop:

for(
int i = 0; i < max; i++)


Instead of returning within the loop, you should set i to max so it exits the loop. WHICH IS THE SAME EXACT THING AS USING break;
I've seen people with these habit. I think it's too paranoic making sure the loop completes itself like this, but there's no problem at all with it (perhaps in their decade it does matters when coding in BASIC or COBOL or another old language. Not sure).
XINLEI is offline