Looping
Beginning C++
You must have noticed that all of the programs that we have made up to this point let you type in one thing, give you an answer and then you must close it. Looping allows you to execute a block of code as many times as you want without the program closing on you.

                                                                              
While Loop
The while loop will execute a block of code as long as a condition is true. A true condition for example would be like 5<6 because 5 is less tham 6. 8>3 this condition is false because 8 is not greater than 3. Here is a small program illustrating how loops are used.
Input, Output and Variables
#include <iostream.h>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
int guess;

while (guess != 4) {
cout << "guess a number between 1 and 10\n";
cin >> guess;
}

system("pause");
return 0;
}
Operators
Looping
Variables
Functions
In the program above, the loop will continue to execute until the number 4 is entered. Try adding a while loop into our guessing game we made earlier. The code below is our guessing game with a loop, try to find out exactly how the while loop works before continuing in the tutorials.
#include <iostream.h>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
int number = 8;
int guess;

while (guess != 8) {

cout << "im thinking of a number between 1 and 10\n";
cout << "enter your guess please\n";

cin >> guess;

if (guess == number) {
cout << "you are correct\n";
}
else if (guess < 8) {
cout << "guess a higher number\n";
}
else if (guess > 10) {
cout << "your not supposed to guess this high\n";
}
else {
cout << "guess a lower number\n";
}
}
system("pause");
return 0;
}
Click here to see how the program above should execute when compiled.
                                                                                Do loop
The "do" loop is used just like a "while" loop except it tests to see if the condidion is true at the bottom of the loop rather than at the top. Here is how a "do" loop is written. This program does the exact same thing as the one above.
#include <iostream.h>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
int number = 8;
int guess;
do {
cout << "im thinking of a number between 1 and 10\n";
cout << "enter your guess please\n";

cin >> guess;

if (guess == number) {
cout << "you are correct\n";
}
else if (guess < 8) {
cout << "guess a higher number\n";
}
else if (guess > 10) {
cout << "your not supposed to guess this high\n";
}
else {
cout << "guess a lower number\n";
}
}while (guess != 8);

system("pause");
return 0;
}
                                                                            For Loops
The "for" loop will execute a block of code for a certain amount of times. Here is how you would write it:
for (int count = 1; count <= 10; count++) {cout << count; } "count" was the variable in this example and the value of count was 1. In the second section you see this: count <= 10 and that is telling the program to count from the value of "count" and go until it reaches 10. The "++" after "count" tells the program to count by 1. The things in blue display the results of the count on your screen. To make the results display vertically, write it like this: {cout << count << "\n";}
You can also make a loop capable of terminating two ways like in the example below. In the example below, the loop will terminate when the number "n" is entered or when the loop has executed 100 strait times.
#include <iostream.h>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
char response;
int count;
for (count = 1; (response != 'n') && (count <= 100); count++)
{
cout << count << "\n";
cout << "continue? (y:n): ";
cin >> response;
}
system("pause");
return 0;
}
In the loop above, we see this to initiliaze a function: char instead of int and letters are used for input in this example. This will all be explained in the next tutorial.