### Repetition Structures

Computer programs make use of selection structures and repetition structures as their main tools ... repetition means repeating the same action continuously until some finishing condition is obtained. In early programming language this was achieved through the use of the `goto` statement. The `goto` statement allows the program to jump from one place to another and can easily produce "spaghetti code" - because of this, it is rarely (never) used in C++

### The While Repetition Structure

A while structure allows the program to repeat a set of statements as long as the starting condition remains true. 'true' is used in the boolean sense and so the condition for the loop to continue must evaluate to true or false

The program evaluates the condition, if it is true then the statements(s) inside the braces are executed. The condition evaluated again .... this will continue until the condition is false and then the program jumps to the next line after the body of the structure. If only one statement is used in the while loop then the braces are not required

```	while (condition)
{
statements;
}
```

The following code fragment outputs the numbers 1 to 10. The `counter++` statement increments the counter by one each time

```int counter = 1;

while (counter < 11)
{
cout << counter << endl;
counter++;
}
```

Exercise: Write a program that displays the first 20 numbers and their squares in two columns (hint: use \t to space columns

The condition of the while structure can be any legal C++ expression and include the logical operators. The following code fragment continues to ask for an input between 1 and 30 until one is received:

```int number=0;
cout << "input a number between 1 and 30 inclusive" << endl;

while ((number <= 0) ¦¦ (number > 30))
{
cin >> number;
}
```

Exercise: Write a program that asks for an even number divisible by 3 and keeps asking for one until an appropriate number is entered

#### Caution - control those loops!

A loop (repetition structure) must have a condition that causes it to exit otherwise an infinite loop is created that will run forever. It is the responsibility of the programmer to carefully account for every possibility and ensure that the loop always ends

Looping is generally controlled in one of two ways:
(i) Counter controlledrepetition. The counter is incremented in the body of the loop. A counter is used and the loop terminates when a predefined number of iterations have been completed. Applications are limited to those where you know the number of repetitions that are necessary
(ii) Sentinel controlled repetition. The loop continues until some 'special' sentinel value is entered. The loop tests for the sentinel on each iteration. It is important to ensure that the sentinel value is not entered by accident as part of the valid data. If you are asking for peoples ages, -1 would make a good sentinel value. This type of control is most suitable where the number of repetitions is not predefined

#### Continue & Break

Two commands that can be included in looping processes are `continue` and `break`. The `continue` statement, when included in the body of the loop, causes the program to return immediately to the start of the loop and ignore any remaining part of the body. The `break` statement causes the program to immediately exit from the loop and resume at the next line of the program

Exercise: Write a program to output all the integers between 1 and 100 excluding those divisible by 3 (hint: use continue)

### The Do ... While Repetition Structure

A variation on the simple while loop is the do ... while structure. The do part contains the body and is executed as long as the while condition remains true

```	do
{
statements;
}
while (condition);
```

The difference between these two 'while' structures is that with a simple while loop the condition is evaluated first and if it immediately false, the body is never executed. In the do ... while loop, the body is executed before the condition is tested and so guaranteed to be executed at least once

### The For Repetition Structure

In the while loop it is quite common to set an initial condition, test the condition and increment the condition as part of the loop. This is more easily achieved using the for loop which has the following structure:

```	for (statement1; statement2; statement3)
{
statements in the body;
}
```

(i) Statement1 can be any C++ statement but is usually the initialization of a counter
(ii) Statement2 must return a value that evaluates to either true or false - the condition
(iii) Statement3 can be any legal C++ statement but is typically used to increment the counter

The following code fragment prints from 1 to 10:

```for (i=1; i <= 10; i++)
cout << i << endl;
```
or
```for (i=1; i < 11; ++i)
cout << i << endl;
```
or, backwards
```for (i=10; i > 0; i--)
cout << i << endl;
```

Avoid using conditions such as i==5 or i != 7 etc as round off error means that these might not cause the loop to exit as expected, always use the greater than or less than operators in the condition

Exercise: Write a program using the `for` statement that asks the user for 10 integers and returns the average value.

Exercise: Adapt the program above ask for positive integers and to keep accepting them until a sentinel value of 0 is entered. You will need separate counters for the number of items entered and the total. Test for non valid input - i.e negative numbers

Exercise: Re write the average program above to allow the user to define how many numbers will be input and then allow the user to input any type of numbers, integers or non-integers, positive or negative

Exercise: Write a program that asks the user for the dimensions of a rectangle and then prints a rectangle of * that size on the screen - you will need to use nested for statements