KEMBAR78
Introduction to Repetition Structures | PPTX
INTRODUCTION TO REPETITION
STRUCTURES
LOOPING STRUCTURES
• An application sometimes needs to perform repetitive actions
like:
• Summing Numbers(Averages)
• Entering Multiple Data Entries
• Prompting the User Till the Correct Value is Entered
• A shopping list(scanning through the shopping list and
getting the items 1 by one until the list is down and then you
leave)
• Waiting for a user to press a button
• Simplifying blinking an LED
LOOPING STRUCTURES
• When a program repeatedly runs a set of statements it is
referred to as a loop, iteration or repetition structure.
• There are several types of looping structures:
For Loops
While Loops
Do-While Loops
Counter Controlled Loops
Conditional Loops
LOOPING STRUCTURES
• Loops are similar to conditionals because they run on a true/false
value set. The loop continuously runs while the condition is true
and terminates when it is false.
• Loops can be run for a desired length or until a flag terminates
them
• Great technique to reuse code and therefore limit the amount of
statements a program needs. Reusing the same conditional
arguments for testing instead of posting hundreds.
True False
EXAMPLE
• Question: How many statements are needed for the following?
• Ask the same question 100 times
• Store the answer in a variable
• Check whether the answer is one of two values
• Question: How many statements are needed if we loop the code?
FOR LOOPS
 The for loop is designed to increment a counter variable over a
range of values.
 It is ideally suited for problems requiring a loop that iterates a specific
number of times (counter controlled).
 Loop through a directory or a set of files
 The number of times a loop can execute can be as few as one, which
seems pointless, to an expression, like x.
• For loops are a pre-test loop, meaning they check their condition before
execution
• Reasons to use a for loop:
• Know the number of times the loop should iterate
• Using a counter
• Need a false condition to terminate the loop
CODE
• In order to utilize a for loop you need 3 things:
1. Needs to initialize a counter
2. Must test the counter variable
1. Less than if start < stop
2. Greater than if start > stop
3. Update the counter variable
• Code:
for initialization in range(start, stop, increment):
statement1
statement2
EXAMPLE
Step 1: Perform the initialization
expression
Step 2: Evaluate the test expressions
Step 3: Execute the
body of the loop
Step 4: Perform the
update
Assign 0 to i
i < 5
Update iPrint “Hello”
True
False
for i in range(0, 5, 1):
print(“Hello”)
COUNTER CONTROLLED LOOP
Set index to 0
Index < 12? Get the time
If they match
then display the
time
Add 1 to index
4.4
OTHER WAYS TO UPDATE
• Most programmers and books refer to the update expression as the
incrementor
• Not only can you increment the expression but decrement as well
• Additionally you can increase and decrease the variable by values other
than 1
• i++ i+=1 i = i + 1
• i-- i -=1 i = i - 1
• i+=2
• i -=5
CHECKPOINT:
1. Name the three expressions that appear inside the
parentheses(header) of the for loop.
2. You want to write a for loop that displays “I love to program!” 50
times.
A. What initialization expression will you use?
B. What test expression will you use?
C. What update expression will you use?
D. Write the loop
3. What will the following code segments produce:
A. for count in range(0, 6, 1):
print(count + count)
B. for j in range(20, 14, -2):
print(j)

Introduction to Repetition Structures

  • 1.
  • 2.
    LOOPING STRUCTURES • Anapplication sometimes needs to perform repetitive actions like: • Summing Numbers(Averages) • Entering Multiple Data Entries • Prompting the User Till the Correct Value is Entered • A shopping list(scanning through the shopping list and getting the items 1 by one until the list is down and then you leave) • Waiting for a user to press a button • Simplifying blinking an LED
  • 3.
    LOOPING STRUCTURES • Whena program repeatedly runs a set of statements it is referred to as a loop, iteration or repetition structure. • There are several types of looping structures: For Loops While Loops Do-While Loops Counter Controlled Loops Conditional Loops
  • 4.
    LOOPING STRUCTURES • Loopsare similar to conditionals because they run on a true/false value set. The loop continuously runs while the condition is true and terminates when it is false. • Loops can be run for a desired length or until a flag terminates them • Great technique to reuse code and therefore limit the amount of statements a program needs. Reusing the same conditional arguments for testing instead of posting hundreds. True False
  • 5.
    EXAMPLE • Question: Howmany statements are needed for the following? • Ask the same question 100 times • Store the answer in a variable • Check whether the answer is one of two values • Question: How many statements are needed if we loop the code?
  • 6.
    FOR LOOPS  Thefor loop is designed to increment a counter variable over a range of values.  It is ideally suited for problems requiring a loop that iterates a specific number of times (counter controlled).  Loop through a directory or a set of files  The number of times a loop can execute can be as few as one, which seems pointless, to an expression, like x. • For loops are a pre-test loop, meaning they check their condition before execution • Reasons to use a for loop: • Know the number of times the loop should iterate • Using a counter • Need a false condition to terminate the loop
  • 7.
    CODE • In orderto utilize a for loop you need 3 things: 1. Needs to initialize a counter 2. Must test the counter variable 1. Less than if start < stop 2. Greater than if start > stop 3. Update the counter variable • Code: for initialization in range(start, stop, increment): statement1 statement2
  • 8.
    EXAMPLE Step 1: Performthe initialization expression Step 2: Evaluate the test expressions Step 3: Execute the body of the loop Step 4: Perform the update Assign 0 to i i < 5 Update iPrint “Hello” True False for i in range(0, 5, 1): print(“Hello”)
  • 9.
    COUNTER CONTROLLED LOOP Setindex to 0 Index < 12? Get the time If they match then display the time Add 1 to index 4.4
  • 10.
    OTHER WAYS TOUPDATE • Most programmers and books refer to the update expression as the incrementor • Not only can you increment the expression but decrement as well • Additionally you can increase and decrease the variable by values other than 1 • i++ i+=1 i = i + 1 • i-- i -=1 i = i - 1 • i+=2 • i -=5
  • 11.
    CHECKPOINT: 1. Name thethree expressions that appear inside the parentheses(header) of the for loop. 2. You want to write a for loop that displays “I love to program!” 50 times. A. What initialization expression will you use? B. What test expression will you use? C. What update expression will you use? D. Write the loop 3. What will the following code segments produce: A. for count in range(0, 6, 1): print(count + count) B. for j in range(20, 14, -2): print(j)