Never get stuck in an infinite loop!
Sisyphus
The next C/C++ language construct we will discuss is a loop. A loop is used to instruct the computer to execute the same block of code multiple time. The most simple form of a loop is a while() loop – repeat the code block until the loop condition is met. The following example will keep asking the user to enter a positive number until user does so:
int x = 0;
while(x <= 0) {
cout << “Enter a number: “;
cin >> x;
}
cout << “Thank you! “ << x << endl;
The program will be executed as follows:
| Executed line of code | Value of x | Comments |
|---|---|---|
int x = 0; |
0 |
|
while(x <= 0) {
|
0 |
(x <= 0) is true, go in the loop |
cout << “Enter a number: “; |
0 |
Print message “Enter a number:” |
cint >> x; |
-5 |
Let’s assume user enters -5 |
} |
-5 |
Go back to the loop condition |
while(x <= 0) {
|
-5 |
(x <= 0) is true, go in the loop |
cout << “Enter a number: “; |
-5 |
Print message “Enter a number:” |
cint >> x; |
3 |
Let’s assume user enters 3 |
} |
3 |
Go back to the loop condition |
while(x <= 0) {
|
3 |
(x <= 0) is false, exit in the loop |
cout << “Thank you! “ << x << endl; |
3 |
Print message “Thank you!” |
Run this program in the Visual Studio debugger and enter different numbers. Note that we don’t know how many times the loop will be executed! It all depends on the numbers user enters.
Let’s look at another example that prints all the numbers between 0 and x:
int ii;
int x;
cin >> x;
ii = 0;
while(ii < x) {
cout << ii << endl;
ii = ii + 1;
}
cout << “Done” << endl;
If user enters x equal to 3, then the program will be executed as follows:
| Executed line of code | Value of ii | Value of x | Comments |
|---|---|---|---|
| cin >> x; | Undefined |
3 |
We assume that user enters 3 |
ii = 0; |
0 |
3 |
|
while(ii < x) {
|
0 |
3 |
(ii < x) is true, go in the loop |
cout << ii << endl; |
0 |
3 |
Print “0″ |
ii = ii + 1; |
1 |
3 |
|
} |
1 |
3 |
Go back to the loop condition |
while(ii < x) {
|
1 |
3 |
(ii < x) is true, go in the loop |
cout << ii << endl; |
1 |
3 |
Print “1″ |
ii = ii + 1; |
2 |
3 |
|
} |
2 |
3 |
Go back to the loop condition |
while(ii < x) {
|
2 |
3 |
(ii < x) is true, go in the loop |
cout << ii << endl; |
2 |
3 |
Print “2″ |
ii = ii + 1; |
3 |
3 |
|
} |
3 |
3 |
Go back to the loop condition |
while(ii < x) {
|
3 |
3 |
(ii < x) is false, exit the loop |
cout << “Done” << endl; |
3 |
3 |
Print message “Done” |
Try this program in Visual Studio and use the step-by-step debugger to follow the program execution. Remember to open variables watch window and see how the variables ii and x change on each step.
The previous example with a control variable ii that changes on every step is so common that C/C++ language has a special for() loop construct designed for it! This is how this example can be re-written with the for() loop:
int ii;
int x;
cin >> x;
for(ii = 0; ii < x; ii = ii + 1) {
cout << ii << endl;
}
cout << “Done” << endl;
The for() loop has the 3 pieces together at the top:
- control variable initialization: ii = 0
- loop condition: ii < x
- control variable update: ii = ii + 1
Any for() loop can be rewritten as a while() loop:
for(initialization; condition; update) {
loop body;
}
is the same as
initialization;
while(condition) {
loop body;
update;
}
And any while() loop can be rewritten as a for() loop by simply having empty initialization and update sections:
while(condition) {
loop body;
}
is the same as
for( ; condition; ) {
loop body;
}
To make code more readable, use for() loops when you have a control variable that you need to update on every loop execution and that you check to determine if it is time to exit the loop. Use while() loop in other cases.
You can combine and nest if(), if/else and loops together. Don’t forget curly brackets {} to indicate the boundaries of a code block and indentation to make code readable:
int x; int ii, jj;
cin >> x;
for(ii = 0; ii < x; ii = ii + 1) {
if(ii % 2 == 1) {
for(jj = 0; jj < ii; jj = jj + 1) {
if(jj % 2 == 1) {
cout << jj;
}
}
cout << endl;
}
}
What does the following program do? Try it in Visual Studio and use debugger to see what’s going on!
One common mistake with using loops is writing an infinite loop that never ends. For example, this loop has a condition that is always true (variable ii does not change in the loop body!) and it will print zillions of the same messages on the screen:
int ii = 0;
while(ii < 10) {
cout << “This is an infinite loop!” << endl;
}
To stop a program in an infinite loop, put focus in the program’s screen and press Ctrl+C . If this doesn’t help, open Task Manager (press Ctrl+Alt+Del), select the program process from the list and press “End Process” button.
Exercises
1) Read a number n from user and print all the even numbers between 0 and n.
2) Read a number n from user and calculate factorial n! = 1*2*3*….*n For example:
0! = 1 1! = 1 2! = 1*2 = 2 3! = 1*2*3 = 6 4! = 1*2*3*4 = 24 ...
3) Read a number n from user and calculate Fibonacci number a(n) where
a(0) = 0 a(1) = 1 a(2) = a(1) + a(0) = 0 + 1 = 1 a(3) = a(2) + a(1) = 1 + 1 = 2 a(4) = a(3) + a(2) = 2 + 1 = 3 a(5) = a(4) + a(3) = 3 + 2 = 5 ... a(n) = a(n-1) + a(n-2)
4) Read two numbers a and b from user and calculate divider a / b and reminder a % b by using only addition and subtraction.
6) Read two numbers a and b from user and calculate greatest common divisor for these two numbers using Euclid’s algorithm.
7) Read a positive number n from user and print all positive integer numbers (x, y) such as x*x + y*y < n.