Necessity for Repetition
Loops allow particular segments of code to be executed over and over again. For example, suppose a program wants to keep doubling the value of a number until a boolean condition is met. Or suppose one simply wants code to execute a set number of times. Repetition in programming code can easily be accomplished with loops.

Syntax of a While Loop
Loops generally consist of brackets enclosing one or more statements and are enclosed by a boolean condition. They execute when the condition is met and skip over to the next part of the code when the condition becomes false. If the condition is initially false, the loop is skipped over altogether. Unfortunately, when first introduced to loops, one can easily get stuck in what is known as an infinite loop. In such a case, a user running a program encounters a never-ending segment of the program, usually resulting in a crash. What happens is the program enters the loop, but none of the statements in the loop make the condition false. Therefore, there is no way to get out of the loop and the user is infinitely left going around in circles.

The syntax for a while loop is extremely simple. It begins with a header saying while (condition) where a condition is any boolean statement. Following this header are a set of brackets enclosing exactly what code is to be executed while the condition is true. On the other hand, in a do while loop, the word do is above the brackets, and the while (condition) is at the bottom of the brackets.

while (condition)
{
...
}


do
{
...
}
while (condition)

Testing Conditions
The difference between the two is where the test for the condition is. For example, suppose a variable is equal to 10. This is followed by a while loop which runs so long as the variable is equal to 11. None of the statements in the while loop are executed. However, suppose this was a do while loop. At the top of the loop all it says is the word "do." The code in the loop is executed once, in this case. But then the program reaches the bottom of the loop where it sees the condition. The loop is then not repeated a second time. Note that both loops contain the same condition. Yet the code within one is not executed at all while the code within the other is executed once.

int number = 10;
do
{
cout << "Hello World\n";
}
while (number == 11);

Accumulators are often used in conjunction with loops. Accumulators are variables which are continuously added to throughout a program. By placing an accumulator inside a loop, you can count up until a certain condition is met. For example, consider this algorithm: a college professor wants to enter test scores for the students in the class, and have the computer print out the average. However, the programmer doesn't know how many students are in the class. By using a do while loop and some basic math knowledge about how to determine numerical averages on the part of the computer programmer, the professor executing the program can find out the average. The professor, for example, can continuously enter test grades in a loop until they enter a flag value such as -1. With each grade entered, a sum accumulator will add that test grade to the sum of all the test grades. Another accumulator will count the number of grades that there are. A condition will look for this flag value and then know to end the loop. The sum can then be divided by the number of grades and the average will be shown to the professor.

For Loops
For loops can be considered a more specific form of the while loop. They are used when the loop is to execute a predetermined number of times. The syntax for a for loop is a bit more complicated. However, it compares directly to that of a mathematical summation. For loops involve a built-in accumulator variable which can be declared prior to the loop, or even inside the loop header.

for (int x = 1; x < 11; x++)
{
...
}

The above for loop, for example, executes ten times. The variable x starts off as 1. With each iteration, the value of x is incremented by one. The loop finally ends when x is equal to 11, in which case the loop is not executed that eleventh time (since the condition is at the top).

This article has been dead for over six months. Start a new discussion instead.