## Week 5 - Repetition and Loop Statements

During this week we cover Chapter 5 of Friedman & Koffman, in which we learn how to repeat a process many times. We will cover the three C++ loop control statements: while, for, and do-while. This will complete the three basic program control structures, being sequence, selection, and repetition.

The various relevant programs for this week have been loaded onto condor. After logging in, change directory to the week5 directory and list the various files as follows:

cd /home/condor/et181/week5
ls

• Section 5.1: Counting Loops and the While Statement
av_grade.cpp - determine the average class grade - This is actually a sentinel controlled conditional while loop covered in Section 5.5, however we prefer to introduce the while loop form with this program.
while_bottles.cpp - recall our two programs bottles.cpp and arg_bottles.cpp introducing functions with "3 bottles of beer" in the week2 directory? Well now with a while loop we can have more beer! (as many bottles as we wish)

• Section 5.2: Accumulating a Sum or a Product in a Loop
computePay.cpp - computes the payroll for a company

• Section 5.3: The for Statement
for_bottles.cpp - introducing the for loop with "n bottles of beer" (counting down)
factorial.cpp - computes the factorial of a number (refer to the function on Example 5.3)
temperatureTable.cpp - conversion of Celsius to Fahrenheit temperature (Example 5.5)

Another common aspect of the for loop is its use in converting a series summation (Σ) into C++ code. Thus for example, the series summation:

could be represented by the following for loop:

```int sum = 0;
for(int i = 1; i <= n; i++)
sum = sum + i*i; // also: sum += i*i;```
• Section 5.4: Conditional Loops
oilSupply.cpp - (case study - Monitoring Oil Supply)

• Section 5.5: Conditional Loops
sumScores.cpp - accumulates the sum of exam scores (Example 5.6) (very similar to our first program example av_grade.cpp)

• Section 5.6: The do-while Statement
largest.cpp - finds the largest number in a sequence of integer values (Example 5.10)

• Section 5.8: Nested Loops
nestedLoops.cpp - illustrates a pair of nested for loops (Example 5.12)
multiplication.cpp - Displays the multiplication table

• Lab 5: (One of the following labs will be done during the 5th lab session)
lab-integral - integration using the Trapezoidal Method
lab-root - root finding using the Bisection Method

Note that before modifying or compiling any example in the week5 directory, you will first need to copy that example to your home directory, and return to your home directory as in the following example:

cp for_bottles.cpp ~
cd