1 objectives ❏ to be able to list and describe the six expression categories ❏ to understand the...

41
1 Objectives To be able to list and describe the six expression categories To understand the rules of precedence and associativity in evaluating expressions To understand the result of side effects in expression evaluation To be able to predict the results when an expression is evaluated To understand implicit and explicit type conversion Chapter 3 Chapter 3 Structure of a C Program Structure of a C Program

Upload: allen-hensley

Post on 03-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

1

Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in

evaluating expressions ❏ To understand the result of side effects in expression evaluation ❏ To be able to predict the results when an expression is evaluated ❏ To understand implicit and explicit type conversion ❏ To understand and use the first four statement types: null,

expression, return, and compound

Chapter 3Chapter 3 Structure of a C ProgramStructure of a C Program

Page 2: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

2

3-1 Expressions

• An expression An expression (( 運算式運算式 )) is a sequence of operands is a sequence of operands (( 運算元運算元 )) and operators and operators (( 運算子運算子 )) that reduces to a that reduces to a single value. single value.

• Expressions can be simple or complex. Expressions can be simple or complex.

• An operator is a syntactical token that requires an An operator is a syntactical token that requires an action be taken. action be taken.

• An operand is an object on which an operation is An operand is an object on which an operation is performed; it receives an operator’s action.performed; it receives an operator’s action.

Page 3: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

3

FIGURE 3-1 Expression Categories

An expression always reduces to a single value.

NoteNote

Primary:

Name (variable): a b12 price calc …

Literal Constants ( 文數字常數 ) : 5 123.98 ‘A’ “Welcome”

後序 前序 三元一元 二元主要

Page 4: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

4

FIGURE 3-2 Postfix Expressions ( 後序運算式 )

運算子運算元

Page 5: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

5

FIGURE 3-3 Result of Postfix a++

(a++) has the same effect as (a = a + 1)

NoteNote

The operand in a postfix expression must be a variable.

NoteNote

Page 6: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

6

PROGRAM 3-1 Demonstrate Postfix Increment

Page 7: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

7

FIGURE 3-4 Prefix Expression ( 前序運算式 )

Page 8: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

8

FIGURE 3-5 Result of Prefix ++a

The operand of a prefix expression must be a variable.

NoteNote

(++a) has the same effect as (a = a + 1)

NoteNote

Page 9: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

9

PROGRAM 3-2 Demonstrate Prefix Increment

Page 10: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

10

If ++ is after the operand, as in a++, the increment takes place after the expression is evaluated.

If ++ is before the operand, as in ++a, the incrementtakes place before the expression is evaluated.

NoteNote

Page 11: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

11

FIGURE 3-6 Unary Expressions ( 一元運算式 )

Examples of Unary Plus And Minus ExpressionsTable 3-1

Page 12: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

12

FIGURE 3-7 Binary Expressions ( 二元運算式 )

Page 13: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

13

Both operands of the modulo operator (%) must be integral types.

NoteNote

Page 14: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

14

PROGRAM 3-3

Binary Expressions

Page 15: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

15

The left operand in an assignment expression must be a single variable.

NoteNote

Table 3-2 Expansion of Compound Expressions ( 複合運算式 )

Page 16: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

16

PROGRAM 3-4

Demonstration of Compound Assignments

Page 17: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

17

3-2 Precedence and Associativity

Precedence is used to determine the order in which Precedence is used to determine the order in which different operators in a complex expression are different operators in a complex expression are evaluated. Associativity is used to determine the order evaluated. Associativity is used to determine the order in which operators with the same precedence are in which operators with the same precedence are evaluated in a complex expression. evaluated in a complex expression.

Precedence ( 優先順序 )Associativity ( 結合性 )

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

Page 18: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

18

PROGRAM 3-5 Precedence

Page 19: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

19

Page 20: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

20

FIGURE 3-8 Left-to-Right Associativity

FIGURE 3-9 Right-to-Left Associativity

Page 21: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

21

3-3 Side Effects

A side effect is an action that results from the A side effect is an action that results from the evaluation of an expression. evaluation of an expression.

For example, in an assignment, C first evaluates the For example, in an assignment, C first evaluates the expression on the right of the assignment operator and expression on the right of the assignment operator and then places the value in the left variable.then places the value in the left variable.

Changing the value of the left variable Changing the value of the left variable is a side effect.is a side effect.

Page 22: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

22

3-4 Evaluating Expressions

Now that we have introduced the concepts of Now that we have introduced the concepts of precedence, associativity, and side effects, let’s work precedence, associativity, and side effects, let’s work through some examples.through some examples.

Expressions without Side EffectsExpressions with Side Effects

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

Page 23: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

Computer Science: A Structured Programming Approach Using C 23

PROGRAM 3-6

Evaluating Expressions

Page 24: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

24

3-5 Type Conversion

Up to this point, we have assumed that all of our Up to this point, we have assumed that all of our expressions involved data of the same type. But, what expressions involved data of the same type. But, what happens when we write an expression that involves happens when we write an expression that involves two different data types, such as multiplying an integer two different data types, such as multiplying an integer and a floating-point number? To perform these and a floating-point number? To perform these evaluations, one of the types must be converted.evaluations, one of the types must be converted.

Implicit Type ConversionExplicit Type Conversion (Cast)

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

Page 25: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

25

FIGURE 3-10 Conversion Rank

Page 26: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

Computer Science: A Structured Programming Approach Using C 26

PROGRAM 3-7

Implicit Type Conversion

Visual C++ 不支援 bool 型態

Page 27: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

Computer Science: A Structured Programming Approach Using C 27

Explicit Casts

PROGRAM 3-8

Page 28: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

28

3-6 Statements

A statement A statement (( 敘述敘述 )) causes an action to be performed causes an action to be performed by the program. It translates directly into one or more by the program. It translates directly into one or more executable computer instructions.executable computer instructions. You may have noticed that we have used a You may have noticed that we have used a semicolon at the end of the statements in our semicolon at the end of the statements in our programs. Most statements need a semicolon at the programs. Most statements need a semicolon at the end; some do not.end; some do not.

Statement TypeThe Role of the SemicolonStatements and Defined Constants

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

Page 29: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

29

FIGURE 3-11 Types of Statements

Page 30: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

30

FIGURE 3-12 Compound Statement

The compound statement does not need a semicolon.

NoteNote

Page 31: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

31

3-7 Sample Programs

This section contains several programs that you This section contains several programs that you should study for programming technique and style.should study for programming technique and style.

Page 32: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

Computer Science: A Structured Programming Approach Using C 32

PROGRAM 3-9

Calculate Quotient and Remainder

Page 33: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

Computer Science: A Structured Programming Approach Using C 33

PROGRAM 3-10

Print Right Digit of Integer

Page 34: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

Computer Science: A Structured Programming Approach Using C 34

PROGRAM 3-11

Calculate Average of Four Numbers

Page 35: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

35

PROGRAM 3-11 Calculate Average of Four Numbers

Page 36: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

Computer Science: A Structured Programming Approach Using C 36

PROGRAM 3-12

Convert Radians to Degrees

Page 37: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

37

PROGRAM 3-13

Calculate Sales Total

Page 38: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

38

PROGRAM 3-13 Calculate Sales Total

Page 39: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

39

PROGRAM 3-14

Calculate Student Score

Page 40: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

Computer Science: A Structured Programming Approach Using C 40

Page 41: 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions

Computer Science: A Structured Programming Approach Using C 41