The difference between For and While Loop ?

What's diffirence between  For and While Loop ?

  1. One main difference is while loops are best suited when you do not know ahead of the number of iterations that you need to do.
  2. When you know this before entering the loop you can go for for loop.

For can behave like while:
while(true)
{
}

for(;;)
{
}
And while can behave like for:
int x = 0;
while(x < 10)
{
    x++;
}

for(x = 0; x < 10; x++)
{
}
 
So, most of sorting algorithms using divide-and-conquer strategy are using WHILE loop because it cannot know how many time the array be divided -  it depends on the array's size. (Mergesort, Quicksort...)

Pay attention:
- For loop can be infinite (ie, for(;;))
- While loop could be faster than For loop.
 According to the slide at: https://sites.google.com/a/googlesciencefair.com/science-fair-2012-project-96b21c243a17ca64bdad77508f297eca9531a766-1333147438-57/home
-
Share on Google Plus

About Chien

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment