computer science: a structured programming approach using c1 5-3 multiway selection in addition to...

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

Upload: audrey-pierce

Post on 04-Jan-2016

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 1

5-3 Multiway Selection

In addition to two-way selection, most programming In addition to two-way selection, most programming languages provide another selection concept known as languages provide another selection concept known as multiway selection. Multiway selection chooses among multiway selection. Multiway selection chooses among several alternatives. C has two different ways to several alternatives. C has two different ways to implement multiway selection: the implement multiway selection: the switchswitch statement statement and and else-ifelse-if construct. construct.

The switch StatementThe else-if

Topics discussed in this section:Topics discussed in this section:

Page 2: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 2

FIGURE 5-19 switch Decision Logic

Page 3: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 3

FIGURE 5-20 switch Statement Syntax

Page 4: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 4

FIGURE 5-21 switch Flow

Page 5: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 5

PROGRAM 5-6 Demonstrate the switch Statement

Page 6: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 6

FIGURE 5-22 switch Results

Page 7: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 7

FIGURE 5-23 A switch with break Statements

Page 8: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 8

PROGRAM 5-7 Multivalued case Statements

Page 9: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 9

Table 5-5 Summary of switch Statement Rules

Page 10: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 10

PROGRAM 5-8 Student Grading

Page 11: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 11

PROGRAM 5-8 Student Grading

Page 12: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 12

PROGRAM 5-8 Student Grading

Page 13: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 13

FIGURE 5-24 The else-if Logic Design for Program 5-9

Page 14: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 14

The else-if is an artificial C construct that is only used when1. The selection variable is not an integral, and2. The same variable is being tested in the expressions.

NoteNote

Page 15: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 15

PROGRAM 5-9 Convert Score to Grade

Page 16: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 16

PROGRAM 5-9 Convert Score to Grade

Page 17: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 17

PROGRAM 5-9 Convert Score to Grade

Page 18: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 18

5-4 More Standard Functions

One of the assets of the C language is its rich set of One of the assets of the C language is its rich set of standard functions that make programming much standard functions that make programming much easier. For example, C99 has two parallel but separate easier. For example, C99 has two parallel but separate header files for manipulating characters: ctype.h and header files for manipulating characters: ctype.h and wctype.h.wctype.h.

Standard Characters FunctionsA Classification ProgramHandling Major Errors

Topics discussed in this section:Topics discussed in this section:

Page 19: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 19

FIGURE 5-25 Classifications of the Character Type

Page 20: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 20

Table 5-6 Classifying Functions

continued

Page 21: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 21

Table 5-6 Classifying Functions (continued)

Page 22: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 22

Table 5-7 Conversion Functions

Page 23: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 23

PROGRAM 5-10 Demonstrate Classification Functions

Page 24: Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide

Computer Science: A Structured Programming Approach Using C 24

PROGRAM 5-10 Demonstrate Classification Functions