Simple Control Structures: (Statement1 Statement2 Statement3 )
Simple Control Structures: (Statement1 Statement2 Statement3 )
prints on the screen x is 100 if indeed x has a value of 100, but if it has
not -and only if not- it prints out x is not 100.
The if + else structures can be concatenated with the intention of
verifying a range of values. The following example shows its use telling if the
value currently stored in x is positive, negative or none of them (i.e. zero):
1 if (x > 0)
2 cout<< "x is positive";
3 else if (x < 0)
4 cout<< "x is negative";
5 else
6 cout<< "x is 0";
Remember that in case that we want more than a single statement to be
executed, we must group them in a block by enclosing them in braces { }.
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:
1 // custom countdown using while Enter the starting number > 8
2 #include <iostream> 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
3 using namespace std;
4 int main ()
5 {
6 int n;
7 cout<< "Enter the starting number >
8 ";
9 cin>> n;
10 while (n>0) {
11 cout<< n << ", ";
12 --n;
13 }
14 cout<< "FIRE!\n";
15 return 0;
16 }
17
18
19
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 (beginning in main):
1. User assigns a value to n
2. The while condition is checked (n>0). At this point there are two
possibilities:
3. * condition is true: statement is executed (to step 3)
* condition is false: ignore statement and continue after it (to step 5)
4. Execute statement
cout<< n << ", ";--n;
(prints the value of n on the screen and decreases n by 1)
5. End of block. Return automatically to step 2
6. 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.