CS173: Intro to Computer Science - Iteration


Activity Goals

The goals of this activity are:
  1. To be able to explain the uses of the for loop structure
  2. To be able to apply boolean expressions to iterative structures via the for loop

Supplemental Reading

Feel free to visit these resources for supplemental background reading material.

The Activity

Directions

Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to take notes for the group, and appoint another member to discuss your findings with the class. After class, think about the questions in the reflective prompt and respond to those individually. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.

Model 1: The for Loop

Conditionals can be used to repeatedly execute code. There are three varieties of these “loops:” the for loop (which is useful when counting the number of iterations that are needed), the while loop (which is useful for executing until something is true), and the do loop (similar to the while loop, but it executes at least once and checks whether it should stop at the end of the loop, rather than at the beginning).

Questions

  1. The code prints the numbers from 0 through 9. Why doesn’t it also print the value 10?
  2. What could you do to change this to print the values 0 through 10?
  3. What could you do to print the values 1 through 10?
  4. What might you change in the code to print only the even values between 0 and 9, changing only the line beginning with for?

Model 2: Nested Loops

Questions

  1. How many times does the inner loop print statement execute?
  2. How many times does the outer loop print statement execute?
  3. How many times does the first print statement execute, and why?

Model 3: Finding the Error

Questions

  1. What does this program do if you execute it?
  2. Why does this program fail to do what you would expect?

Model 4: Flipping a Coin

Questions

  1. How many times would you expect to land on heads?
  2. What would you change to flip the coin 100 times? How many heads would you expect then?
  3. What would be the effect of changing the constant in the program from 0.5 to 0.3?

Submission

Submit your answers to the questions using the Collaborative Spaces section of OneNote. You can add a page with your name and your group members' names, and today's date, as the title. Under the appropriate section (i.e., "Class Notes", "Collaborative Spaces", "Reflective Prompts") that you can select on the left side of the screen, you can click "Add Page" on the right side. You can answer any reflective prompt questions in the Reflective Journal section of your OneNote Classroom personal section.

For Additional Practice

Feel free to visit these resources for additional practice exercises.