SlideShare a Scribd company logo
1 of 22
Lecture 7Lecture 7
Version 1.0Version 1.0
TheThe do-whiledo-while LoopLoop
TheThe switchswitch StatementStatement
2Rushdi Shams, Dept of CSE, KUET, Bangladesh
The do-while Loop StructureThe do-while Loop Structure
3Rushdi Shams, Dept of CSE, KUET, Bangladesh
The do-while LoopThe do-while Loop
 There is a minor difference between the workingThere is a minor difference between the working
ofof whilewhile andand do-whiledo-while loopsloops
 This difference is the place where the conditionThis difference is the place where the condition
is testedis tested
 TheThe whilewhile tests the condition before executingtests the condition before executing
any of the statements within theany of the statements within the whilewhile looploop
 do-whiledo-while tests the condition after havingtests the condition after having
executed the statements within the loopexecuted the statements within the loop
4Rushdi Shams, Dept of CSE, KUET, Bangladesh
The do-while LoopThe do-while Loop
 This meansThis means do-whiledo-while would execute itswould execute its
statements at least once, even if the conditionstatements at least once, even if the condition
fails for the first timefails for the first time
5Rushdi Shams, Dept of CSE, KUET, Bangladesh
while versus do-whilewhile versus do-while
6Rushdi Shams, Dept of CSE, KUET, Bangladesh
break and continue in do-whilebreak and continue in do-while
 breakbreak andand continuecontinue are used withare used with do-whiledo-while justjust
as they would be in aas they would be in a whilewhile or aor a forfor looploop
 AA breakbreak takes you out of thetakes you out of the do-whiledo-while
bypassing the conditional test. Abypassing the conditional test. A continuecontinue sendssends
you straight to the test at the end of the loopyou straight to the test at the end of the loop
7Rushdi Shams, Dept of CSE, KUET, Bangladesh
The switch StatementThe switch Statement
 The control statement that allows us to make aThe control statement that allows us to make a
decision from the number of choices is called adecision from the number of choices is called a
switchswitch
8Rushdi Shams, Dept of CSE, KUET, Bangladesh
General form of switchGeneral form of switch
9Rushdi Shams, Dept of CSE, KUET, Bangladesh
switch Statement: unleashedswitch Statement: unleashed
 The integer expression following the keywordThe integer expression following the keyword
switchswitch is any C expression that will yield anis any C expression that will yield an
integer value. It could be an integer constant likeinteger value. It could be an integer constant like
1, 2 or 3, or an expression that evaluates to an1, 2 or 3, or an expression that evaluates to an
integerinteger
 The keywordThe keyword casecase is followed by an integer or ais followed by an integer or a
character constantcharacter constant
 Each constant in eachEach constant in each casecase must be differentmust be different
from all the othersfrom all the others
10Rushdi Shams, Dept of CSE, KUET, Bangladesh
What happens when we run aWhat happens when we run a
program containing aprogram containing a switchswitch??
 The integer expression following the keywordThe integer expression following the keyword switchswitch isis
evaluated.evaluated.
 The value it gives is then matched, one by one, againstThe value it gives is then matched, one by one, against
the constant values that follow thethe constant values that follow the casecase statements.statements.
 When a match is found, the program executes theWhen a match is found, the program executes the
statements following thatstatements following that casecase, and all subsequent, and all subsequent casecase
andand defaultdefault statements as well.statements as well.
 If no match is found with any of theIf no match is found with any of the casecase statements,statements,
only the statements following theonly the statements following the defaultdefault are executed.are executed.
11Rushdi Shams, Dept of CSE, KUET, Bangladesh
Unusual behaviour of switchUnusual behaviour of switch
12Rushdi Shams, Dept of CSE, KUET, Bangladesh
Making the unusual behaviour- usualMaking the unusual behaviour- usual
13Rushdi Shams, Dept of CSE, KUET, Bangladesh
Do I need cases in serial?Do I need cases in serial?
14Rushdi Shams, Dept of CSE, KUET, Bangladesh
Can I use characters in cases?Can I use characters in cases?
15Rushdi Shams, Dept of CSE, KUET, Bangladesh
Blank casesBlank cases
16Rushdi Shams, Dept of CSE, KUET, Bangladesh
Statements outside casesStatements outside cases
17Rushdi Shams, Dept of CSE, KUET, Bangladesh
if versus switchif versus switch
 The disadvantage ofThe disadvantage of switchswitch is that one cannotis that one cannot
