Chap-03-2.ppt

advertisement
3-3 Side Effects
A side effect is an action that results from
evaluation of an expression. For example, in
assignment, C first evaluates the expression on
right of the assignment operator and then places
value in the left variable. Changing the value of
left variable is a side effect.
Computer Science: A Structured Programming Approach Using C
the
an
the
the
the
1
PROGRAM 3-6 Evaluating Expressions
Computer Science: A Structured Programming Approach Using C
2
PROGRAM 3-6 Evaluating Expressions
Computer Science: A Structured Programming Approach Using C
3
PROGRAM 3-6 Evaluating Expressions
Computer Science: A Structured Programming Approach Using C
4
Warning
Computer Science: A Structured Programming Approach Using C
5
3-5 Type Conversion
Up to this point, we have assumed that all of our
expressions involved data of the same type. But, what
happens when we write an expression that involves
two different data types, such as multiplying an integer
and a floating-point number? To perform these
evaluations, one of the types must be converted.
Topics discussed in this section:
Implicit Type Conversion
Explicit Type Conversion (Cast)
Computer Science: A Structured Programming Approach Using C
6
FIGURE 3-10 Conversion Rank
Computer Science: A Structured Programming Approach Using C
7
PROGRAM 3-7 Implicit Type Conversion
Computer Science: A Structured Programming Approach Using C
8
PROGRAM 3-7 Implicit Type Conversion
Computer Science: A Structured Programming Approach Using C
9
PROGRAM 3-7 Implicit Type Conversion
Computer Science: A Structured Programming Approach Using C
10
Implicit Conversion of Ints and
Floats
int i = 3;
float f = 2.5;
i = f;
// i will equal 2 after this
f = 3/i; // f will equal 1.0 after this
f = i + 0.6; // f will equal 2.6 after this
i = 2.0 * 2.4; // i will equal 4 after this
Computer Science: A Structured Programming Approach Using C
11
PROGRAM 3-8
Explicit Casts
Computer Science: A Structured Programming Approach Using C
12
PROGRAM 3-8
Explicit Casts
Computer Science: A Structured Programming Approach Using C
13
PROGRAM 3-8
Explicit Casts
Computer Science: A Structured Programming Approach Using C
14
3-6 Statements
A statement causes an action to be performed by the
program. It translates directly into one or more
executable computer instructions.
You may have noticed that we have used a
semicolon at the end of the statements in our
programs. Most statements need a semicolon at the
end; some do not.
Computer Science: A Structured Programming Approach Using C
15
FIGURE 3-12 Compound Statement
Computer Science: A Structured Programming Approach Using C
16
Download