Programming in C
1
Example 1
What if we want to process
three different pairs of integers?
2
Example 2
One solution is to copy and paste the necessary lines
of code. Consider the following modification:
What if you wanted to process four sets?
Five? Six? ….
3
Processing an arbitrary number of pairs
We might be willing to copy and paste to process a
small number of pairs of integers but
How about 1,000,000 pairs of integers?
The solution lies in mechanisms used to control the
flow of execution
In particular, the solution lies in the constructs that
allow us to instruct the computer to perform a task
repetitively
4
Repetition (Looping)
Use looping when you want to execute a block of code
several times
Block of code = Body of loop
C provides three types of loops
while statement
1 Most flexible
No ‘restrictions’
for statement
2 Natural ‘counting’ loop
do-while statement
3 Always executes body at least once
5
The while Repetition Structure
Repetition structure
Programmer specifies
Condition under which actions will be executed
Actions to be repeated
Psuedocode
While there are more items on my shopping list
Purchase next item and cross it off my list
while loop repeated
As long as condition is true
Until condition becomes false
6
The while Repetition Structure
• The condition is
tested false
condition
• If the condition is
true, the loop body
is executed and the true
condition is
loop body
retested.
• When the condition
is false, the loop is
exited. next stmt
7
1
The while Repetition Structure
Syntax:
while (expression)
basic block
Expression = Condition to be tested
Resolves to true or false
Basic Block = Loop Body
Reminder – Basic Block:
Single statement or
Multiple statements enclosed in braces
8
Loop Control Variable (LCV)
The loop control variable is the variable whose value
controls loop repetition.
For a while loop to execute properly, the loop control
variable must be
declared
initialized
tested
updated in the body of the loop in such a way that the
expression/condition will become false
If not we will have an endless or infinite loop
9
Counter-Controlled Repetition
Requires:
1. Counter variable , LCV, initialized to beginning value
2. Condition that tests for the final value of the counter
(i.e., whether looping should continue)
3. Constant increment (or decrement) by which the
control variable is modified each time through the
loop
Definite repetition
Loop executes a specified number of times
Number of repetitions is known
10
Example 3
EXECUTION CHART
count count<5 repetition
0 true 1
1 true 2
2 true 3
3 true 4
4 true 5
5 false
11
Loop Pitfalls
Enter value or zero to end: 2
What is wrong with my
program? It just sits there!
12
Loop Pitfalls: Misplaced semicolon
Notice the ‘;’ after the while condition!
Body of loop is between ) and ;
Result here: INFINITE LOOP!
Ctrl-c = Kill foreground process
13
The for Repetition Structure
A natural 'counting' loop
Steps are built into for structure!
1. Initialization initialization
2. Loop condition test
3. Increment or decrement false
condition
true
loop body
…
increment
next stmt
14
Review: Assignment Operators
Statements of the form
variable = variable operator expression;
can be rewritten as
variable operator= expression;
Examples of assignment operators:
a += 5 (a = a + 5)
a -= 4 (a = a - 4)
b *= 5 (b = b * 5)
c /= 3 (c = c / 3)
d %= 9 (d = d % 9)
15
Review: Pre-increment operator
Pre-increment operator: ++n
i) Stand alone: add 1 to n
If n equals 1, then after execution of the statement
the value of n will be 2.
ii) In an expression:
Add 1 to n and then use the new value of n in the expression.
If n is initially 1, the above statement will print the value 2.
After execution of printf, n will have the value 2.
16
Review: Post-increment operator
Pre-increment operator: n++
i) Stand alone: add 1 to n
If n equals 1, then after execution of the statement
the value of n will be 2.
ii) In an expression:
Use the value of n in the expression and then add 1 to n.
If n is initially 1, the above statement will print the value 1 and then
add 1 to n. After execution, n will have the value 2.
17
Pre- and Post-decrement operator
Pre- and post-decrement operators, --n, n-- ,
behave in a similar manner
Use caution when using in an expression
Do not use unless you know what you are doing!
18
2
The for Repetition Structure
Syntax:
for (initialization; test; increment)
basic block
19
for loop example
Prints the integers from one to ten
20
for Loop Example
How many times does loop body execute?
21
for Loop Example
How many times does loop body execute?
Bite 1 -- Yum!
Bite 2 -- Yum!
Bite 3 -- Yum!
22
The do-while Repetition Structure
The do-while repetition structure is similar to the
while structure
Condition for repetition tested after the body of the loop
is executed
loop body
true
condition
false
23
3
The do-while Repetition Structure
Syntax:
do {
statements
} while ( condition );
24
The do-while Repetition Structure
Example
Prints the integers from 1 to 10
25
The do-while Repetition Structure
Example
Makes sure that the user enters a valid weight
26
The break Statement
break
Causes immediate exit from
a while, for, do/while or switch structure
We will use the break statement
only to exit the switch structure!
27
The continue Statement
continue
Control passes to the next iteration
We will not use the continue statement!
28
Programming in C
THE END
29