Computer Programming/Loops

From Wikiversity
Jump to: navigation, search
Flowchart Conditions

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

Objectives and Skills[edit]

Objectives and skills for this lesson include:

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

Readings[edit]

  1. Wikipedia: Control flow

Multimedia[edit]

  1. YouTube: Looping
  2. YouTube: Programming For Beginners - 'while' loops
  3. YouTube: Programming For Beginners - 'for' loops
  4. YouTube: Introduction to Programming - Iteration
  5. YouTube: Programming Basics #25 Iteration
  6. YouTube: Programming Basics #26 While Statement
  7. YouTube: Programming Basics #27 For Loop

Examples[edit]

Activities[edit]

  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. Using separate subroutines/functions/methods, implement the loop control structure based on a while loop in one subroutine and a for loop in the other subroutine. 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
    Using separate subroutines/functions/methods, implement the loop control structure based on a while loop in one subroutine and a for loop in the other subroutine. 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 might look like:
            1   2   3
        1   1   2   3
        2   2   4   6
        3   3   6   9
    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.

Lesson Summary[edit]

  • 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]

Key Terms[edit]

do while loop
for loop
foreach loop
while loop

Assessments[edit]

See Also[edit]

References[edit]