computer science: a structured programming approach using c1 5-2 two-way selection the decision is...

30
Computer Science: A Structured Programming Approach Using C 1 5-2 Two-Way Selection The decision is described to the computer as a The decision is described to the computer as a conditional statement that can be answered conditional statement that can be answered either true or false. If the answer is true, one or either true or false. If the answer is true, one or more action statements are executed. If the more action statements are executed. If the answer is false, then a different action or set of answer is false, then a different action or set of actions is executed. actions is executed. ifelse and Null else Statement Nested if Statements and Dangling else Problem Simplifying if Statements Conditional Expressions Topics discussed in this section: Topics discussed in this section:

Upload: ami-chandler

Post on 05-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 1

5-2 Two-Way Selection

The decision is described to the computer as a The decision is described to the computer as a conditional statement that can be answered either true conditional statement that can be answered either true or false. If the answer is true, one or more action or false. If the answer is true, one or more action statements are executed. If the answer is false, then a statements are executed. If the answer is false, then a different action or set of actions is executed. different action or set of actions is executed.

if…else and Null else StatementNested if Statements and Dangling else ProblemSimplifying if StatementsConditional Expressions

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

Page 2: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 2

FIGURE 5-7 if...else Logic Flow

Page 3: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 3

Table 5-2 Syntactical Rules for if…else Statements

Page 4: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 4

FIGURE 5-8 A Simple if...else Statement

Page 5: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 5

FIGURE 5-9 Compound Statements in an if...else

Page 6: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 6

FIGURE 5-10 Complemented if...else Statements

Page 7: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 7

FIGURE 5-11 A Null else Statement

Page 8: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 8

FIGURE 5-12 A Null if Statement

Page 9: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 9

PROGRAM 5-3 Two-way Selection

Page 10: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 10

PROGRAM 5-3 Two-way Selection

Page 11: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 11

FIGURE 5-13 Nested if Statements

Page 12: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 12

PROGRAM 5-4 Nested if Statements

Page 13: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 13

PROGRAM 5-4 Nested if Statements

Page 14: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 14

else is always paired with the most recent unpaired if.

NoteNote

Page 15: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 15

FIGURE 5-14 Dangling else

Page 16: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 16

FIGURE 5-15 Dangling else Solution

Page 17: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 17

Table 5-3 Simplifying the Condition

Page 18: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 18

FIGURE 5-16 Conditional Expression

Page 19: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 19

Table 5-4 Examples of Marginal Tax Rates

Page 20: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 20

FIGURE 5-18 Design for Program 5-5 (Part III)

Page 21: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 21

PROGRAM 5-5 Calculate Taxes

Page 22: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 22

PROGRAM 5-5 Calculate Taxes

Page 23: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 23

PROGRAM 5-5 Calculate Taxes

Page 24: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 24

PROGRAM 5-5 Calculate Taxes

Page 25: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 25

PROGRAM 5-5 Calculate Taxes

Page 26: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 26

PROGRAM 5-5 Calculate Taxes

Page 27: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 27

PROGRAM 5-5 Calculate Taxes

Page 28: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 28

PROGRAM 5-5 Calculate Taxes

Page 29: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 29

PROGRAM 5-5 Calculate Taxes

Page 30: Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement

Computer Science: A Structured Programming Approach Using C 30

PROGRAM 5-5 Calculate Taxes