At the end of this lesson, you will be able to:
understand and use a for loop
With a pencil and paper, answer the following questions:
What is the minimum number of times a DO..WHILE loop will execute? Why?
In what situation would you use a DO..WHILE loop instead of a WHILE loop?
Write the pseudocode for the following:
Ask the user to enter a word or "q" to quit
Use a DO..WHILE loop to keep track of all the words entered
After the loop has finished, display the total number of words entered
do..while loops
execute a minimum of 1 time
from the online book Computer Based Problem Solving by Patrick Coxall, read:
For loops:
have the counter initialization, boolean expression & counter increment/decrement all on the first line
however, the boolean expression and counter increment/decrement are only executed AFTER each loop is executed
execute a minimum of 0 times
An infinite loop occurs when the boolean expression never evaluates to false and therefore the loop goes on forever and your program crashes.
see these examples
Note: in order to specify a range from 0 to num, you must type:
for counter in range (num + 1)
Example 1
#!/usr/bin/env python3
#to iterate on the number 0 to 9 passes
for counter in range(10):
Example 2
#!/usr/bin/env python3
#to iterate between 10 to 19 passes
for counter in range(10, 20):
Example 3
#!/usr/bin/env python3
#to iterate on the number 1 to 12 going up by 2 each time
for counter in range(1, 13, 2):
Write a program that asks the user to enter a whole number (i.e. 0, 1, 2, ...)
It then uses a for loop to calculate the power of two of each number from 0 until that number.
Make sure your program does not crash if a user enters invalid input!
in groups of 2, do the following on the board for today's daily assignment:
Top-Down Design
Flow Chart
Pseudocode
Test Cases
complete the Daily Assignment section in Hãpara Workspace for this day
if Hãpara is not working, make a copy of this document
move it to your IMH-ICS folder for this course
recreate the same program in C++
Note: This code does not have error checking!