SlideShare a Scribd company logo
2
Most read
4
Most read
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)

More Related Content

PPT
7.data types in c#
PPS
Wrapper class
PPTX
Control Structure in JavaScript (1).pptx
PPTX
Types of Statements in Python Programming Language
ODP
Introduction to programming with python
PPTX
Python-Classes.pptx
PPT
Desktop environment
PDF
Wrapper classes
7.data types in c#
Wrapper class
Control Structure in JavaScript (1).pptx
Types of Statements in Python Programming Language
Introduction to programming with python
Python-Classes.pptx
Desktop environment
Wrapper classes

What's hot (20)

PDF
Learn C# Programming - Data Types & Type Conversion
PDF
Java threading
PDF
Date and Time Module in Python | Edureka
PPTX
C++ language basic
PDF
Python Flow Control
PPTX
Python Libraries and Modules
PPTX
Presentation on C++ Programming Language
PPT
Wrapper class (130240116056)
PPTX
functions of C++
PPTX
python conditional statement.pptx
PPTX
Loop(for, while, do while) condition Presentation
PPT
Server Side Technologies
PPTX
FLOW OF CONTROL-NESTED IFS IN PYTHON
PDF
Datatypes in python
PDF
Python Generators
PPTX
Functions in Python
PPTX
Functions in Python
PPTX
Regular expressions in Python
PPTX
PART 1 - Python Tutorial | Variables and Data Types in Python
Learn C# Programming - Data Types & Type Conversion
Java threading
Date and Time Module in Python | Edureka
C++ language basic
Python Flow Control
Python Libraries and Modules
Presentation on C++ Programming Language
Wrapper class (130240116056)
functions of C++
python conditional statement.pptx
Loop(for, while, do while) condition Presentation
Server Side Technologies
FLOW OF CONTROL-NESTED IFS IN PYTHON
Datatypes in python
Python Generators
Functions in Python
Functions in Python
Regular expressions in Python
PART 1 - Python Tutorial | Variables and Data Types in Python
Ad

Similar to Introduction to Repetition Structures (20)

PDF
Repetition, Basic loop structures, Loop programming techniques
PDF
ICP - Lecture 9
PPT
Fundamentals of Programming Chapter 7
PDF
4-Loops computer programming in c btech.pdf
PPTX
For Loops and Nesting in Python
PPT
Conditional Loops Python
PPT
Mesics lecture 7 iteration and repetitive executions
PPTX
PPTX
presentation on powerpoint template.pptx
PPT
Chapter06.PPT
PPT
Visula C# Programming Lecture 4
PPTX
For Loop In C Programming (Programming Language I).pptx
PPTX
COM1407: Program Control Structures – Repetition and Loops
PPT
Loops Do While Arduino Programming Robotics
PPTX
Python programming –part 3
PPTX
python ppt.pptx
PPT
Chapter05
PDF
Loop and while Loop
PDF
From Beginner to Developer: Understanding Loops in Python
Repetition, Basic loop structures, Loop programming techniques
ICP - Lecture 9
Fundamentals of Programming Chapter 7
4-Loops computer programming in c btech.pdf
For Loops and Nesting in Python
Conditional Loops Python
Mesics lecture 7 iteration and repetitive executions
presentation on powerpoint template.pptx
Chapter06.PPT
Visula C# Programming Lecture 4
For Loop In C Programming (Programming Language I).pptx
COM1407: Program Control Structures – Repetition and Loops
Loops Do While Arduino Programming Robotics
Python programming –part 3
python ppt.pptx
Chapter05
Loop and while Loop
From Beginner to Developer: Understanding Loops in Python
Ad

More from primeteacher32 (20)

