Ask a question
0 0

Explain when you would use a while loop rather than a for loop and vice versa. Provide an example of one such instance.

Tutors, please sign in to answer this question.

3 Answers

Hi Murshed, if you know how to use them, you can use either. As my fellow tutors pointed out they both can be written to give you the same result.
As a rule of thumb, if you find yourself copying and pasting the same code over and over, think for loop. If you want to break your loop based on a runtime value, think while loop.
Zakiyyah's explanations are spot on. Here are some examples in C++, but the concepts are the same for most languages.

Proper use of a While Loop

Here we are taking user input and insisting the user give us a positive number:
int num;
cout << "What is your favorite positive integer? ";
cin >> num
while(num <= 0)
    cout << "What is your favorite positive integer? ";
    cin >> num;
cout << "My favorite number is " << num << ", too!" << endl;
This can be written with a for loop, but it just looks ugly. The reason we use a while loop is because we don't know how many times the user will give us the wrong kind of number.
int num;
cout << "What is your favorite positive integer? ";
cin >> num

for(; num <=0 ; )
    cout << "What is your favorite positive integer? ";
    cin >> num;
Notice how I had to leave the initialization and incrementation parts blank -- I have no need for them! Why? Because I don't know how many times they will give me an incorrect value!

Proper Use of a For Loop

If I know how many times I want to loop, I use a for loop. For example, here I will print out the contents of a vector of ints.
vector<int> myVec;
//The myVec variable is a vector containing 1, 2 and 3
for(int i = 0; i < myVec.size(); ++i)
   cout << "The number is: " << myVec[i] << endl;
I knew that exactly how many iterations I needed. I needed one for each element in the vector. I could do this as a while loop, but there are traps to doing so.
/* Initialize the vector same as above */
int i = 0;
while(i < myVec.size() )
    cout << "The number is: " << myVec[i]  << endl;
When using while loops in this fashion, one place you could make an error is forgetting to increment the loop counter (in this case the variable i). Doing so would give you an infinite loop and you wouldn't know it until run time. The for loop is better because it is designed with an incrementation portion so you won't forget it.
So in programming, the while loop and a for loop can be used interchangeably in some cases.
In other cases, it is better to use a while loop, and then again, others, a for loop is better.
Usually we'll use the for loop when we know how many time we want to execute the loop.  (for x<=100) whereas we normally will use a while loop when the number of iterations are not known.  (while x>0)
Since the for loop provides incremental change of the variable, it is better to use it when "counting"
Otherwise, if you want the the condition to just be true or false ex while (true) or while (false)
its better to use a while loop.
Keep in mind a while loop and be translated to for loop or vice versa;
for (x=0; x<10; x++)
can be translated to
int x=0
while (x<10)
Sorry, I'll update tomorrow with some examples if I can but hopefully that's a good start.