Home » Ask & Discuss » Board Exams - CBSE, ICSE, State Boards » Computer Science « Back to Discussion
Computer Science
Comments (8)
Hey.... a FOR loop is generally used when u can predetermine the number of times u wanna run the loop. And a WHILE loop is generally used when u wanna run the code until u satisfy a condition. U may note that wateva u can do in a FOR loop, can b done by a WHILE loop bt the reverse may not b true. U can also use the FOR loop to check boolean algebraic conditions as in for ( ; ans! = TRUE; ).....
hope this helps.....
and a special request to Mr Ankur Goel... try to have a control on ur tongue..... mind it.... If u cant be polite, then u r not welcome.... Doubts lyk this can b genuine, if u feel nethn strange abt this, try nt to answer than pukin ur damn "IRRITATION" on sum1 else...
For loops are used when you know how many times you need to loop. While loops are used to loop until an event occurs. Also, note that whatever you can do with a for loop, you can do it with a while loop (just add a variable that increments in the while loop and uses it to break out of the loop when the variable reaches a certain value). However, the reverse is not true (whatever you can do with a while do can not be done in a for loop).
-wish you good luck for the future
not more difference dear your can use in same sense really
only if you want to give increment condition in while loop in () it is not possiable as for loop
in while loop you can give only checking condition and you will set increment /decrement in { .. } block
while(condition)
{
i++;
}
but in for you can use as this type also
for(i=0; i<10; i++ )
{
}
i=0;
for(; i<10; i++ )
{
}
i=0;
for(; i<10;)
{
i++;
}
three types you can use for loop
there is a difference of convinience , while loop is little tricky and long as u have to initialise the variable before d while() and u have to write an extra statement for d icrement or decrement
but in for loop u can declare or ++ or -- in a singe for( ; ;) statement which saves time.















(do while) loop just requires the starting point or initializtion for a variable .there is no specification for ending in the start and thus checks the condition of termination after going thru processing of its body where it encounters the condition to terminate or to cntinue
whereas in for and while loop ,it checks all the conditions at the start and thus work accordingly the same .
yest there is no differ as such .only it depends on the comfortability of user