Addison wesley professional-dojo_using_the_dojo_java_script_library_to_build_ajax_applications_2009
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5:...
-
Upload
jesus-crabtree -
Category
Documents
-
view
217 -
download
1
Transcript of © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5:...
![Page 1: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/1.jpg)
© 2010 Pearson Addison-Wesley. All rights reserved.
Addison Wesley is an imprint of
Chapter 5:Repetition and Loop Statements
Problem Solving & Program Design in C
Sixth Edition
By Jeri R. Hanly &
Elliot B. Koffman
![Page 2: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/2.jpg)
1-2
© 2010 Pearson Addison-Wesley. All rights reserved. 5-2
Introduction to Repetition Structures
A repetition structure causes a statement or set of statements to execute repeatedly
Allow a programmer to avoid duplicate code– Duplicate code makes a program large– Write a long sequence of statements is time
consuming– If part of the duplicate code has to be
corrected or changed, then the change has to be done many times
![Page 3: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/3.jpg)
1-3
© 2010 Pearson Addison-Wesley. All rights reserved. 5-3
Condition-Controlled Loops
• While Loop– While a condition is true, do some task
• Do-While Loop– Do some task, while condition is true
• Do-Until Loop– Do some task, while a condition is false (or
until it’s true)
• With all loops, be careful not to create infinite loops – always provide a way to break out
![Page 4: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/4.jpg)
1-4
© 2010 Pearson Addison-Wesley. All rights reserved. 5-4
Condition-Controlled Loops
The While Loop – pretest loopWhile condition
Statement
Statement
End While
Figure 5-1 The logic of a While loop
![Page 5: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/5.jpg)
1-5
© 2010 Pearson Addison-Wesley. All rights reserved. 5-5
Condition-Controlled Loops
Working with Modules and Loops• To run a program multiple times, modules
can be put within a loop
Figure 5-5 The main module of Program 5-3
![Page 6: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/6.jpg)
1-6
© 2010 Pearson Addison-Wesley. All rights reserved. 5-6
Condition-Controlled Loops
The Do-While Loop – posttest loop Do
Statement
Statement
While condition
Figure 5-8 Flowchart for the main module in Program 5-4
![Page 7: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/7.jpg)
1-7
© 2010 Pearson Addison-Wesley. All rights reserved. 5-7
Condition-Controlled Loops
The Do-Until Loop• Loop iterates until a condition is true, but
not all languages support this type of loop
Figure 5-10 The logic of a Do-Until loop
![Page 8: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/8.jpg)
1-8
© 2010 Pearson Addison-Wesley. All rights reserved. 5-8
Count-Controlled Loops
A count-controlled loop iterates a specific number of times
• A for loop is best used for this situationFor counterVariable = startingValue to maxValue
statement
statement
End for
• There is an Initialization, Test, and Increment expression that controls the loop
![Page 9: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/9.jpg)
1-9
© 2010 Pearson Addison-Wesley. All rights reserved. 5-9
Count-Controlled Loops
For loops can also increment by more than one, count backwards by decrementing, or allow the user to control the number of interactions
The for loop in actionFigure 5-14 Flowchart for Program 5-8
![Page 10: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/10.jpg)
1-10
© 2010 Pearson Addison-Wesley. All rights reserved. 5-10
Count-Controlled Loops
General loop concerns• Do not forget to initialize the loop control
variable• Do not forget to modify the loop control variable• Many loops are interchangeable, but generally
– Use while loop when loop may not have to process
– Use do while when it must process at least once
– Use for loop with specific number of iterations
![Page 11: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/11.jpg)
1-11
© 2010 Pearson Addison-Wesley. All rights reserved. 5-11
5.4 Calculating a Running Total
A running total is a sum of number
that accumulates with each iteration of a loop
![Page 12: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/12.jpg)
1-12
© 2010 Pearson Addison-Wesley. All rights reserved. 5-12
Sentinels
A sentinel is a special value that marks the end of a list of values, used as stop values for loops
How it can be done– Ask the user at the end of each loop iteration,
if there is another value to process– Ask the user at the beginning of the loop,
how many times the loop should process
![Page 13: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/13.jpg)
1-13
© 2010 Pearson Addison-Wesley. All rights reserved. 5-13
Sentinels – Controlled loop
1. Initialize sum to zero.
2. Get first Score
3. While score is not the sentinel
a. Add score to sum
b. Get the next score
#define SENTINEL -99
printf(“Enter first score (or %d to quit) “ ,SENTINEL);
scanf(“%d”, &score);
while (score != SENTINEL){
sum += score;
printf(“Enter next score (%d to quit) “, SENTINEL);
scanf(“%d”, &score);
}
printf(“\nSum of exam scores is %d\n”, sum);
printf(“Enter first score (or %d to quit) “ ,SENTINEL);
for (scanf(“%d”, &score);
score != SENTINEL;
scanf(“%d”, &score)) {
sum += score;
printf(“Enter next score (%d to quit) “, SENTINEL);
}
![Page 14: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/14.jpg)
1-14
© 2010 Pearson Addison-Wesley. All rights reserved. 5-14
Nested Loops All loops can be nested, that is, a loop inside of a loop
Figure 5-21 Flowchart for a clock simulator
![Page 15: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/15.jpg)
1-15
© 2010 Pearson Addison-Wesley. All rights reserved. 1-15
Figure 5.1 Flow Diagram of Loop Choice Process
![Page 16: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/16.jpg)
1-16
© 2010 Pearson Addison-Wesley. All rights reserved. 1-16
Figure 5.2 Program Fragment with a Loop
![Page 17: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/17.jpg)
1-17
© 2010 Pearson Addison-Wesley. All rights reserved. 1-17
Figure 5.3 Flowchart for a while Loop
![Page 18: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/18.jpg)
1-18
© 2010 Pearson Addison-Wesley. All rights reserved. 1-18
Figure 5.4 Program to Compute Company Payroll
![Page 19: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/19.jpg)
1-19
© 2010 Pearson Addison-Wesley. All rights reserved. 1-19
Figure 5.4 Program to Compute Company Payroll
![Page 20: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/20.jpg)
1-20
© 2010 Pearson Addison-Wesley. All rights reserved. 1-20
Figure 5.5 Using a for Statement in a Counting Loop
![Page 21: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/21.jpg)
1-21
© 2010 Pearson Addison-Wesley. All rights reserved. 1-21
*Multiplication Table of 9*//* Displays the 9s Table */
#include <stdio.h> /* printf, scanf definitions */#define MULT_NUM 9intmain(void){
int num, answer = 0;
/* Display 9s Table */printf("The 9's Multiplication Table \n\n");for (num = 0; num < 10; ++num) {
answer = num * MULT_NUM;printf("%d times %d = %d \n", num, MULT_NUM, answer);
}printf(" \n");return (0);
}
![Page 22: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/22.jpg)
1-22
© 2010 Pearson Addison-Wesley. All rights reserved. 1-22
/*Multiplication Tables*//* Displays all the tables */
#include <stdio.h> /* printf, scanf definitions */
intmain(void){
int num, mult_num, answer = 0;
/* Display 9s Table */printf("The Multiplication Tables \n\n");for (num = 0; num < 10; ++num) {
for(mult_num = 0; mult_num < 10; ++mult_num){answer = num * mult_num;printf("%d times %d = %d \n", num, mult_num, answer);
}printf(" \n");}printf(" \n");return (0);
}
![Page 23: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/23.jpg)
1-23
© 2010 Pearson Addison-Wesley. All rights reserved. 1-23
Figure 5.6 Comparison of Prefix and Postfix Increments
![Page 24: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/24.jpg)
1-24
© 2010 Pearson Addison-Wesley. All rights reserved. 1-24
Figure 5.7 Function to Compute Factorial
![Page 25: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/25.jpg)
1-25
© 2010 Pearson Addison-Wesley. All rights reserved. 1-25
Figure 5.8 Displaying a Celsius-to-Fahrenheit Conversion Table
![Page 26: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/26.jpg)
1-26
© 2010 Pearson Addison-Wesley. All rights reserved. 1-26
Figure 5.9 Program to Monitor Gasoline Storage Tank
![Page 27: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/27.jpg)
1-27
© 2010 Pearson Addison-Wesley. All rights reserved. 1-27
Figure 5.9 Program to Monitor Gasoline Storage Tank
![Page 28: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/28.jpg)
1-28
© 2010 Pearson Addison-Wesley. All rights reserved. 1-28
Figure 5.9 Program to Monitor Gasoline Storage Tank
![Page 29: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/29.jpg)
1-29
© 2010 Pearson Addison-Wesley. All rights reserved. 1-29
Figure 5.10 Sentinel-Controlled while Loop
![Page 30: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/30.jpg)
1-30
© 2010 Pearson Addison-Wesley. All rights reserved. 1-30
Figure 5.11 Batch Version of Sum of Exam Scores Program
![Page 31: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/31.jpg)
1-31
© 2010 Pearson Addison-Wesley. All rights reserved. 1-31
Figure 5.12 Program to Process Bald Eagle Sightings for a Year
![Page 32: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/32.jpg)
1-32
© 2010 Pearson Addison-Wesley. All rights reserved. 1-32
Figure 5.12 Program to Process Bald Eagle Sightings for a Year
![Page 33: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/33.jpg)
1-33
© 2010 Pearson Addison-Wesley. All rights reserved. 1-33
Figure 5.13 Nested Counting Loop Program
![Page 34: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/34.jpg)
1-34
© 2010 Pearson Addison-Wesley. All rights reserved. 1-34
Figure 5.14 Validating Input Using do-while Statement
![Page 35: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/35.jpg)
1-35
© 2010 Pearson Addison-Wesley. All rights reserved. 1-35
Figure 5.15 Structure Chart for Computing Solar Collecting Area Size
![Page 36: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/36.jpg)
1-36
© 2010 Pearson Addison-Wesley. All rights reserved. 1-36
Figure 5.16 Program to Approximate Solar Collecting Area Size
![Page 37: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/37.jpg)
1-37
© 2010 Pearson Addison-Wesley. All rights reserved. 1-37
Figure 5.16 Program to Approximate Solar Collecting Area Size
![Page 38: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/38.jpg)
1-38
© 2010 Pearson Addison-Wesley. All rights reserved. 1-38
Figure 5.16 Program to Approximate Solar Collecting Area Size
![Page 39: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55150c53550346a80c8b5929/html5/thumbnails/39.jpg)
1-39
© 2010 Pearson Addison-Wesley. All rights reserved. 1-39
Figure 5.16 Program to Approximate Solar Collecting Area Size