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

PPTX
Operators and expressions in c language
PPT
Boolean Algebra DLD
PPT
PDF
15 bitwise operators
PPT
Digital Logic Design
PPT
Introduction to Compiler Construction
PPTX
Computer arithmetic
PPTX
Number system conversion
Operators and expressions in c language
Boolean Algebra DLD
15 bitwise operators
Digital Logic Design
Introduction to Compiler Construction
Computer arithmetic
Number system conversion

What's hot (20)

PPTX
dld 01-introduction
PPT
Basics of c++ Programming Language
PPTX
UNIT-1 CSA.pptx
PPTX
Visual Programming
PPT
Representation of Negative Numbers
PPTX
Register transfer language
PPT
Infix prefix postfix
PPT
PPT
Boolean Algebra
PPTX
Stack data structure
PPT
introduction computer programming languages
PPTX
Polish Notation In Data Structure
PDF
Boolean algebra and Logic gates
PDF
Binary codes
PPTX
FIT-Unit3 chapter2- Computer Languages
PPTX
array of object pointer in c++
PPTX
Presentation on function
PPT
Pointers C programming
PPT
Compiler Design Unit 1
PPTX
Compiler an overview
dld 01-introduction
Basics of c++ Programming Language
UNIT-1 CSA.pptx
Visual Programming
Representation of Negative Numbers
Register transfer language
Infix prefix postfix
Boolean Algebra
Stack data structure
introduction computer programming languages
Polish Notation In Data Structure
Boolean algebra and Logic gates
Binary codes
FIT-Unit3 chapter2- Computer Languages
array of object pointer in c++
Presentation on function
Pointers C programming
Compiler Design Unit 1
Compiler an overview
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
PDF
Repetition Control Structure_lsson 3.pdf
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
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
Repetition Control Structure_lsson 3.pdf
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
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
Unit 3 Presentation Etiquette Business and Corporate Etiquette
PDF
Basic GMP - Training good manufacturing procedure
PDF
BPT_Beach_Energy_FY25_half_year_results_presentation.pdf
PDF
Beginner’s Guide to Digital Marketing.pdf
PPTX
Opioid_Analgesics_Presentation (1).pptxp
PPTX
430838499-Anaesthesiiiia-Equipmenooot.pptx
PPTX
D1basicstoloopscppforbeginnersgodoit.pptx
PPTX
employee on boarding for jobs for freshers try it
PPT
444174684-Welding-Presentatiohhhn-ppt.ppt
PDF
Sheri Ann Lowe Compliance Strategist Resume
PDF
Acne presentation and homeopathy treatment
PPTX
Core Characteristics and Abilities of an Effective Teacher_0.pptx
PDF
Women’s Talk Session 1- Talking about women
PDF
Environmental-social-and-governance-report.pdf
PPTX
ANN DL UNIT 1 ANIL 13.10.24.pptxcccccccccc
PPTX
ChandigarhUniversityinformationcareer.pptx
PPT
Woman as Engineer and Technicians in the field of Clinical & Biomedical Engin...
PPT
notes_Lecture2 23l3j2 dfjl dfdlkj d 2.ppt
PPT
NO000387 (1).pptsbsnsnsnsnsnsnsmsnnsnsnsjsnnsnsnsnnsnnansnwjwnshshshs
PPTX
Unit 1- Introduction to Corporate Etiquettes
Unit 3 Presentation Etiquette Business and Corporate Etiquette
Basic GMP - Training good manufacturing procedure
BPT_Beach_Energy_FY25_half_year_results_presentation.pdf
Beginner’s Guide to Digital Marketing.pdf
Opioid_Analgesics_Presentation (1).pptxp
430838499-Anaesthesiiiia-Equipmenooot.pptx
D1basicstoloopscppforbeginnersgodoit.pptx
employee on boarding for jobs for freshers try it
444174684-Welding-Presentatiohhhn-ppt.ppt
Sheri Ann Lowe Compliance Strategist Resume
Acne presentation and homeopathy treatment
Core Characteristics and Abilities of an Effective Teacher_0.pptx
Women’s Talk Session 1- Talking about women
Environmental-social-and-governance-report.pdf
ANN DL UNIT 1 ANIL 13.10.24.pptxcccccccccc
ChandigarhUniversityinformationcareer.pptx
Woman as Engineer and Technicians in the field of Clinical & Biomedical Engin...
notes_Lecture2 23l3j2 dfjl dfdlkj d 2.ppt
NO000387 (1).pptsbsnsnsnsnsnsnsmsnnsnsnsjsnnsnsnsnnsnnansnwjwnshshshs
Unit 1- Introduction to Corporate Etiquettes

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)