+ All Categories

C-07

Date post: 19-Jan-2015
Category:
Upload: rushdi-shams
View: 34 times
Download: 3 times
Share this document with a friend
Description:
 
Popular Tags:
22
Lecture 7 Lecture 7 Version 1.0 Version 1.0 The The do-while do-while Loop Loop The The switch switch Statement Statement
Transcript
Page 1: C-07

Lecture 7Lecture 7Version 1.0Version 1.0

The The do-whiledo-while Loop Loop

The The switchswitch Statement Statement

Page 2: C-07

2Rushdi Shams, Dept of CSE, KUET, Bangladesh

The do-while Loop The do-while Loop StructureStructure

Page 3: C-07

3Rushdi Shams, Dept of CSE, KUET, Bangladesh

The do-while LoopThe do-while Loop

There is a minor difference between the There is a minor difference between the working of working of while while and and do-while do-while loops loops

This difference is the place where the This difference is the place where the condition is tested condition is tested

The The while while tests the condition before tests the condition before executing any of the statements within executing any of the statements within the the while while loop loop

do-while do-while tests the condition after having tests the condition after having executed the statements within the loop executed the statements within the loop

Page 4: C-07

4Rushdi Shams, Dept of CSE, KUET, Bangladesh

The do-while LoopThe do-while Loop

This means This means do-while do-while would execute would execute its statements at least once, even if its statements at least once, even if the condition fails for the first timethe condition fails for the first time

Page 5: C-07

5Rushdi Shams, Dept of CSE, KUET, Bangladesh

while versus do-whilewhile versus do-while

Page 6: C-07

6Rushdi Shams, Dept of CSE, KUET, Bangladesh

break and continue in break and continue in do-whiledo-while

break break and and continue continue are used with are used with do-while do-while just as they would be in a just as they would be in a while while or a or a for for loop loop

A A break break takes you out of the takes you out of the do-do-while while bypassing the conditional test. bypassing the conditional test. A A continue continue sends you straight to sends you straight to the test at the end of the loopthe test at the end of the loop

Page 7: C-07

7Rushdi Shams, Dept of CSE, KUET, Bangladesh

The switch StatementThe switch Statement

The control statement that allows us The control statement that allows us to make a decision from the number to make a decision from the number of choices is called a of choices is called a switchswitch

Page 8: C-07

8Rushdi Shams, Dept of CSE, KUET, Bangladesh

General form of switchGeneral form of switch

Page 9: C-07

9Rushdi Shams, Dept of CSE, KUET, Bangladesh

switch Statement: switch Statement: unleashedunleashed

The integer expression following the The integer expression following the keyword keyword switch switch is any C expression is any C expression that will yield an integer value. It that will yield an integer value. It could be an integer constant like 1, 2 could be an integer constant like 1, 2 or 3, or an expression that evaluates or 3, or an expression that evaluates to an integer to an integer

The keyword The keyword case case is followed by an is followed by an integer or a character constant integer or a character constant

Each constant in each Each constant in each case case must be must be different from all the others different from all the others

Page 10: C-07

10Rushdi Shams, Dept of CSE, KUET, Bangladesh

What happens when we run What happens when we run a program containing a a program containing a

switchswitch? ? The integer expression following the keyword The integer expression following the keyword

switch switch is evaluated.is evaluated. The value it gives is then matched, one by one, The value it gives is then matched, one by one,

against the constant values that follow the against the constant values that follow the case case statements.statements.

When a match is found, the program executes When a match is found, the program executes the statements following that the statements following that casecase, and all , and all subsequent subsequent case case and and default default statements as well.statements as well.

If no match is found with any of the If no match is found with any of the case case statements, only the statements following the statements, only the statements following the default default are executed. are executed.

Page 11: C-07

11Rushdi Shams, Dept of CSE, KUET, Bangladesh

Unusual behaviour of Unusual behaviour of switchswitch

Page 12: C-07

12Rushdi Shams, Dept of CSE, KUET, Bangladesh

Making the unusual Making the unusual behaviour- usualbehaviour- usual

Page 13: C-07

13Rushdi Shams, Dept of CSE, KUET, Bangladesh

Do I need cases in serial?Do I need cases in serial?

Page 14: C-07

14Rushdi Shams, Dept of CSE, KUET, Bangladesh

Can I use characters in Can I use characters in cases?cases?

Page 15: C-07

15Rushdi Shams, Dept of CSE, KUET, Bangladesh

Blank casesBlank cases

Page 16: C-07

16Rushdi Shams, Dept of CSE, KUET, Bangladesh

Statements outside casesStatements outside cases

Page 17: C-07

17Rushdi Shams, Dept of CSE, KUET, Bangladesh

if versus switchif versus switch

The disadvantage of The disadvantage of switch switch is that is that one cannot have a case in a one cannot have a case in a switch switch which looks like: which looks like:

case i <= 20 :case i <= 20 : All that we can have after the case is All that we can have after the case is

an an int int constant or a constant or a char char constant constant or an expression that evaluates to or an expression that evaluates to one of these constants. Even a one of these constants. Even a float float is not allowed.is not allowed.

Page 18: C-07

18Rushdi Shams, Dept of CSE, KUET, Bangladesh

if versus switchif versus switch

The advantage of The advantage of switch switch over over if if is is that it leads to a more structured that it leads to a more structured program and the level of indentation program and the level of indentation is manageable, more so if there are is manageable, more so if there are multiple statements within each multiple statements within each case case of a of a switchswitch. .

Page 19: C-07

19Rushdi Shams, Dept of CSE, KUET, Bangladesh

switch and case switch and case expressionsexpressions

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 Expressions can also be used in cases

provided they are constant provided they are constant expressions. Thus expressions. Thus case 3 + 7 case 3 + 7 is is correct, however, correct, however, case a + b case a + b is is incorrect.incorrect.

Page 20: C-07

20Rushdi Shams, Dept of CSE, KUET, Bangladesh

break and continue in break and continue in switchswitch

The The break break statement when used in statement when used in a a switch switch takes the control outside takes the control outside the the switchswitch. However, use of . However, use of continue continue will not take the control will not take the control to the beginning of to the beginning of switch switch as one as one is likely to believe. is likely to believe.

Page 21: C-07

21Rushdi Shams, Dept of CSE, KUET, Bangladesh

Nested switchNested switch

In principle, a In principle, a switch switch may occur may occur within another, but in practice it is within another, but in practice it is rarely done. Such statements would rarely done. Such statements would be called nested be called nested switch switch statements.statements.

Page 22: C-07

22Rushdi Shams, Dept of CSE, KUET, Bangladesh

Use of switchUse of switch

The The switch switch statement is very useful statement is very useful while writing menu driven while writing menu driven programs. programs.


Recommended