cpts121-6-2

# cpts121-6-2 - CptS 121 Fall 09 Lecture 6-2 HK Chapter 5...

This preview shows pages 1–6. Sign up to view the full content.

1 CptS 121 - 9/30/09 Prof. Chris Hundhausen CptS 121 Fall ‘09 Lecture 6-2 HK Chapter 5: Iteration Lecture Outline I. Iterative constructs II. Counter loops ( while and for ) III. Compound, increment, and decrement operators IV. Conditional Loops

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 CptS 121 - 9/30/09 Prof. Chris Hundhausen Iterative Constructs Recall that algorithms are composed of three different kinds of statements: Sequence : the ability to execute a series of instructions, one after the other. Conditional : the ability to execute an instruction contingent upon some condition. Iteration : the ability to execute one or more instructions repeatedly. This week, we'll learn about loops: the ability to repeatedly execute a sequence of statements.
3 CptS 121 - 9/30/09 Prof. Chris Hundhausen Iterative Constructs (cont.) You have already seen many examples of iteration Consider, for example, the following segment of the tollbooth application presented in lecture: axles1 = read_num_axles(infile); weight1 = read_weight(infile); axles2 = read_num_axles(infile); weight2 = read_weight(infile); axles3 = read_num_axles(infile); weight3 = read_weight(infile); toll1 = compute_toll(axles1,weight1); toll2 = compute_toll(axles2, weight2); toll3 = compute_toll(axles3, weight3); display_toll(axles1, weight1, toll1); display_toll(axles2, weight2, toll2); display_toll(axles3, weight3, toll3); Notice that this segment repeats four statements: axles = read_num_axles(infile); weight = read_weight(infile); toll = compute_toll(axles,weight); display_toll(axles, weight, toll);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 CptS 121 - 9/30/09 Prof. Chris Hundhausen Iterative Constructs How to decide when a loop is needed Are any steps repeated? - No No loop required - Yes Do you know in advance how many steps are repeated? No   Use a conditional loop Yes   Use a counting loop Kinds of loops Counting loop ( for or while ): executes a fixed number of times) Sentinel-controlled or Endfile-Controlled loop ( for or while ): (process data until a special value is encountered, e.g., end-of-file)
5 CptS 121 - 9/30/09 Prof. Chris Hundhausen Iterative Constructs (cont.) Kinds of loops (cont.)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern