SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Lecture 6
Version 1.0
The for Loop
The break Statement
The continue Statement
Version 1.0
Rushdi Shams, Dept of CSE, KUET, Bangladesh2
The for Loop
 The for loop allows us to specify three
things in a single line:
1. Setting a loop counter to an initial value.
2. Testing the loop counter to determine whether its value has
reached the number of repetitions desired.
3. Increasing/decreasing the value of loop counter each time the
program segment within the loop is executed.
Rushdi Shams, Dept of CSE, KUET, Bangladesh3
General form of for loop
Rushdi Shams, Dept of CSE, KUET, Bangladesh4
Example of for loop
 This program prints number 1 to 10
Rushdi Shams, Dept of CSE, KUET, Bangladesh5
Structure of for loop
Rushdi Shams, Dept of CSE, KUET, Bangladesh6
Variation of for loop
Rushdi Shams, Dept of CSE, KUET, Bangladesh7
Variation of for loop
Rushdi Shams, Dept of CSE, KUET, Bangladesh8
Variation of for loop
Rushdi Shams, Dept of CSE, KUET, Bangladesh9
Variation of for loop: Infinite
loop
Rushdi Shams, Dept of CSE, KUET, Bangladesh10
Observing for loops
The initialization, loop-continuation
condition and increment/decrement can
contain arithmetic expressions. Assume
that x=2 and y=10. The statement-
for(j=x; j<=4*x*y; j=j+y/x)
is equivalent to-
for(j=2; j<=80; j=j+5)
Rushdi Shams, Dept of CSE, KUET, Bangladesh11
Observing for loops
There can be a decrement as well (or you
can say a negative increment). For
example-
Rushdi Shams, Dept of CSE, KUET, Bangladesh12
Observing for loops
If the loop continuation condition is initially
false, the body portion of the loop is not
performed.
Rushdi Shams, Dept of CSE, KUET, Bangladesh13
Observing for loops
Rushdi Shams, Dept of CSE, KUET, Bangladesh14
Qs
Rushdi Shams, Dept of CSE, KUET, Bangladesh15
Nested for loops
Rushdi Shams, Dept of CSE, KUET, Bangladesh16
Output
Rushdi Shams, Dept of CSE, KUET, Bangladesh17
What will be the output?
Rushdi Shams, Dept of CSE, KUET, Bangladesh18
What will be the output?
Rushdi Shams, Dept of CSE, KUET, Bangladesh19
The break Statement
 We often come across situations where
we want to jump out of a loop instantly,
without waiting to get back to the
conditional test.
 The keyword break allows us to do
this.
Rushdi Shams, Dept of CSE, KUET, Bangladesh20
The break Statement
 When break is encountered inside any
loop, control automatically passes to the
first statement after the loop.
Rushdi Shams, Dept of CSE, KUET, Bangladesh21
The break Statement
Rushdi Shams, Dept of CSE, KUET, Bangladesh22
The continue Statement
 In some programming situations we
want to take the control to the beginning
of the loop, bypassing the statements
inside the loop, which have not yet
been executed.
Rushdi Shams, Dept of CSE, KUET, Bangladesh23
The continue Statement
 The keyword continue allows us to do
this. When continue is encountered
inside any loop, control automatically
passes to the beginning of the loop.
Rushdi Shams, Dept of CSE, KUET, Bangladesh24
The continue Statement

Weitere ähnliche Inhalte

Ähnlich wie Lec 06. For Loop / Break / Continue

Lec 05. While Loop
Lec 05. While LoopLec 05. While Loop
Lec 05. While Loop
Rushdi Shams
 
Lec 07. Do-While Loop / Switch
Lec 07. Do-While Loop / SwitchLec 07. Do-While Loop / Switch
Lec 07. Do-While Loop / Switch
Rushdi Shams
 
Lecture 7, 8, 9 and 10 Inter Process Communication (IPC) in Operating Systems
Lecture 7, 8, 9 and 10  Inter Process Communication (IPC) in Operating SystemsLecture 7, 8, 9 and 10  Inter Process Communication (IPC) in Operating Systems
Lecture 7, 8, 9 and 10 Inter Process Communication (IPC) in Operating Systems
Rushdi Shams
 
Lec 03. Arithmetic Operator / Relational Operator
Lec 03. Arithmetic Operator / Relational OperatorLec 03. Arithmetic Operator / Relational Operator
Lec 03. Arithmetic Operator / Relational Operator
Rushdi Shams
 
Lec 10. Functions (Part II)
Lec 10. Functions (Part II)Lec 10. Functions (Part II)
Lec 10. Functions (Part II)
Rushdi Shams
 

Ähnlich wie Lec 06. For Loop / Break / Continue (6)

Lec 05. While Loop
Lec 05. While LoopLec 05. While Loop
Lec 05. While Loop
 
Lec 07. Do-While Loop / Switch
Lec 07. Do-While Loop / SwitchLec 07. Do-While Loop / Switch
Lec 07. Do-While Loop / Switch
 