PPT
Software Development Life Cycle
PPTX
Variable Scope
PPTX
Returning Data
PPTX
Intro to Functions
PPTX
Introduction to GUIs with guizero
PPTX
Function Parameters
PPTX
Nested Loops
PPT
Conditional Loops
PPTX
Input Validation
PPTX
Windows File Systems
PPTX
Nesting Conditionals
PPTX
Conditionals
PPT
Intro to Python with GPIO
PPTX
Variables and Statements
PPTX
Variables and User Input
PPT
Intro to Python
PPTX
Raspberry Pi
PPT
Hardware vs. Software Presentations
PPTX
Block chain security
PPTX
Software Development Life Cycle
Variable Scope
Returning Data
Intro to Functions
Introduction to GUIs with guizero
Function Parameters
Nested Loops
Conditional Loops
Input Validation
Windows File Systems
Nesting Conditionals
Conditionals
Intro to Python with GPIO
Variables and Statements
Variables and User Input
Intro to Python
Raspberry Pi
Hardware vs. Software Presentations
Block chain security

Recently uploaded (20)

PPTX
chapter 3_bem.pptxKLJLKJLKJLKJKJKLJKJKJKHJH
PDF
Why Today’s Brands Need ORM & SEO Specialists More Than Ever.pdf
PPTX
Overview Planner of Soft Skills in a single ppt
PDF
Sales and Distribution Managemnjnfijient.pdf
PPTX
cse couse aefrfrqewrbqwrgbqgvq2w3vqbvq23rbgw3rnw345
DOC
field study for teachers graduating samplr
PPTX
ESD MODULE-5hdbdhbdbdbdbbdbdbbdndbdbdbdbbdbd
PPTX
OnePlus 13R – ⚡ All-Rounder King Performance: Snapdragon 8 Gen 3 – same as iQ...
PPTX
Discovering the LMA Course by Tim Han.pptx
PPT
APPROACH TO DEVELOPMENTALlllllllllllllllll
PDF
Biography of Mohammad Anamul Haque Nayan
PDF
Blue-Modern-Elegant-Presentation (1).pdf
PPTX
microtomy kkk. presenting to cryst in gl
PDF
シュアーイノベーション採用ピッチ資料|Company Introduction & Recruiting Deck
PDF
MCQ Practice CBT OL Official Language 1.pptx.pdf
PPTX
Your Guide to a Winning Interview Aug 2025.
PPT
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
PDF
L-0018048598visual cloud book for PCa-pdf.pdf
PPTX
Condensed_Food_Science_Lecture1_Precised.pptx
PPTX
1751884730-Visual Basic -Unitj CS B.pptx
chapter 3_bem.pptxKLJLKJLKJLKJKJKLJKJKJKHJH
Why Today’s Brands Need ORM & SEO Specialists More Than Ever.pdf
Overview Planner of Soft Skills in a single ppt
Sales and Distribution Managemnjnfijient.pdf
cse couse aefrfrqewrbqwrgbqgvq2w3vqbvq23rbgw3rnw345
field study for teachers graduating samplr
ESD MODULE-5hdbdhbdbdbdbbdbdbbdndbdbdbdbbdbd
OnePlus 13R – ⚡ All-Rounder King Performance: Snapdragon 8 Gen 3 – same as iQ...
Discovering the LMA Course by Tim Han.pptx
APPROACH TO DEVELOPMENTALlllllllllllllllll
Biography of Mohammad Anamul Haque Nayan
Blue-Modern-Elegant-Presentation (1).pdf
microtomy kkk. presenting to cryst in gl
シュアーイノベーション採用ピッチ資料|Company Introduction & Recruiting Deck
MCQ Practice CBT OL Official Language 1.pptx.pdf
Your Guide to a Winning Interview Aug 2025.
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
L-0018048598visual cloud book for PCa-pdf.pdf
Condensed_Food_Science_Lecture1_Precised.pptx
1751884730-Visual Basic -Unitj CS B.pptx

Introduction to Repetition Structures

  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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?
  • 6. 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
  • 7. 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
  • 8. 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”)
  • 9. 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
  • 10. 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
  • 11. 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)