For statement

Jun 27, 2013 at 2:10pm
closed account (EwCjE3v7)
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 Jun 27, 2013 at 3:35pm
Jun 27, 2013 at 2:19pm
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.
Jun 27, 2013 at 2:22pm
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.
Jun 27, 2013 at 3:05pm
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
Jun 27, 2013 at 3:35pm
closed account (EwCjE3v7)
Thank you all.you have been a great help
Topic archived. No new replies allowed.