SlideShare a Scribd company logo
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

More Related Content

Similar to Lec 06. For Loop / Break / Continue

Lec 05. While Loop
Lec 05. While LoopLec 05. While Loop
Lec 05. While LoopRushdi Shams
 
Lec 07. Do-While Loop / Switch
Lec 07. Do-While Loop / SwitchLec 07. Do-While Loop / Switch
Lec 07. Do-While Loop / SwitchRushdi 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 SystemsRushdi Shams
 
Lec 03. Arithmetic Operator / Relational Operator
Lec 03. Arithmetic Operator / Relational OperatorLec 03. Arithmetic Operator / Relational Operator
Lec 03. Arithmetic Operator / Relational OperatorRushdi Shams
 
Lec 10. Functions (Part II)
Lec 10. Functions (Part II)Lec 10. Functions (Part II)
Lec 10. Functions (Part II)Rushdi Shams
 

Similar to 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)
 

More from Rushdi Shams

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 ResearchRushdi Shams
 
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 IRRushdi Shams
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101Rushdi Shams
 
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 processingRushdi Shams
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: ParsingRushdi Shams
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translationRushdi 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 translationRushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semanticsRushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logicRushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logicRushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structureRushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representationRushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hackingRushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)Rushdi Shams
 

More from 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
 

Recently uploaded

INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfbu07226
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxssuserbdd3e8
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxbennyroshan06
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxDenish Jangid
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfTamralipta Mahavidyalaya
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resourcesdimpy50
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesRased Khan
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online PresentationGDSCYCCE
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePedroFerreira53928
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleCeline George
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportAvinash Rai
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXMIRIAMSALINAS13
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfQucHHunhnh
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfDr. M. Kumaresan Hort.
 

Recently uploaded (20)

INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
 

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