Back to Bluesfear
Digital Art

C++ - While Loops

Written by Gary Texmo

The while loop is almost exactly the same as the do loop except that its condition is tested at the start of the loop intead of at the end... and the format is slightly different. The easiest way to see this is simply to re-write the above example with a while loop.

 

#include <iostream.h>

 

int main(void) {

int x = 0;

int y = 0;

 

cout << "Please enter an integer between 1 and 10: ";

cin >> x;

cout << "You entered: " << x << endl << endl;

 

while ((x < 1) || (x > 10)) {

cout << "Please enter an integer between 1 and 10: ";

cin >> x;

cout << "You entered: " << x << endl << endl;

 

if ((x < 1) || (x > 10)) {

cout << "Your value for x is not between 1 and 10!"

<< endl;

cout << "Please re-enter the number!" << endl << endl;

}

}

 

cout << "Thank you for entering a valid number!" << endl;

 

return 0;

}

 

Note that since the condition is tested at the start, we have to prompt the user for a number before we start. Depending on the circumstance, we could arrange this loop in a more intelligent way. For instance, if we set the value of x to one that would make the loop execute as false the first time through, then it would work without gathering input before the loop. This is generally not a good idea though since we might have this code in a function and the variable in question might be an argument (more later on that if you don't understand it). A more accepted way of accomplishing our goal of laziness would be to use a sentinel. Usually a boolean (true or false) variable that we would set to false when the condition was not satisfied and set to true when it was. Then we can loop so long as our sentinel is false. Check it out.

 

#include <iostream.h>

 

int main(void) {

int x = 0;

int y = 0;

bool validNumber = false;

 

while (validNumber == false) {

cout << "Please enter an integer between 1 and 10: ";

cin >> x;

cout << "You entered: " << x << endl << endl;

 

if ((x < 1) || (x > 10)) {

cout << "Your value for x is not between 1 and 10!"

<< endl;

cout << "Please re-enter the number!" << endl << endl;

}

else

validNumber = true;

}

 

cout << "Thank you for entering a valid number!" << endl;

 

return 0;

}

 

So there you go, a sentinel lets us accomplish the same thing in less code (always a bonus). One last thing to note is that do and while loops can be nested just like for loops in the same way. Sometimes it's not apparent so I just thought I'd let you know.

 

In conclusion, loops are an easy way to execute the same block of code more than once. Very good for those nitty gritty repetative tasks that make programming so much fun. You might want to fool around with loops a little just to get a feel for them, but they are pretty simple and after seeing a few more applications of them, you'll have it down pat if you don't already.

Tutorial Info
Written By: Gary Texmo
Written For: Mike of http://www.bluesfear.com

Back to Bluesfear
Digital Art

2000, 2004 BlueSfear