have a case in ahave a case in a switchswitch which looks like:which looks like:
case i <= 20 :case i <= 20 :
 All that we can have after the case is anAll that we can have after the case is an intint
constant or aconstant or a charchar constant or an expressionconstant or an expression
that evaluates to one of these constants. Even athat evaluates to one of these constants. Even a
floatfloat is not allowed.is not allowed.
18Rushdi Shams, Dept of CSE, KUET, Bangladesh
if versus switchif versus switch
 The advantage ofThe advantage of switchswitch overover ifif is that it leadsis that it leads
to a more structured program and the level ofto a more structured program and the level of
indentation is manageable, more so if there areindentation is manageable, more so if there are
multiple statements within eachmultiple statements within each casecase of aof a
switchswitch..
19Rushdi Shams, Dept of CSE, KUET, Bangladesh
switch and case expressionsswitch and case expressions
 The following expressions are valid-The following expressions are valid-
switch ( i + j * k )switch ( i + j * k )
switch ( 23 + 45 % 4 * k )switch ( 23 + 45 % 4 * k )
switch ( a < 4 && b > 7 )switch ( a < 4 && b > 7 )
 Expressions can also be used in cases providedExpressions can also be used in cases provided
they are constant expressions. Thusthey are constant expressions. Thus case 3 + 7case 3 + 7
is correct, however,is correct, however, case a + bcase a + b is incorrect.is incorrect.
20Rushdi Shams, Dept of CSE, KUET, Bangladesh
break and continue in switchbreak and continue in switch
 TheThe breakbreak statement when used in astatement when used in a switchswitch
takes the control outside thetakes the control outside the switchswitch. However,. However,
use ofuse of continuecontinue will not take the control towill not take the control to
the beginning ofthe beginning of switchswitch as one is likely toas one is likely to
believe.believe.
21Rushdi Shams, Dept of CSE, KUET, Bangladesh
Nested switchNested switch
 In principle, aIn principle, a switchswitch may occur within another,may occur within another,
but in practice it is rarely done. Such statementsbut in practice it is rarely done. Such statements
would be called nestedwould be called nested switchswitch statements.statements.
22Rushdi Shams, Dept of CSE, KUET, Bangladesh
Use of switchUse of switch
 TheThe switchswitch statement is very useful whilestatement is very useful while
writing menu driven programs.writing menu driven programs.

More Related Content

Viewers also liked

毅佳電腦科技獨家代理:虛擬化軟體softxpand
毅佳電腦科技獨家代理:虛擬化軟體softxpand 毅佳電腦科技獨家代理:虛擬化軟體softxpand
毅佳電腦科技獨家代理:虛擬化軟體softxpand softxpand
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hackingRushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structureRushdi Shams
 
Database Keys / Referential Integrity / Propagation Constraint / Entity Integ...
Database Keys / Referential Integrity / Propagation Constraint / Entity Integ...Database Keys / Referential Integrity / Propagation Constraint / Entity Integ...
Database Keys / Referential Integrity / Propagation Constraint / Entity Integ...Rushdi Shams
 
L1 overview of software engineering
L1  overview of software engineeringL1  overview of software engineering
L1 overview of software engineeringRushdi 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
 
L8 components and properties of dbms
L8  components and properties of dbmsL8  components and properties of dbms
L8 components and properties of dbmsRushdi Shams
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: ParsingRushdi Shams
 
Distributed Database Management Systems (Distributed DBMS)
Distributed Database Management Systems (Distributed DBMS)Distributed Database Management Systems (Distributed DBMS)
Distributed Database Management Systems (Distributed DBMS)Rushdi Shams
 
How to successfully give a seminar presentation
How to successfully give a seminar presentationHow to successfully give a seminar presentation
How to successfully give a seminar presentationRushdi Shams
 

Viewers also liked (11)

