Loops
Loops are the most accepted way to execute a repetitive action.
In C++ there are multiple loop options but the most common are the For Loop and the While Loop (the do While, and goto are also options but all goto statements are considered BAD form when working in higher languages)
For Loops
- are most effective if you know the number of repetitions required.
- Requires a variable (often seen as a i, j or k) to be set to the starting number you are using (often 1 or 0)
- Needs a condition to stop
- Needs how much to increment (by default it increments by 1)
Syntax:
For ( variable initialization; condition; variable update) {
Code to execute while the condition is true
}
Example:
// do 3 iterations using a for loop
for (int i=0; i!=3; i++) {
cout << i << ", ";
}
Or
// countdown using a for loop
for (int n=10; n>0; n--) {
cout << n << ", ";
}
It works this way:
1. The variable is initialized. Generally it is an initial value setting for a counter variable. This is executed only once.
2. The condition is checked. If it is true the loop continues, otherwise the loop ends and the statement or instructions within the {} is skipped (not executed).
3. The statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }.
4. The variable value is increased or decreased by whatever is specified in the increase field and the loop goes back to step 2 checking the condition.
While loops
While loops are more effective when we know we have to repeat something but we are unsure how many repetitions we have to make. The while loop continues to run until the condition you have set is no longer true. Since the condition is at the beginning of the loop we call this a pre-test loop
Syntax:
while (expression) statement
For example:
// custom countdown using while
#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0) {
cout << n << ", ";
--n;
}
cout << "FIRE!\n";
return 0;
}
What is going to be the output from this example?
In order for the while loop to execute in the code above:
1. The user must assign a value to n before the loop.
2. The while condition is checked (n>0). At this point there are two possibilities:
· condition is true: therefore the statement is executed (step 3)
· condition is false: ignore statement and jump to the end (step 5)
3. Execute statement:
cout << n << ", ";
--n;
(prints the value of n on the screen and decreases n by 1)
4. End of block. Returns automatically to step 2
5. Continue the program right after the block
Knowing that a ‘0’ represents “false” or “off and ‘1’ represents “true” or “on”
What would be the easiest way to make a forever loop?
There are two additional key words that you will be introduced to at a later date that help within loops and conditional statements. They are break and continue. Once you start executing larger blocks of code within your loops they may become useful.<!--
/**
* GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
* (http://qbnz.com/highlighter/ and http://geshi.org/)
*/
.text {font-family:monospace;}
.text .imp {font-weight: bold; color: red;}
.text span.xtra { display:block; }
-->