Repetition - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Repetition

Description:

... that before or after each iteration, it checks to see if ... Within each iteration of the first or outer loop and inner loop is executed one or more times. ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 13
Provided by: johnpm3
Category:

less

Transcript and Presenter's Notes

Title: Repetition


1
Repetition
Repetition allows you to repeat an operation or a
series of operations many times. This is called
looping and is one of the basic structured
programming concepts.
An action or a series of actions
The above flowchart would be considered an
infinite loop because its repeats the action over
and over again and never stops. To make a loop
end we must have a condition that controls the
loop. In other words, the loop must be designed
so that before or after each iteration, it checks
to see if it is done.
false
Condition
Body (action)
true
true
Condition
Body (action)
false
Pretest loop
Posttest loop
2
Repetition
In a pretest loop, the body can be done zero or
more times. In a posttest loop, the body must be
done at least once. Before a loop can start, a
variable must be initialized. The value of a
variable or expression is tested. Then something
must happen inside the loop to update the
variable, otherwise you could have an infinite
loop.
Initialize
Initialize
false
Condition
Action
true
Loop update
Action
true
Condition
Loop update
false
Pretest loop
Posttest loop
  • Two general categories of loops
  • event-controlled loop - this is where an event
    will stop the loop from executing (e.g. reading
    the end of a file.)
  • counter-controlled (conditional) loop - this is
    used if the number of times an action to be
    repeated is known in advance.

3
Repetition
Loops in C There are three loop statements in C
the while, the for, and the dowhile. The first
two are pretest loops and the dowhile is a
posttest loop. All of them can be used for
even-controlled and counter-controlled loops.
The while and dowhile are most commonly used for
event-controlled loops and the for is usually
used for counter-controlled loops. All three of
these loop constructs continue looping when the
condition is true and terminate when it is false.

The while loop The while loop uses an expression
to control the loop. Since it is a pretest loop,
it tests the expression before every iteration of
the loop. Syntax
while (expression) statement
no semicolon
false
expression
true
statements
If the expression evaluates to true ( any nonzero
value) the statements are executed and if it
evaluates to false ( 0 ) the loop body is skipped
and execution continues after the loop construct.
4
Repetition
Example of the while statement
loop repetition condition count_emp is tested
before the start of each loop repetition. If the
condition is true the loop body is executed again
loop control variable initialized
before entering while statement
count_emp 0 while( count_emp lt 7)
printf(Enter Hours gt) scanf(d,
hours) printf(Enter rate gt) scanf(lf,
rate) pay hours rate printf(Pay is
6.2f\n, pay) count_emp count_emp
1 printf(\nAll employees processed\n)
loop body executed once for each value of
count_emp that is less than 7
control variable is incremented ( updated )
in the body of the loop
when count_emp becomes 7, the condition
evaluates to false and control passes to the
statement that follows the loop body
5
Repetition
The for loop The for statement is a pretest loop
that uses three expressions. The first
expression contains any initialization
statements, the second contains the terminating
expression (test ) and the third contains the
updating expression. The for loop is used when
your loop is to be executed a known number of
times. You can do the same thing with a while
loop. But a for loop is easier to read and more
natural for counting loops. Syntax
for ( expr 1 expr 2 expr 3
) statement
expr1
false
expr3
expr2
true
expr 1
statement
false
expr 2
true
statement
expr 3
Flowchart
Expanded flowchart
6
Repetition
2nd
Example of the for statement
semicolons
total_pay 0.0 for( count_emp 0
count_emp lt number_emp count_emp
) printf(Enter Hours
gt) scanf(d, hours) printf(Enter rate
gt) scanf(lf, rate) pay hours
rate printf(Pay is 6.2f\n,
pay) total_pay total_pay
pay printf(\nAll employees
processed\n) printf(Total payroll is
8.2f\n, total_pay)
exp 2 loop repetition condition count_emp
is tested before the start of each
loop repetition if the condition is true the
loop body is executed again if the condition
is false, the loop is exited and the next
program statement after the for statement is
executed
1st
exp 1 initialization expression -
initializes the loop control variable - only
done once
4th
expr 3 update expression the value of the
control variable is changed and then the
repetition condition is tested again
3rd
loop body if the condition is true, the
statements in the loop body are executed. After
the last statement in the loop body is executed,
the update expression is executed.
7
Repetition
Example 2 of the for statement
8
Repetition
The dowhile loop The dowhile statement is a
posttest loop. Like the while and for loops, it
also uses an expression to control the loop, but
it tests the expression after the execution of
the body. Syntax
do statement while (expression)
statements
semicolon
expression
true
false
9
Repetition
Example of the dowhile statement
10
Repetition
Results while loop 1 2 3 4 5 6 7 8 9
10 Loop Count 11 Number of tests
11 dowhile loop 1 2 3 4 5 6 7 8 9
10 Loop Count 11 Number of tests 10
The Comma expression A comma expression is a
complex expression made up of two expressions
separated by commas. Although it can be used in
many places, it is generally used only in for
statements. The expressions are evaluated left
to right. The value and type of the expression
are the value and type of the right expression -
the other expression is included for its side
effect. The comma expression has the lowest
priority of all expressions, priority 1.
Example for ( sum 0, i 1 i lt 20
i ) scanf ( d, a ) sum a
Comma expressions can be nested. When
they are, all expression values other than the
last are discarded.
11
Repetition
The nested Loop A nested loop is a loop within a
loop. Within each iteration of the first or
outer loop and inner loop is executed one or more
times. Example include ltstdio.hgt void
main(void) int i, j, k, m 0 for
( i 1 i lt 5 i 2 ) for ( j 1 j lt
4 j ) k i j printf( i
3d, j 3d, k 3d\n, i, j, k) m
k i Output i 1, j 1, k 2 i
1, j 2, k 3 i 1, j 3, k 4 i 1,
j 4, k 5 i 3, j 1, k 4 i 3, j
2, k 5 i 3, j 3, k 6 i 3, j 4, k
7 i 5, j 1, k 6 i 5, j 2, k 7 i
5, j 3, k 8 i 5, j 4, k 9
12
Repetition
Other statements related to looping ( the break
continue) The break statement causes a loop to
terminate. It is the same as setting the loops
limit test to false. break can be used in any of
the loop statements - while, for and dowhile and
in the selection switch statement. Good
structured programming limits its use to the
switch statement. The continue statement does
not terminate the loop, but simply transfers to
the testing statement in the while and dowhile
statements and transfers to the updating
expression in a for statement. The use of the
continue statement is also considered
unstructured programming.
Write a Comment
User Comments (0)
About PowerShow.com