SlideShare a Scribd company logo
1 of 9
Nested Loops
Nested Loops
 A nested loop is a loop inside the body of another loop
 Inner (inside), outer (outside) loops:
for row in range(1, 3, 1): //outer
for col in range(1, 3, 1)://inner
print(row * col)
 Inner loop goes through all repetitions for each repetition of
outer loop
 Inner loop repetitions complete sooner than outer loop
 Total number of repetitions for inner loop is product of number
of repetitions of the two loops.
 Can nest different styles of loops together
Nesting Loops
 Nested loops are necessary when a task is performs a repetitive
operation and that task itself must be repeated.
 for initialize in range(start, stop, update): while(condition):
for initialize in range(start, stop, update):
while(condition):
 Just like when we worked with nested conditionals
 if (condition):
if (condition):
 For example, a digital clock is a good example of needing to nest
for loops.
Digital Clock
 How can we use loops to simulate a digital clock?
 What would each loop be responsible for keeping track of?
 There would be 3 loops:
 One would track the hour
 One would track the minutes
 One would track the seconds
 NOT AN ANALOG CLOCK, why??????
 What would it look like?
Digital Clock
 First let’s begin by creating a loop to display the seconds up to
a minute.
 Can anyone describe that loop???
for sec in range(0, 60, 1):
print(format(sec, ‘>2’)
 Question: What is the initialization, test and update for the
previous for loop?
 Question: What would it look like if we printed to the left with
setw(2)?
Digital Clock
 Now let’s add minutes by nesting our previous loop:
for min in range(0, 60, 1):
for sec in range(0, 60, 1):
print(format(min, ‘>2’), ”:”, format(sec, ‘>2’)
Digital Clock
for hour in range(0, 12, 1):
for min in range(0, 60, 1):
for sec in range(0, 60, 1):
print(format(hour, ‘>2’), ”:”, format(min, ‘>2’), ”:”, format(sec, ‘>2’)
 Question: What if we wanted to add AM/PM
Digital Clock
Assign 0 to
hour
hour <
12
Assign 0 to
sec
Assign 0 to
min
True
False
min <
60
False
True
sec <
60
Fals
True Print clock
Infinite Loops
 Loops must contain a way to terminate otherwise the loop will
continue to repeat until the program is interrupted.
 Infinite loops usually occur when the programmer forgets to write
code inside the loop making the Boolean expression false.

More Related Content

Similar to Nested Loops

Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
Karwan Mustafa Kareem
 
Introducing MERLIN_3.0.pptx
Introducing MERLIN_3.0.pptxIntroducing MERLIN_3.0.pptx
Introducing MERLIN_3.0.pptx
ssuser716de5
 
Generating Automated and Online Test Oracles for Simulink Models with Continu...
Generating Automated and Online Test Oracles for Simulink Models with Continu...Generating Automated and Online Test Oracles for Simulink Models with Continu...
Generating Automated and Online Test Oracles for Simulink Models with Continu...
Lionel Briand
 
Notes2
Notes2Notes2
Notes2
hccit
 
vibration of machines and structures
vibration of machines and structuresvibration of machines and structures
vibration of machines and structures
Aniruddhsinh Barad
 
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAlgorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
AliaaAqilah3
 
Introduction to writing algorithms
Introduction to writing algorithmsIntroduction to writing algorithms
Introduction to writing algorithms
Krishna Chaytaniah
 
OSNoise Tracer: Who Is Stealing My CPU Time?
OSNoise Tracer: Who Is Stealing My CPU Time?OSNoise Tracer: Who Is Stealing My CPU Time?
OSNoise Tracer: Who Is Stealing My CPU Time?
ScyllaDB
 

Similar to Nested Loops (20)

Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
 
Common clock path pessimism removal
Common clock path pessimism removalCommon clock path pessimism removal
Common clock path pessimism removal
 
Time brings all things to pass
Time brings all things to passTime brings all things to pass
Time brings all things to pass
 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
 
Introducing MERLIN_3.0.pptx
Introducing MERLIN_3.0.pptxIntroducing MERLIN_3.0.pptx
Introducing MERLIN_3.0.pptx
 
Storm
StormStorm
Storm
 
Note 0
Note 0Note 0
Note 0
 
The Ring programming language version 1.8 book - Part 84 of 202
The Ring programming language version 1.8 book - Part 84 of 202The Ring programming language version 1.8 book - Part 84 of 202
The Ring programming language version 1.8 book - Part 84 of 202
 
Becoming a better developer with EXPLAIN
Becoming a better developer with EXPLAINBecoming a better developer with EXPLAIN
Becoming a better developer with EXPLAIN
 
Generating Automated and Online Test Oracles for Simulink Models with Continu...
Generating Automated and Online Test Oracles for Simulink Models with Continu...Generating Automated and Online Test Oracles for Simulink Models with Continu...
Generating Automated and Online Test Oracles for Simulink Models with Continu...
 
UNIT I- Session 3.pptx
UNIT I- Session 3.pptxUNIT I- Session 3.pptx
UNIT I- Session 3.pptx
 
Bb2
Bb2Bb2
Bb2
 
class12_time.ppt
class12_time.pptclass12_time.ppt
class12_time.ppt
 
Notes2
Notes2Notes2
Notes2
 
vibration of machines and structures
vibration of machines and structuresvibration of machines and structures
vibration of machines and structures
 
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAlgorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
 
Virtual post rig
Virtual post rigVirtual post rig
Virtual post rig
 
Introduction to writing algorithms
Introduction to writing algorithmsIntroduction to writing algorithms
Introduction to writing algorithms
 
OSNoise Tracer: Who Is Stealing My CPU Time?
OSNoise Tracer: Who Is Stealing My CPU Time?OSNoise Tracer: Who Is Stealing My CPU Time?
OSNoise Tracer: Who Is Stealing My CPU Time?
 
Writing Faster Python 3
Writing Faster Python 3Writing Faster Python 3
Writing Faster Python 3
 

More from primeteacher32

More from primeteacher32 (20)

Software Development Life Cycle
Software Development Life CycleSoftware Development Life Cycle
Software Development Life Cycle
 
Variable Scope
Variable ScopeVariable Scope
Variable Scope
 
Returning Data
Returning DataReturning Data
Returning Data
 
Intro to Functions
Intro to FunctionsIntro to Functions
Intro to Functions
 
Introduction to GUIs with guizero
Introduction to GUIs with guizeroIntroduction to GUIs with guizero
Introduction to GUIs with guizero
 
Function Parameters
Function ParametersFunction Parameters
Function Parameters
 
Conditional Loops
Conditional LoopsConditional Loops
Conditional Loops
 
Introduction to Repetition Structures
Introduction to Repetition StructuresIntroduction to Repetition Structures
Introduction to Repetition Structures
 
Input Validation
Input ValidationInput Validation
Input Validation
 
Windows File Systems
Windows File SystemsWindows File Systems
Windows File Systems
 
Nesting Conditionals
Nesting ConditionalsNesting Conditionals
Nesting Conditionals
 
Conditionals
ConditionalsConditionals
Conditionals
 
Intro to Python with GPIO
Intro to Python with GPIOIntro to Python with GPIO
Intro to Python with GPIO
 
Variables and Statements
Variables and StatementsVariables and Statements
Variables and Statements
 
Variables and User Input
Variables and User InputVariables and User Input
Variables and User Input
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Raspberry Pi
Raspberry PiRaspberry Pi
Raspberry Pi
 
Hardware vs. Software Presentations
Hardware vs. Software PresentationsHardware vs. Software Presentations
Hardware vs. Software Presentations
 
Block chain security
Block chain securityBlock chain security
Block chain security
 
Hashes
HashesHashes
Hashes
 

Recently uploaded

怎样办理伊利诺伊大学厄巴纳-香槟分校毕业证(UIUC毕业证书)成绩单学校原版复制
怎样办理伊利诺伊大学厄巴纳-香槟分校毕业证(UIUC毕业证书)成绩单学校原版复制怎样办理伊利诺伊大学厄巴纳-香槟分校毕业证(UIUC毕业证书)成绩单学校原版复制
怎样办理伊利诺伊大学厄巴纳-香槟分校毕业证(UIUC毕业证书)成绩单学校原版复制
yynod
 
Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...
Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...
Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...
ZurliaSoop
 
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Menggugurkan Kandungan 087776558899
 
Top profile Call Girls In daman [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In daman [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In daman [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In daman [ 7014168258 ] Call Me For Genuine Models We ...
gajnagarg
 
Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...
Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...
Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...
gajnagarg
 
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
gynedubai
 
Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...
nirzagarg
 
一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证
一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证
一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证
eqaqen
 
Girls in Aiims Metro (delhi) call me [🔝9953056974🔝] escort service 24X7
Girls in Aiims Metro (delhi) call me [🔝9953056974🔝] escort service 24X7Girls in Aiims Metro (delhi) call me [🔝9953056974🔝] escort service 24X7
Girls in Aiims Metro (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
b-sc-agri-course-curriculum.pdf for Karnataka state board
b-sc-agri-course-curriculum.pdf for Karnataka state boardb-sc-agri-course-curriculum.pdf for Karnataka state board
b-sc-agri-course-curriculum.pdf for Karnataka state board
ramyaul734
 
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
gajnagarg
 

Recently uploaded (20)

怎样办理伊利诺伊大学厄巴纳-香槟分校毕业证(UIUC毕业证书)成绩单学校原版复制
怎样办理伊利诺伊大学厄巴纳-香槟分校毕业证(UIUC毕业证书)成绩单学校原版复制怎样办理伊利诺伊大学厄巴纳-香槟分校毕业证(UIUC毕业证书)成绩单学校原版复制
怎样办理伊利诺伊大学厄巴纳-香槟分校毕业证(UIUC毕业证书)成绩单学校原版复制
 
Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...
Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...
Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...
 
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
 
Guide to a Winning Interview May 2024 for MCWN
Guide to a Winning Interview May 2024 for MCWNGuide to a Winning Interview May 2024 for MCWN
Guide to a Winning Interview May 2024 for MCWN
 
Top profile Call Girls In daman [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In daman [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In daman [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In daman [ 7014168258 ] Call Me For Genuine Models We ...
 
Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...
Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...
Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...
 
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
 
Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...
 
Vip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime Malegaon
Vip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime MalegaonVip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime Malegaon
Vip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime Malegaon
 
一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证
一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证
一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证
 
Girls in Aiims Metro (delhi) call me [🔝9953056974🔝] escort service 24X7
Girls in Aiims Metro (delhi) call me [🔝9953056974🔝] escort service 24X7Girls in Aiims Metro (delhi) call me [🔝9953056974🔝] escort service 24X7
Girls in Aiims Metro (delhi) call me [🔝9953056974🔝] escort service 24X7
 
7737669865 Call Girls In Ahmedabad Escort Service Available 24×7 In In Ahmedabad
7737669865 Call Girls In Ahmedabad Escort Service Available 24×7 In In Ahmedabad7737669865 Call Girls In Ahmedabad Escort Service Available 24×7 In In Ahmedabad
7737669865 Call Girls In Ahmedabad Escort Service Available 24×7 In In Ahmedabad
 
DMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdf
DMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdfDMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdf
DMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdf
 
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
 
Joshua Minker Brand Exploration Sports Broadcaster .pptx
Joshua Minker Brand Exploration Sports Broadcaster .pptxJoshua Minker Brand Exploration Sports Broadcaster .pptx
Joshua Minker Brand Exploration Sports Broadcaster .pptx
 
Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...
Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...
Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...
 
b-sc-agri-course-curriculum.pdf for Karnataka state board
b-sc-agri-course-curriculum.pdf for Karnataka state boardb-sc-agri-course-curriculum.pdf for Karnataka state board
b-sc-agri-course-curriculum.pdf for Karnataka state board
 
Personal Brand Exploration ppt.- Ronnie Jones
Personal Brand  Exploration ppt.- Ronnie JonesPersonal Brand  Exploration ppt.- Ronnie Jones
Personal Brand Exploration ppt.- Ronnie Jones
 
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
 
UIowa Application Instructions - 2024 Update
UIowa Application Instructions - 2024 UpdateUIowa Application Instructions - 2024 Update
UIowa Application Instructions - 2024 Update
 

Nested Loops

  • 2. Nested Loops  A nested loop is a loop inside the body of another loop  Inner (inside), outer (outside) loops: for row in range(1, 3, 1): //outer for col in range(1, 3, 1)://inner print(row * col)  Inner loop goes through all repetitions for each repetition of outer loop  Inner loop repetitions complete sooner than outer loop  Total number of repetitions for inner loop is product of number of repetitions of the two loops.  Can nest different styles of loops together
  • 3. Nesting Loops  Nested loops are necessary when a task is performs a repetitive operation and that task itself must be repeated.  for initialize in range(start, stop, update): while(condition): for initialize in range(start, stop, update): while(condition):  Just like when we worked with nested conditionals  if (condition): if (condition):  For example, a digital clock is a good example of needing to nest for loops.
  • 4. Digital Clock  How can we use loops to simulate a digital clock?  What would each loop be responsible for keeping track of?  There would be 3 loops:  One would track the hour  One would track the minutes  One would track the seconds  NOT AN ANALOG CLOCK, why??????  What would it look like?
  • 5. Digital Clock  First let’s begin by creating a loop to display the seconds up to a minute.  Can anyone describe that loop??? for sec in range(0, 60, 1): print(format(sec, ‘>2’)  Question: What is the initialization, test and update for the previous for loop?  Question: What would it look like if we printed to the left with setw(2)?
  • 6. Digital Clock  Now let’s add minutes by nesting our previous loop: for min in range(0, 60, 1): for sec in range(0, 60, 1): print(format(min, ‘>2’), ”:”, format(sec, ‘>2’)
  • 7. Digital Clock for hour in range(0, 12, 1): for min in range(0, 60, 1): for sec in range(0, 60, 1): print(format(hour, ‘>2’), ”:”, format(min, ‘>2’), ”:”, format(sec, ‘>2’)  Question: What if we wanted to add AM/PM
  • 8. Digital Clock Assign 0 to hour hour < 12 Assign 0 to sec Assign 0 to min True False min < 60 False True sec < 60 Fals True Print clock
  • 9. Infinite Loops  Loops must contain a way to terminate otherwise the loop will continue to repeat until the program is interrupted.  Infinite loops usually occur when the programmer forgets to write code inside the loop making the Boolean expression false.