毅佳電腦科技獨家代理:虛擬化軟體softxpand
毅佳電腦科技獨家代理:虛擬化軟體softxpand 毅佳電腦科技獨家代理:虛擬化軟體softxpand
毅佳電腦科技獨家代理:虛擬化軟體softxpand
 
Lec 18. Recursion
Lec 18. RecursionLec 18. Recursion
Lec 18. Recursion
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
 
Database Keys / Referential Integrity / Propagation Constraint / Entity Integ...
Database Keys / Referential Integrity / Propagation Constraint / Entity Integ...Database Keys / Referential Integrity / Propagation Constraint / Entity Integ...
Database Keys / Referential Integrity / Propagation Constraint / Entity Integ...
 
L1 overview of software engineering
L1  overview of software engineeringL1  overview of software engineering
L1 overview of software engineering
 
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
 
L8 components and properties of dbms
L8  components and properties of dbmsL8  components and properties of dbms
L8 components and properties of dbms
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
 
Distributed Database Management Systems (Distributed DBMS)
Distributed Database Management Systems (Distributed DBMS)Distributed Database Management Systems (Distributed DBMS)
Distributed Database Management Systems (Distributed DBMS)
 
How to successfully give a seminar presentation
How to successfully give a seminar presentationHow to successfully give a seminar presentation
How to successfully give a seminar presentation
 

Similar to Lec 07. Do-While Loop / Switch

Lec 10. Functions (Part II)
Lec 10. Functions (Part II)Lec 10. Functions (Part II)
Lec 10. Functions (Part II)Rushdi 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
 
05. Control Structures.ppt
05. Control Structures.ppt05. Control Structures.ppt
05. Control Structures.pptAyushDut
 
Control statements
Control statementsControl statements
Control statementsCutyChhaya
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsEng Teong Cheah
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)TejaswiB4
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, LoopingMURALIDHAR R
 
Lec 06. For Loop / Break / Continue
Lec 06. For Loop / Break / ContinueLec 06. For Loop / Break / Continue
Lec 06. For Loop / Break / ContinueRushdi Shams
 
Lec 05. While Loop
Lec 05. While LoopLec 05. While Loop
Lec 05. While LoopRushdi Shams
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 
Control Structure in JavaScript (1).pptx
Control Structure in JavaScript (1).pptxControl Structure in JavaScript (1).pptx
Control Structure in JavaScript (1).pptxBansalShrivastava
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.pptManojKhadilkar1
 
Notes: Verilog Part 4- Behavioural Modelling
Notes: Verilog Part 4- Behavioural ModellingNotes: Verilog Part 4- Behavioural Modelling
Notes: Verilog Part 4- Behavioural ModellingJay Baxi
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#Prasanna Kumar SM
 
BSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETBSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETUjwala Junghare
 
Bansal presentation (1).pptx
Bansal presentation (1).pptxBansal presentation (1).pptx
Bansal presentation (1).pptxAbhiYadav655132
 

Similar to Lec 07. Do-While Loop / Switch (20)

Lec 10. Functions (Part II)
Lec 10. Functions (Part II)Lec 10. Functions (Part II)
Lec 10. Functions (Part II)
 
Lec 03. Arithmetic Operator / Relational Operator
Lec 03. Arithmetic Operator / Relational OperatorLec 03. Arithmetic Operator / Relational Operator
Lec 03. Arithmetic Operator / Relational Operator
 
05. Control Structures.ppt
05. Control Structures.ppt05. Control Structures.ppt
05. Control Structures.ppt
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Control statements
Control statementsControl statements
Control statements
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Lec 06. For Loop / Break / Continue
Lec 06. For Loop / Break / ContinueLec 06. For Loop / Break / Continue
Lec 06. For Loop / Break / Continue
 
Lec 05. While Loop
Lec 05. While LoopLec 05. While Loop
Lec 05. While Loop
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Control Structure in JavaScript (1).pptx
Control Structure in JavaScript (1).pptxControl Structure in JavaScript (1).pptx
Control Structure in JavaScript (1).pptx
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
Notes: Verilog Part 4- Behavioural Modelling
Notes: Verilog Part 4- Behavioural ModellingNotes: Verilog Part 4- Behavioural Modelling
Notes: Verilog Part 4- Behavioural Modelling
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
 
BSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETBSc. III Unit iii VB.NET
BSc. III Unit iii VB.NET
 
Bansal presentation (1).pptx
Bansal presentation (1).pptxBansal presentation (1).pptx
Bansal presentation (1).pptx
 
Computer programming 2 Lesson 8
Computer programming 2  Lesson 8Computer programming 2  Lesson 8
Computer programming 2 Lesson 8
 

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
 
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 representation
Knowledge representationKnowledge representation
Knowledge representationRushdi Shams
 
L2 l3 l4 software process models
L2 l3 l4  software process modelsL2 l3 l4  software process models
L2 l3 l4 software process modelsRushdi Shams
 
L13 why software fails
L13  why software failsL13  why software fails
L13 why software failsRushdi Shams
 
Lecture 14,15 and 16 file systems
Lecture 14,15 and 16  file systemsLecture 14,15 and 16  file systems
Lecture 14,15 and 16 file systemsRushdi Shams
 
Lecture 11,12 and 13 deadlocks
Lecture 11,12 and 13  deadlocksLecture 11,12 and 13  deadlocks
Lecture 11,12 and 13 deadlocksRushdi 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
 
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 representation
Knowledge representationKnowledge representation
Knowledge representation
 
First order logic
First order logicFirst order logic
First order logic
 
Belief function
Belief functionBelief function
Belief function
 
L4 vpn
L4  vpnL4  vpn
L4 vpn
 
L3 defense
L3  defenseL3  defense
L3 defense
 
L1 phishing
L1  phishingL1  phishing
L1 phishing
 
L2 l3 l4 software process models
L2 l3 l4  software process modelsL2 l3 l4  software process models
L2 l3 l4 software process models
 
L13 why software fails
L13  why software failsL13  why software fails
L13 why software fails
 
Lecture 14,15 and 16 file systems
Lecture 14,15 and 16  file systemsLecture 14,15 and 16  file systems
Lecture 14,15 and 16 file systems
 
Lecture 11,12 and 13 deadlocks
Lecture 11,12 and 13  deadlocksLecture 11,12 and 13  deadlocks
Lecture 11,12 and 13 deadlocks
 

Recently uploaded

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
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 . pdfQucHHunhnh
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
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 13Steve Thomason
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
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 ModeThiyagu K
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
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).pdfSoniaTolstoy
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Recently uploaded (20)

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
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"
 
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
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
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
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
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
 
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
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
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"
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
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
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

