Chap5 - Chapter 5 1 Outline Basic Loop Structures while...

Info icon This preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Chapter 5
Image of page 1

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

View Full Document Right Arrow Icon
2 Outline Basic Loop Structures while Loops Interactive while Loops for loops Loop Programming Techniques Nested Loops dowhile Loops Common Programming Errors
Image of page 2
3 Basic Loop Structures Repetition structure has four required elements: Repetition statement Condition to be evaluated Initial value for the condition Loop termination Repetition statements include while for do while
Image of page 3

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

View Full Document Right Arrow Icon
4 Basic Loop Structures (continued) The condition can be tested At the beginning: pretest or entrance- controlled loop At the end: posttest or exit-controlled loop Something in the loop body must cause the condition to change, to avoid an infinite loop, which never terminates
Image of page 4
5 Basic Loop Structures (continued) Pretest loop: condition is tested first; if false, statements in the loop body are never executed while and for loops are pretest loops
Image of page 5

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

View Full Document Right Arrow Icon
6 Basic Loop Structures (continued) Posttest loop: condition is tested after the loop body statements are executed; loop body always executes at least once do while is a posttest loop
Image of page 6
7 Basic Loop Structures (continued) Fixed-count loop: loop is processed for a fixed number of repetitions Variable-condition loop: number of repetitions depends on the value of a variable
Image of page 7

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

View Full Document Right Arrow Icon
8 while Loops while statement is used to create a while loop Syntax: while (expression) statement; Statements following the expressions are executed as long as the expression condition remains true (evaluates to a non-zero value)
Image of page 8
9 while Loops (continued) Figure 5.3 Anatomy of a while loop.
Image of page 9

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

View Full Document Right Arrow Icon
10 while Loops (continued)
Image of page 10
11 while Loops (continued)
Image of page 11

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

View Full Document Right Arrow Icon
12 Interactive while Loops Combining interactive data entry with the while statement provides for repetitive entry and accumulation of totals
Image of page 12