27.6.9: Practice- Loops
-
- Last updated
- Save as PDF
Review Questions
True / False
- The do while and repeat until structure act exactly the same.
- Students sometimes confuse assignment and equality.
- The repeat until looping control structure is available in all programming languages.
- Because flags are often used, they are usually a special data type.
- The do while is a test before loop.
- Only for loops can be counting loops.
- The integer data type has modular arithmetic attributes.
- The escape code of \n is part of formatting output.
- Nested for loops is not allowed in the C++ programming language.
- Counting loops use all four of the loop attributes.
Answers:
- false
- true
- false
- false
- false
- false
- true
- true
- false
- true
Activities
Complete the following activities using pseudocode, a flowcharting tool, or your selected programming language. Use separate functions for input, each type of processing, and output. Avoid global variables by passing parameters and returning results. Create test data to validate the accuracy of each program. Add comments at the top of the program and include references to any resources used.
While Loops
Complete the following using a while loop structure.
-
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
- 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.
- 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.
Do While / Repeat Until Loops
Complete the following using a do while / repeat until loop structure.
- Review MathsIsFun: Definition of Average . Create a program that asks the user to enter grade scores. Use a loop to request each score and add it to a total. Continue accepting scores until the user enters no value (empty input). Finally, calculate and display the average for the entered scores.
- 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 the user’s 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.
- Add a do while / repeat until loop to any activity from a previous chapter. Continue running the program while the user wants to continue or until the user wants to stop.
- Add an input validation loop to any activity from a previous chapter. Verify that the input is valid before returning the value. Ask the user to input the value again while the input is invalid.
For Loops
Complete the following using a for loop structure.
-
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
- 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.
- 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.
Nested Loops
Complete the following using a nested loop structure.
-
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
- Add a do while / repeat until loop to any activity from this chapter. Continue running the program while the user wants to continue or until the user wants to stop.
References
- cnx.org: Programming Fundamentals – A Modular Structured Approach using C++
- Wikiversity: Computer Programming