Programming in C
Programming in C
1
Example 1
2
Example 2
One solution is to copy and paste the necessary lines
of code. Consider the following modification:
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
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
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
12
Loop Pitfalls: Misplaced semicolon
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;
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
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
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
18
2
The for Repetition Structure
Syntax:
19
for loop example
Prints the integers from one to ten
20
for Loop Example
21
for Loop Example
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
25
The do-while Repetition Structure
Example
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