720 likes | 724 Views
Learn about expression categories, rules of precedence and associativity, side effects, type conversion, and different statement types in C programming.
E N D
Chapter 3 Structure of a C Program 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 Computer Science: A Structured Programming Approach Using C
3-1 Expressions An expression is a sequence of operands and operators that reduces to a single value. Expressions can be simple or complex. An operator is a syntactical token that requires an action be taken. An operand is an object on which an operation is performed; it receives an operator’s action. Topics discussed in this section: Primary Expressions Postfix Expressions Prefix Expressions Unary Expressions Binary Expressions Computer Science: A Structured Programming Approach Using C
Note An expression always reduces to a single value. Computer Science: A Structured Programming Approach Using C
FIGURE 3-2 Postfix Expressions Computer Science: A Structured Programming Approach Using C
Note (a++) has the same effect as (a = a + 1) Computer Science: A Structured Programming Approach Using C
FIGURE 3-3 Result of Postfix a++ Computer Science: A Structured Programming Approach Using C
Note The operand in a postfix expression must be a variable. Computer Science: A Structured Programming Approach Using C
PROGRAM 3-1 Demonstrate Postfix Increment Computer Science: A Structured Programming Approach Using C
PROGRAM 3-1 Demonstrate Postfix Increment (continued) Note 1: The statement printf(“a++: %d\n”, a++); is equal to the following two statements: printf(“a++: %d\n”, a); a++; Note 2: The statement printf(“++a: %d\n”, ++a); is equal to the following two statements: ++a; printf(“++ a : %d\n”, a); Computer Science: A Structured Programming Approach Using C
FIGURE 3-4 Prefix Expression Computer Science: A Structured Programming Approach Using C
Note The operand of a prefix expression must be a variable. Computer Science: A Structured Programming Approach Using C
FIGURE 3-5 Result of Prefix ++a Computer Science: A Structured Programming Approach Using C
Note (++a) has the same effect as (a = a + 1) Computer Science: A Structured Programming Approach Using C
PROGRAM 3-2 Demonstrate Prefix Increment Computer Science: A Structured Programming Approach Using C
PROGRAM 3-2 Demonstrate Prefix Increment (continued) Computer Science: A Structured Programming Approach Using C
Note 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 increment takes place before the expression is evaluated. Computer Science: A Structured Programming Approach Using C
FIGURE 3-6 Unary Expressions Computer Science: A Structured Programming Approach Using C
Table 3-1 Examples of Unary Plus And Minus Expressions Computer Science: A Structured Programming Approach Using C
FIGURE 3-7 Binary Expressions Computer Science: A Structured Programming Approach Using C
Note Both operands of the modulo operator (%) must be integral types. Computer Science: A Structured Programming Approach Using C
PROGRAM 3-3 Binary Expressions Computer Science: A Structured Programming Approach Using C
PROGRAM 3-3 Binary Expressions (continued) Computer Science: A Structured Programming Approach Using C
PROGRAM 3-3 Binary Expressions (continued) Computer Science: A Structured Programming Approach Using C
Note The left operand in an assignment expression must be a single variable. Computer Science: A Structured Programming Approach Using C
Table 3-2 Expansion of Compound Expressions Computer Science: A Structured Programming Approach Using C
PROGRAM 3-4 Demonstration of Compound Assignments Computer Science: A Structured Programming Approach Using C
PROGRAM 3-4 Demonstration of Compound Assignments Computer Science: A Structured Programming Approach Using C
PROGRAM 3-4 Demonstration of Compound Assignments Computer Science: A Structured Programming Approach Using C
3-2 Precedence and Associativity Precedence is used to determine the order in which different operators in a complex expression are evaluated. Associativity is used to determine the order in which operators with the same precedence are evaluated in a complex expression. Topics discussed in this section: Precedence Associativity Computer Science: A Structured Programming Approach Using C
PROGRAM 3-5 Precedence Computer Science: A Structured Programming Approach Using C
PROGRAM 3-5 Precedence Computer Science: A Structured Programming Approach Using C
FIGURE 3-8 Left-to-Right Associativity Computer Science: A Structured Programming Approach Using C
FIGURE 3-9 Right-to-Left Associativity Computer Science: A Structured Programming Approach Using C
So you can have things like:int i =1;int j =2;i = j*= 2; // i = j = 4 afteri += j+=2; // j=6, i = 10 // after Computer Science: A Structured Programming Approach Using C
You can actually sayint i, sum;i = sum = 3 +1; // Right to left // associativityAnd i will equal 4, but it is not good programming practice. Computer Science: A Structured Programming Approach Using C
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression on the right of the assignment operator and then places the value in the left variable. Changing the value of the left variable is a side effect. Computer Science: A Structured Programming Approach Using C
PROGRAM 3-6 Evaluating Expressions Computer Science: A Structured Programming Approach Using C
PROGRAM 3-6 Evaluating Expressions Computer Science: A Structured Programming Approach Using C
PROGRAM 3-6 Evaluating Expressions Computer Science: A Structured Programming Approach Using C
Warning Advices When possible, avoid expressions that are too complicated and may cause confusion. Computer Science: A Structured Programming Approach Using C
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
FIGURE 3-10 Conversion Rank Computer Science: A Structured Programming Approach Using C
PROGRAM 3-7 Implicit Type Conversion Computer Science: A Structured Programming Approach Using C
PROGRAM 3-7 Implicit Type Conversion Computer Science: A Structured Programming Approach Using C
PROGRAM 3-7 Implicit Type Conversion Note: Not true. The value should be 0. Computer Science: A Structured Programming Approach Using C
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; // int / int performs an integer division 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
PROGRAM 3-8 Explicit Casts Computer Science: A Structured Programming Approach Using C
PROGRAM 3-8 Explicit Casts Computer Science: A Structured Programming Approach Using C
PROGRAM 3-8 Explicit Casts Computer Science: A Structured Programming Approach Using C