Title: Control Structures
1Control Structures
- Remember that in structured programming, we use
three basic control structures - Sequence
- Selection
- Repetition
- So far, we have worked with sequence and
selection - Were now going to look at repetition
2Repetition
- The repetition control structure allows us to
execute a set of instructions more than once - The number of times the loop is executed is
determined by a condition (just like the
conditions in selection statements) - As long as the condition is true, the set of
instructions will be executed again
3Repetition
- Repetition (looping, iteration) performs a
segment of code as long as condition remains true
or until a condition is false
?
False
True
exit loop
4Repetition Terminology
- A repetition control structure is often called a
loop - Another term you will hear for repetition is
iteration - The set of instructions the loop controls are
called the body of the loop - A sentinel value is a special value that
indicates the end of an input sequence. It does
not count as one of the input values.
5Loop Variations
- Three basic types
- Pre-testing condition is checked before body is
executed (the type we had in Jessica) - Post-testing body is executed once, then the
condition is checked to see whether the body
should be executed again - Counting specialized loops for handling cases
where the loop is controlled by the value of a
counter
6Pre-testing loop
- Also called a leading decision loop
- May not execute the body at all
- Condition is tested at the beginning of the loop
- Condition must be changed within the loop
total_score 0 Get exam_score DO WHILE
exam_score not 999 print student_name,
exam_score total_score total_score
exam_score get exam_score ENDDO
7DOWHILE Using a COUNTER
Print_student_records Set temp_count to
0 (initialize) DOWHILE temp_count lt 31
Prompt operator for daily_temp
Get daily_temp Display daily_temp
Add 1 to temp_count (increment) ENDDO
Print All temperatures printed END (How
many temperatures will be printed?)
8Print_student_records Read student_name,
test_score DOWHILE test_score not 999
Print student_name, test_score
Read student_name, test_score ENDDO
Print All students printedENDNote priming
read before DOWHILE trailer record - exam_score
999
9Example 1
- Given a bag of plastic chips with numbers on
them, count the number of chips in the bag
labeled with the number 2. Assume you cannot
tell whether there are chips in the bag until you
try to retrieve one
10Example 2
- Design an algorithm which will prompt for,
receive, and total a collection of payroll
amounts entered at the terminal until a sentinel
amount of -99 is entered. After the sentinel has
been entered, display the total payroll amount on
the screen.
11Pre-testing Loops in C
- while (condition) statement block
- while (num1 gt 5) total num1 num1--
12Updating Assignment Operators
- Shortcut versions of assignment operators
a b same as a a b add rvalue to
lvalue a - b same as a a - b subtract
rvalue from lvalue a b same as a a
b multiply a / b same as a a / b divide a
b same as a a b modulus Note if
rvalue is an expression, will evaluate it
first a b c equivalent to a a (b c)
13Increment and Decrement Operators
- Increment operator
- a way to add 1 to a variable
- these are all the same
- count count 1
- count 1
- count
- count
- Decrement operator
- --
- a way to subtract 1 from a variable
- these are all the same
- count count - 1
- count - 1
- count--
- --count
14Example 2 in C
- Design an algorithm which will prompt for,
receive, and total a collection of payroll
amounts entered at the terminal until a sentinel
amount of -99 is entered. After the sentinel has
been entered, display the total payroll amount on
the screen.
15Practice Problem
- Write a while loop to compute the sum of all
integers between first and second (including
first and second), where first and second are
integers and first lt second.
16Practice Problem 2
- Write a program that sums a sequence of integers.
Assume that the first integer read specifies the
number of values remaining to be entered. Your
program should read only one value each time
scanf is executed. A typical input sequence might
be 5 100 200 300 400 500
17Practice Problem 3
- Write a program that finds the smallest of
several integers. Assume that input will end
when a sentinel value of 999 is read. Do not
count 999 as one of the integers to consider.
18Post-testing Loops in Pseudocode
- REPEAT statement blockUNTIL condition p is true
- Always executed at least once
- Stop when condition is true
- Much less common
19Post-testing Example in Pseudocode
set_total to zero REPEAT Prompt for product
type Read product type Prompt for
product qty Read product qty Compute
product total Add product total to total
Ask if more purchases Get response UNTIL
response is NO
20Post-testing Loops in C
- do statement block while (condition)
- do printf(Enter a nonnegative integer
) scanf(d, num) while (num lt 0)
21Counted Loops in Pseudocode
- Used when exact number of iterations is known
- DO index initial_value to final_value statement
blockENDDO - DO i 1 to 10 Write iENDDO
22Counter-controlled loops can do all these things
DO temperature_count 1 to 30 Prompt
operator for max_temp Add max_temp to
max_total ENDDO
- Repeat the statement block
- initialize the loop_index to the initial value
- increment the loop_index each pass
- test the value of the loop_index each pass
- terminate the loop when loop_index exceeds final
value
(How many records will be processed?)
23Example 1
- Write an algorithm to sum the numbers 1 to 10 and
display the resulting sum.
24Counting Loops in C
- for (initialization condition modification)
statement block - sum 0for (i 1 i lt 11 i) sum i
- initialization - assignment statement to set loop
control variable - condition - determines when loop will exit
- modification - defines how loop control variable
will change
25for vs. while loop
These are the same
initialization
i 0 while (i lt 6) print (d, i)
i i 1
for (i 0 i lt 6 i i 1) printf
(d, i)
condition
increment
26Example 1 in C
- Write a function to sum the numbers 1 to 10 and
display the resulting sum.
27Practice Problem 1
- Write a program that sums a sequence of integers.
Assume that the first integer read specifies the
number of values remaining to be entered. Your
program should read only one value each time
scanf is executed. Use a for loop.
28Practice Problem 2
- Write nested for loops to print the pattern on
the right. Each printf should print a single
asterisk.
29Practice Problem 3
- Write a for loop that computes the number of
integer values that can be divided evenly by 11
or by 13 in the range of start through end, where
start and end are integer variables.
30Give the Output
Example 3 int r 10 while (r gt 0)
printf (d , r) r - 2
Example 1 int i 2 while (i lt 10)
printf (d, i)
Example 2 int x 2 do printf (d ,
x) while (x lt 10)
Example 4 for (r 7 r gt 3 r--) printf(d
, r)