# Computer Programming/Loops

This lesson introduces loops, including while, for, and do loops.

## Objectives and Skills

Objectives and skills for this lesson include:

• Understand while and do while loops
• Understand for and foreach loops
• Use loops to implement program functionality

1. Wikipedia: Control flow

## Activities

1. Review MathsIsFun: Definition of Average. Create a program that asks the user to enter grade scores. Start by asking the user how many scores they would like to enter. Then use a loop to request each score and add it to a total. Finally, calculate and display the average for the entered scores. In a single program, use separate subroutines/functions/methods to implement the loop control structure based on a while loop in one subroutine and a for loop in the other subroutine. Show you can implement the same logic using both loop structures. Use separate subroutines/functions/methods for input, processing, and output. Avoid global variables by passing parameters and returning results.
2. Create a program that uses a loop to generate a list of multiplication expressions for a given value. Ask the user to enter the value and the number of expressions to be displayed. For example, a list of three expressions for the value 1 would be:
`1 * 1 = 1`
`1 * 2 = 2`
`1 * 3 = 3`
A list of five expressions for the value 3 would be:
`3 * 1 = 3`
`3 * 2 = 6`
`3 * 3 = 9`
`3 * 4 = 12`
`3 * 5 = 15`
In a single program, use separate subroutines/functions/methods to implement the loop control structure based on a while loop in one subroutine and a for loop in the other subroutine. Show you can implement the same logic using both loop structures. Use separate subroutines/functions/methods for input, processing, and output. Avoid global variables by passing parameters and returning results.
3. Review MathsIsFun: 10x Printable Multiplication Table. Create a program that uses nested loops to generate a multiplication table. Rather than simply creating a 10 by 10 table, ask the user to enter the starting and ending values. Include row and column labels. For example, the output from 1 to 3 might look like:
`    1   2   3`
`1   1   2   3`
`2   2   4   6`
`3   3   6   9`
The output from 3 to 5 might look like:
`    3   4   5`
`3   9  12  15`
`4  12  16  20`
`5  15  20  25`
Use separate subroutines/functions/methods for input, processing, and output. Avoid global variables by passing parameters and returning results.
4. Review MathsIsFun: Pi. Write a program that uses the Nilakantha series to calculate Pi based on a given number of iterations entered by the user. How many iterations are necessary to accurately calculate Pi to 15 digits? Use separate subroutines/functions/methods for input, processing, and output. Avoid global variables by passing parameters and returning results.
5. Review Khan Academy: A guessing game. Write a program that allows the user to think of a number between 0 and 100, inclusive. Then have the program try to guess their number. Start at the midpoint (50) and ask the user if their number is (h)igher, (l)ower, or (e)qual to the guess. If they indicate lower, guess the new midpoint (25). If they indicate higher, guess the new midpoint (75). Continue efficiently guessing higher or lower until they indicate equal, then print the number of guesses required to guess their number and end the program.

## Lesson Summary

• Control flow refers to the order in which the individual statements, instructions or function calls of an imperative or a declarative program are executed or evaluated.[1]
• Control flow statement types include unconditional branch, conditional branch, conditional loop, subroutines, and unconditional halt.[2]
• A loop is a sequence of statements which is specified once but which may be carried out several times in succession.[3]

do while loop
for loop
foreach loop
while loop