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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
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);
Background image of page 3

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

View Full DocumentRight Arrow Icon
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)
Background image of page 4
5 CptS 121 - 9/30/09 Prof. Chris Hundhausen Iterative Constructs (cont.) Kinds of loops (cont.) Input validation loop (
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/25/2011 for the course CPTS 121 taught by Professor Hundhaus during the Spring '09 term at Washington State University .

Page1 / 22

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

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online