CSE-1203
Structured Programming Language
Lecture 6
Repetition/Loops Part-I
Md. Rafsan Jani
Assistant Professor
Department of Computer Science and Engineering
Jahangirnagar University
Outline
Iterations in C
– while statement
– for statement
– do…while statement
– Infinity Loop
– Comparison
January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 2
Loop
 You may encounter situations, when a block of code needs to
be executed several number of times. In general, statements
are executed sequentially: The first statement in a function is
executed first, followed by the second, and so on.
 Programming languages provide various control structures
that allow for more complicated execution paths.
 A loop statement allows us to execute a statement or group
of statements multiple times.
January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 3
Example 1
 What if we want to process
three different pairs of integers?
January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 4
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? ….
January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 5
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
January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 6
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
 Most flexible
 No ‘restrictions’
for statement
 Natural ‘counting’ loop
do-while statement
 Always executes body at least once
1
2
3
January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 7
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
January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 8
The while Repetition Structure
• The condition is
tested
• If the condition is
true, the loop body
is executed and the
condition is
retested.
• When the condition
is false, the loop is
exited.
condition
loop body
false
true
next stmt
January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 9
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
1
January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 10

CSE-1203-Lecture-06-Loops-Part-I (1).pptx

  • 1.
    CSE-1203 Structured Programming Language Lecture6 Repetition/Loops Part-I Md. Rafsan Jani Assistant Professor Department of Computer Science and Engineering Jahangirnagar University
  • 2.
    Outline Iterations in C –while statement – for statement – do…while statement – Infinity Loop – Comparison January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 2
  • 3.
    Loop  You mayencounter situations, when a block of code needs to be executed several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.  Programming languages provide various control structures that allow for more complicated execution paths.  A loop statement allows us to execute a statement or group of statements multiple times. January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 3
  • 4.
    Example 1  Whatif we want to process three different pairs of integers? January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 4
  • 5.
    Example 2  Onesolution is to copy and paste the necessary lines of code. Consider the following modification:  What if you wanted to process four sets? Five? Six? …. January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 5
  • 6.
    Processing an arbitrarynumber 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 January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 6
  • 7.
    Repetition (Looping)   Uselooping 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  Most flexible  No ‘restrictions’ for statement  Natural ‘counting’ loop do-while statement  Always executes body at least once 1 2 3 January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 7
  • 8.
    The while RepetitionStructure  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 January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 8
  • 9.
    The while RepetitionStructure • The condition is tested • If the condition is true, the loop body is executed and the condition is retested. • When the condition is false, the loop is exited. condition loop body false true next stmt January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 9
  • 10.
    The while RepetitionStructure  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 1 January 12, 2025 Md. Rafsan Jani, Assistant Professor, Dept. of CSE, JU 10