Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# C-07

40

Published on

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
40
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
4
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

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