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).