5 3 Multiway Selection In addition to twoway

  • Slides: 24
Download presentation
5 -3 Multiway Selection In addition to two-way selection, most programming languages provide another

5 -3 Multiway Selection In addition to two-way selection, most programming languages provide another selection concept known as multiway selection. Multiway selection chooses among several alternatives. C has two different ways to implement multiway selection: the switch statement and else-if construct. Topics discussed in this section: The switch Statement The else-if Computer Science: A Structured Programming Approach Using C 1

FIGURE 5 -19 switch Decision Logic Computer Science: A Structured Programming Approach Using C

FIGURE 5 -19 switch Decision Logic Computer Science: A Structured Programming Approach Using C 2

FIGURE 5 -20 switch Statement Syntax Computer Science: A Structured Programming Approach Using C

FIGURE 5 -20 switch Statement Syntax Computer Science: A Structured Programming Approach Using C 3

FIGURE 5 -21 switch Flow Computer Science: A Structured Programming Approach Using C 4

FIGURE 5 -21 switch Flow Computer Science: A Structured Programming Approach Using C 4

PROGRAM 5 -6 Demonstrate the switch Statement Computer Science: A Structured Programming Approach Using

PROGRAM 5 -6 Demonstrate the switch Statement Computer Science: A Structured Programming Approach Using C 5

FIGURE 5 -22 switch Results Computer Science: A Structured Programming Approach Using C 6

FIGURE 5 -22 switch Results Computer Science: A Structured Programming Approach Using C 6

FIGURE 5 -23 A switch with break Statements Computer Science: A Structured Programming Approach

FIGURE 5 -23 A switch with break Statements Computer Science: A Structured Programming Approach Using C 7

PROGRAM 5 -7 Multivalued case Statements Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -7 Multivalued case Statements Computer Science: A Structured Programming Approach Using C 8

Table 5 -5 Summary of switch Statement Rules Computer Science: A Structured Programming Approach

Table 5 -5 Summary of switch Statement Rules Computer Science: A Structured Programming Approach Using C 9

PROGRAM 5 -8 Student Grading Computer Science: A Structured Programming Approach Using C 10

PROGRAM 5 -8 Student Grading Computer Science: A Structured Programming Approach Using C 10

PROGRAM 5 -8 Student Grading Computer Science: A Structured Programming Approach Using C 11

PROGRAM 5 -8 Student Grading Computer Science: A Structured Programming Approach Using C 11

PROGRAM 5 -8 Student Grading Computer Science: A Structured Programming Approach Using C 12

PROGRAM 5 -8 Student Grading Computer Science: A Structured Programming Approach Using C 12

FIGURE 5 -24 The else-if Logic Design for Program 5 -9 Computer Science: A

FIGURE 5 -24 The else-if Logic Design for Program 5 -9 Computer Science: A Structured Programming Approach Using C 13

Note The else-if is an artificial C construct that is only used when 1.

Note The else-if is an artificial C construct that is only used when 1. The selection variable is not an integral, and 2. The same variable is being tested in the expressions. Computer Science: A Structured Programming Approach Using C 14

PROGRAM 5 -9 Convert Score to Grade Computer Science: A Structured Programming Approach Using

PROGRAM 5 -9 Convert Score to Grade Computer Science: A Structured Programming Approach Using C 15

PROGRAM 5 -9 Convert Score to Grade Computer Science: A Structured Programming Approach Using

PROGRAM 5 -9 Convert Score to Grade Computer Science: A Structured Programming Approach Using C 16

PROGRAM 5 -9 Convert Score to Grade Computer Science: A Structured Programming Approach Using

PROGRAM 5 -9 Convert Score to Grade Computer Science: A Structured Programming Approach Using C 17

5 -4 More Standard Functions One of the assets of the C language is

5 -4 More Standard Functions One of the assets of the C language is its rich set of standard functions that make programming much easier. For example, C 99 has two parallel but separate header files for manipulating characters: ctype. h and wctype. h. Topics discussed in this section: Standard Characters Functions A Classification Program Handling Major Errors Computer Science: A Structured Programming Approach Using C 18

FIGURE 5 -25 Classifications of the Character Type Computer Science: A Structured Programming Approach

FIGURE 5 -25 Classifications of the Character Type Computer Science: A Structured Programming Approach Using C 19

continued Table 5 -6 Classifying Functions Computer Science: A Structured Programming Approach Using C

continued Table 5 -6 Classifying Functions Computer Science: A Structured Programming Approach Using C 20

Table 5 -6 Classifying Functions (continued) Computer Science: A Structured Programming Approach Using C

Table 5 -6 Classifying Functions (continued) Computer Science: A Structured Programming Approach Using C 21

Table 5 -7 Conversion Functions Computer Science: A Structured Programming Approach Using C 22

Table 5 -7 Conversion Functions Computer Science: A Structured Programming Approach Using C 22

PROGRAM 5 -10 Demonstrate Classification Functions Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -10 Demonstrate Classification Functions Computer Science: A Structured Programming Approach Using C 23

PROGRAM 5 -10 Demonstrate Classification Functions Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -10 Demonstrate Classification Functions Computer Science: A Structured Programming Approach Using C 24