Sunday, 2 March 2014

Difference between “for” loop and “while” loop?

Topic
For
While
Main margin:
Generally runs till a predetermined iteration.
Loops until a condition meets.

Boolean substitution:
Not possible in general sense.
One of the best feature of while is Boolean (true/false) checking condition. e.g:

while ( x--){}


Accessory
variables:
Is a must creating 1 new variable.
No needed most of the time.

Avatars:
for has 2 types.

1. Regular for loop
for( initialization;  condition; increments/decrements )
{    statements; 
}

2. Alternative notation in Java 5
for( element type name :  collection )
{    statements;
// loop will execute each element 
}

Has while and do-while.
Speed:
Probably for loop, may be because it looks out for 1 specific item through statement body.

while loop has to check conditions for each run until the condition satisfy(ies).




2 comments:

  1. dude, thanks for clearing it up. would be much more helpful if had examples on 'while' and 'do-while' avatars.

    ReplyDelete
    Replies
    1. thanks for the review. and I am connecting link for the further one down too :)

      Delete