Lecture 7, 8, 9 and 10 Inter Process Communication (IPC) in Operating Systems
Lecture 7, 8, 9 and 10  Inter Process Communication (IPC) in Operating SystemsLecture 7, 8, 9 and 10  Inter Process Communication (IPC) in Operating Systems
Lecture 7, 8, 9 and 10 Inter Process Communication (IPC) in Operating Systems
 
Lec 03. Arithmetic Operator / Relational Operator
Lec 03. Arithmetic Operator / Relational OperatorLec 03. Arithmetic Operator / Relational Operator
Lec 03. Arithmetic Operator / Relational Operator
 
Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 7Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 7
 
Lec 10. Functions (Part II)
Lec 10. Functions (Part II)Lec 10. Functions (Part II)
Lec 10. Functions (Part II)
 

Mehr von Rushdi Shams

Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
Rushdi Shams
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
Rushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
Rushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
Rushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
Rushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
Rushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
Rushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
Rushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
Rushdi Shams
 

Mehr von Rushdi Shams (20)

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IR
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processing
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
 
L15 fuzzy logic
L15  fuzzy logicL15  fuzzy logic
L15 fuzzy logic
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
First order logic
First order logicFirst order logic
First order logic
 
Belief function
Belief functionBelief function
Belief function
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
 
L4 vpn
L4  vpnL4  vpn
L4 vpn
 
L3 defense
L3  defenseL3  defense
L3 defense
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
 
L1 phishing
L1  phishingL1  phishing
L1 phishing
 

Kürzlich hochgeladen

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Kürzlich hochgeladen (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

Lec 06. For Loop / Break / Continue

  • 1. Lecture 6 Version 1.0 The for Loop The break Statement The continue Statement Version 1.0
  • 2. Rushdi Shams, Dept of CSE, KUET, Bangladesh2 The for Loop  The for loop allows us to specify three things in a single line: 1. Setting a loop counter to an initial value. 2. Testing the loop counter to determine whether its value has reached the number of repetitions desired. 3. Increasing/decreasing the value of loop counter each time the program segment within the loop is executed.
  • 3. Rushdi Shams, Dept of CSE, KUET, Bangladesh3 General form of for loop
  • 4. Rushdi Shams, Dept of CSE, KUET, Bangladesh4 Example of for loop  This program prints number 1 to 10
  • 5. Rushdi Shams, Dept of CSE, KUET, Bangladesh5 Structure of for loop
  • 6. Rushdi Shams, Dept of CSE, KUET, Bangladesh6 Variation of for loop
  • 7. Rushdi Shams, Dept of CSE, KUET, Bangladesh7 Variation of for loop
  • 8. Rushdi Shams, Dept of CSE, KUET, Bangladesh8 Variation of for loop
  • 9. Rushdi Shams, Dept of CSE, KUET, Bangladesh9 Variation of for loop: Infinite loop
  • 10. Rushdi Shams, Dept of CSE, KUET, Bangladesh10 Observing for loops The initialization, loop-continuation condition and increment/decrement can contain arithmetic expressions. Assume that x=2 and y=10. The statement- for(j=x; j<=4*x*y; j=j+y/x) is equivalent to- for(j=2; j<=80; j=j+5)
  • 11. Rushdi Shams, Dept of CSE, KUET, Bangladesh11 Observing for loops There can be a decrement as well (or you can say a negative increment). For example-
  • 12. Rushdi Shams, Dept of CSE, KUET, Bangladesh12 Observing for loops If the loop continuation condition is initially false, the body portion of the loop is not performed.
  • 13. Rushdi Shams, Dept of CSE, KUET, Bangladesh13 Observing for loops
  • 14. Rushdi Shams, Dept of CSE, KUET, Bangladesh14 Qs
  • 15. Rushdi Shams, Dept of CSE, KUET, Bangladesh15 Nested for loops
  • 16. Rushdi Shams, Dept of CSE, KUET, Bangladesh16 Output
  • 17. Rushdi Shams, Dept of CSE, KUET, Bangladesh17 What will be the output?
  • 18. Rushdi Shams, Dept of CSE, KUET, Bangladesh18 What will be the output?
  • 19. Rushdi Shams, Dept of CSE, KUET, Bangladesh19 The break Statement  We often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test.  The keyword break allows us to do this.
  • 20. Rushdi Shams, Dept of CSE, KUET, Bangladesh20 The break Statement  When break is encountered inside any loop, control automatically passes to the first statement after the loop.
  • 21. Rushdi Shams, Dept of CSE, KUET, Bangladesh21 The break Statement
  • 22. Rushdi Shams, Dept of CSE, KUET, Bangladesh22 The continue Statement  In some programming situations we want to take the control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed.
  • 23. Rushdi Shams, Dept of CSE, KUET, Bangladesh23 The continue Statement  The keyword continue allows us to do this. When continue is encountered inside any loop, control automatically passes to the beginning of the loop.
  • 24. Rushdi Shams, Dept of CSE, KUET, Bangladesh24 The continue Statement