Friday 17 May 2013

Iteration structures (loops)


Loops have as purpose to repeat a statement a certain number of times or while a condition is fulfilled.
The while loop
Its format is:
while (expression) statement
and its functionality is simply to repeat statement while the condition set in expression is true.
For example, we are going to make a program to countdown using a while-loop:

// custom countdown using while    Enter the starting number > 8
//8, 7, 6, 5, 4, 3, 2, 1, FIRE!
#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;
}

When the program starts the user is prompted to insert a starting number for the countdown. Then the while loop
begins, if the value entered by the user fulfills the condition n>0 (that n is greater than zero) the block that follows
the condition will be executed and repeated while the condition (n>0) remains being true.
The whole process of the previous program can be interpreted according to the following script:

1. User assigns a value to n
2. The while condition is checked (n>0). At this point there are two posibilities:
* condition is true: statement is executed (to step 3)
* condition is false: ignore statement and continue after it (to 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. Return automatically to step 2
5. Continue the program right after the block: print FIRE! and end program.
When creating a while-loop, we must always consider that it has to end at some point, therefore we must provide
within the block some method to force the condition to become false at some point, otherwise the loop will
continue looping forever. In this case we have included --n; that decreases the value of the variable that is being
evaluated in the condition (n) by one - this will eventually make the condition (n>0) to become false after a certain
number of loop iterations: to be more specific, when n becomes 0, that is where our while-loop and our countdown
end.
Of course this is such a simple action for our computer that the whole countdown is performed instantly without
any practical delay between numbers.

any practical delay between numbers.
The do-while loop
Its format is:
do statement while (condition);
Its functionality is exactly the same as the while loop, except that condition in the do-while loop is evaluated after
the execution of statement instead of before, granting at least one execution of statement even if condition is
never fulfilled. For example, the following example program echoes any number you enter until you enter 0.
// number echoer
#include <iostream>
using namespace std;
int main ()
{
unsigned long n;
do {
cout << "Enter number (0 to end): ";
cin >> n;
cout << "You entered: " << n << "\n";
} while (n != 0);
return 0;
}
Enter number (0 to end): 12345
You entered: 12345
Enter number (0 to end): 160277
You entered: 160277
Enter number (0 to end): 0
You entered: 0
The do-while loop is usually used when the condition that has to determine the end of the loop is determined within
the loop statement itself, like in the previous case, where the user input within the block is what is used to
determine if the loop has to end. In fact if you never enter the value 0 in the previous example you can be
prompted for more numbers forever.



No comments:

Post a Comment