0% found this document useful (0 votes)
88 views36 pages

CSE-141: Structured Programming CSE-141: Structured Programming

This document provides an overview of repetition structures (looping) in C programming. It discusses the while, for, and do-while loops. The while loop repeats as long as a condition is true. The for loop is a natural counting loop that allows initialization, condition testing, and increment/decrement to be specified within the loop header. The do-while loop guarantees that the body is executed at least once before checking the condition. Examples are provided to illustrate each loop type. Common pitfalls like infinite loops are also discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views36 pages

CSE-141: Structured Programming CSE-141: Structured Programming

This document provides an overview of repetition structures (looping) in C programming. It discusses the while, for, and do-while loops. The while loop repeats as long as a condition is true. The for loop is a natural counting loop that allows initialization, condition testing, and increment/decrement to be specified within the loop header. The do-while loop guarantees that the body is executed at least once before checking the condition. Examples are provided to illustrate each loop type. Common pitfalls like infinite loops are also discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

CSE-141: Structured Programming

 Lecture 10-15: Repetitions (Looping)


Instructor
Md. Sabir Hossain
Lecturer
Dept. of CSE, CUET
Mobile: 01737143868/01882826575
Email: [email protected]

Acknowledgment: Most of the content of this slide is adopted from different books and internet
Recap - Decision Making

..
g

, Just
in
m

Freelancer
m
ra

g
g

n
o
Pr

h i
+

o No t
G PA
C

D
Only CGPA

Only Programming

2
Recap - Decision Making
Algorithms
Rules to Write Pseudo code
Example
Flow-Chart
Rules to Draw a flow-chart
Example
If-Else
Switch
Break
Today’s Target
Example 1

 What if we want to process


three different pairs of integers?

5
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? ….

6
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

7
Practical Implementation of Loop

http://www.universitetsavisa.no/student/article38766.ece
http://elitetrack.com/practice-maker-repetition/
http://studio73.com/design-intuition-4-reasons/

Repetitio
n in
Practical
http://
https://www.flickr.com/photos/
praesentire/873139106 andnowpresenting.typepad.com/
professionally_speaking/2013/04
Hallucination
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

12
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

13
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

14
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

15  Multiple statements
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

16
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

17
Example 3

EXECUTION CHART
count count<5 repetition
1 true 1
2 true 2
3 true 3
4 true 4
5 true 5
6 false
18
Loop Pitfalls

Enter value or zero to end: 2

What is wrong with my


program? It just sits there!

19
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

20
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
21
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)

22
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.

23
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.

24
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!

25
2
The for Repetition Structure
 Syntax:

for (initialization; test; increment)


basic block

26
for loop example
 Prints the integers from one to ten

27
for Loop Example

How many times does loop body execute?

28
for Loop Example

How many times does loop body execute?

Bite 1 -- Yum!
Bite 2 -- Yum!
Bite 3 -- Yum!

29
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

30
3
The do-while Repetition Structure
 Syntax:
do
{ stateme
nts
} while
( condition )
;

31
The do-while Repetition Structure
 Example

Prints the integers from 1 to


10

32
The do-while Repetition Structure
 Example

 Makes sure that the user enters a valid weight

33
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!

34
The continue Statement
 continue
Control passes to the next iteration
We will not use the continue statement!

35
Allah Hafez

You might also like