Short_Iteration

Short_Iteration - C Programming From Problem Analysis to...

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

View Full Document Right Arrow Icon
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 5: Control Structures II (Repetition)
Background image of page 1

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

View Full Document Right Arrow Icon
Why Is Repetition Needed? Repetition allows you to efficiently use variables Can input, add, and average multiple numbers using a limited number of variables For example, to add five numbers: Declare a variable for each number, input the numbers and add the variables together Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers
Background image of page 2
The while Loop The general form of the while statement is: while (expression) statement while is a reserved word Statement can be simple or compound Expression acts as a decision maker and is usually a logical expression Statement is called the body of the loop The parentheses are part of the syntax
Background image of page 3

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

View Full Document Right Arrow Icon
The while Loop (continued) Expression provides an entry condition Statement executes if the expression initially evaluates to true Loop condition is then reevaluated Statement continues to execute until the expression is no longer true Initialize condition ; while ( Condition is true) { Task ; Reset condition ; } Continue ;
Background image of page 4
The while Loop (continued) Infinite loop : continues to execute endlessly Condition is not reset in the loop Can be avoided by including statements in the loop body that assure exit condition will eventually be false Proper resetting of the condition
Background image of page 5

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

View Full Document Right Arrow Icon
Background image of page 6
Background image of page 7

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

View Full Document Right Arrow Icon
Background image of page 8
Counter-Controlled while Loops If you know exactly how many pieces of data need to be read, the while loop becomes a counter-controlled (Counting) loop
Background image of page 9

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

View Full Document Right Arrow Icon
Sentinel-Controlled while Loops Sentinel variable is tested in the condition and loop ends when sentinel is encountered, a unique value (sentinel) will stop the loop.
Background image of page 10
Flag-Controlled while Loops A flag-controlled while loop uses a bool variable to control the loop The flag-controlled while loop takes the form:
Background image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 36

Short_Iteration - C Programming From Problem Analysis to...

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

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