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

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

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

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
 

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

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
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
 

Recently uploaded (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
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
 

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