What's diffirence between For and While Loop ?
- 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.
- 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
-
0 comments:
Post a Comment