Lec 07. Do-While Loop / Switch

  • 1. Lecture 7Lecture 7 Version 1.0Version 1.0 TheThe do-whiledo-while LoopLoop TheThe switchswitch StatementStatement
  • 2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh The do-while Loop StructureThe do-while Loop Structure
  • 3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh The do-while LoopThe do-while Loop  There is a minor difference between the workingThere is a minor difference between the working ofof whilewhile andand do-whiledo-while loopsloops  This difference is the place where the conditionThis difference is the place where the condition is testedis tested  TheThe whilewhile tests the condition before executingtests the condition before executing any of the statements within theany of the statements within the whilewhile looploop  do-whiledo-while tests the condition after havingtests the condition after having executed the statements within the loopexecuted the statements within the loop
  • 4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh The do-while LoopThe do-while Loop  This meansThis means do-whiledo-while would execute itswould execute its statements at least once, even if the conditionstatements at least once, even if the condition fails for the first timefails for the first time
  • 5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh while versus do-whilewhile versus do-while
  • 6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh break and continue in do-whilebreak and continue in do-while  breakbreak andand continuecontinue are used withare used with do-whiledo-while justjust as they would be in aas they would be in a whilewhile or aor a forfor looploop  AA breakbreak takes you out of thetakes you out of the do-whiledo-while bypassing the conditional test. Abypassing the conditional test. A continuecontinue sendssends you straight to the test at the end of the loopyou straight to the test at the end of the loop
  • 7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh The switch StatementThe switch Statement  The control statement that allows us to make aThe control statement that allows us to make a decision from the number of choices is called adecision from the number of choices is called a switchswitch
  • 8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh General form of switchGeneral form of switch
  • 9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh switch Statement: unleashedswitch Statement: unleashed  The integer expression following the keywordThe integer expression following the keyword switchswitch is any C expression that will yield anis any C expression that will yield an integer value. It could be an integer constant likeinteger value. It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an1, 2 or 3, or an expression that evaluates to an integerinteger  The keywordThe keyword casecase is followed by an integer or ais followed by an integer or a character constantcharacter constant  Each constant in eachEach constant in each casecase must be differentmust be different from all the othersfrom all the others
  • 10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh What happens when we run aWhat happens when we run a program containing aprogram containing a switchswitch??  The integer expression following the keywordThe integer expression following the keyword switchswitch isis evaluated.evaluated.  The value it gives is then matched, one by one, againstThe value it gives is then matched, one by one, against the constant values that follow thethe constant values that follow the casecase statements.statements.  When a match is found, the program executes theWhen a match is found, the program executes the statements following thatstatements following that casecase, and all subsequent, and all subsequent casecase andand defaultdefault statements as well.statements as well.  If no match is found with any of theIf no match is found with any of the casecase statements,statements, only the statements following theonly the statements following the defaultdefault are executed.are executed.
  • 11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh Unusual behaviour of switchUnusual behaviour of switch
  • 12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh Making the unusual behaviour- usualMaking the unusual behaviour- usual
  • 13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh Do I need cases in serial?Do I need cases in serial?
  • 14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh Can I use characters in cases?Can I use characters in cases?
  • 15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh Blank casesBlank cases
  • 16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh Statements outside casesStatements outside cases
  • 17. 17Rushdi Shams, Dept of CSE, KUET, Bangladesh if versus switchif versus switch  The disadvantage ofThe disadvantage of switchswitch is that one cannotis that one cannot have a case in ahave a case in a switchswitch which looks like:which looks like: case i <= 20 :case i <= 20 :  All that we can have after the case is anAll that we can have after the case is an intint constant or aconstant or a charchar constant or an expressionconstant or an expression that evaluates to one of these constants. Even athat evaluates to one of these constants. Even a floatfloat is not allowed.is not allowed.
  • 18. 18Rushdi Shams, Dept of CSE, KUET, Bangladesh if versus switchif versus switch  The advantage ofThe advantage of switchswitch overover ifif is that it leadsis that it leads to a more structured program and the level ofto a more structured program and the level of indentation is manageable, more so if there areindentation is manageable, more so if there are multiple statements within eachmultiple statements within each casecase of aof a switchswitch..
  • 19. 19Rushdi Shams, Dept of CSE, KUET, Bangladesh switch and case expressionsswitch and case expressions  The following expressions are valid-The following expressions are valid- switch ( i + j * k )switch ( i + j * k ) switch ( 23 + 45 % 4 * k )switch ( 23 + 45 % 4 * k ) switch ( a < 4 && b > 7 )switch ( a < 4 && b > 7 )  Expressions can also be used in cases providedExpressions can also be used in cases provided they are constant expressions. Thusthey are constant expressions. Thus case 3 + 7case 3 + 7 is correct, however,is correct, however, case a + bcase a + b is incorrect.is incorrect.
  • 20. 20Rushdi Shams, Dept of CSE, KUET, Bangladesh break and continue in switchbreak and continue in switch  TheThe breakbreak statement when used in astatement when used in a switchswitch takes the control outside thetakes the control outside the switchswitch. However,. However, use ofuse of continuecontinue will not take the control towill not take the control to the beginning ofthe beginning of switchswitch as one is likely toas one is likely to believe.believe.
  • 21. 21Rushdi Shams, Dept of CSE, KUET, Bangladesh Nested switchNested switch  In principle, aIn principle, a switchswitch may occur within another,may occur within another, but in practice it is rarely done. Such statementsbut in practice it is rarely done. Such statements would be called nestedwould be called nested switchswitch statements.statements.
  • 22. 22Rushdi Shams, Dept of CSE, KUET, Bangladesh Use of switchUse of switch  TheThe switchswitch statement is very useful whilestatement is very useful while writing menu driven programs.writing menu driven programs.