For statement

Hi there
i would like to know why is there a for statement its the same as while statements.,right...are there advantages and disadvantages?thanks
Last edited on
Generally speaking, a for loop is used when you know how many iterations you need.

Otherwise a while loop is used and is terminated when a condition is met.

That's not to say that there isn't instances where you could use either. And both can be broken out of prior to the condition that would naturally end them.
Usually i use a while statement when i don't know form start when the loop will end.

If the numbers of cycle are known, i prefer a for loop.
for statements also allow you to keep code less cluttered as well.

for ( int i = 0 ; i < length ; i++ ) {}

it does all the counting up there so you dont have to have it in the block :D
Thank you all.you have been a great help
Topic archived. No new replies allowed.