<<

Reading Assignment Preview

Arithmetic Expressions

Assignment, lValues and rValues

K.N. King Chapter 5 Logical Expressions

Conditional expressions K.N. King Chapter 6

Operator associativity K.N. King Section 24.1 Operator precedence

Type conversions

81 82

Arithmetic Operators Assignment The arithmetic operators are used to compute the values of integer and real (float/double) expressions. lValues and rValues

Assignment is the act of computing the value of some expression and making the arithmetic operators in are that expression the value of some variable. ¡ negation ¢

An lValue represents an object stored in the memory of the computer.

£ ¤ Variables are lValuesa

¡ % modulus

An lValue is used in an assignment to indicate where the value of the

For all operators except %, result is of type double if either or both operands expression is to be stored in memory. are of type double; if both operands are of type integer then the result is of

type integer. An rValue is another name for an arbitrary expression. The modulus operator only takes integer operands % always returns a rValues can be used anywhere that an expression is allowed. integer result. Division of integers truncates the fractional part. In particular an rValue is required on the right side of an assignment.

The modulus operation A % B returns the remainder of A divided by B. aOther kinds of lValues will be discussed later in the term.

It is always true that 0 <= A%B ¥ B

83 84 Assignment Operator Combined Operate and Assign

a lValue = rValue For (almost) any binary operators lValue binaryOp= rValue is equivalent to:

¦ Assignment is an operator in C. lValue = lValue binaryOp rValue The left operand of this operator must be an lValue (usually a variable).

Examples

¦ The right operand of this operator must be an rValue . The rValue can be any arbitrary expression involving constants, variables, operators x+=1; /*x=x+1*/ and function calls. The result of the assignment operator is also an rValue . y*=x+z; /*y=y*(x+z)*/ i%=7 /*i=i%7 */ ¦ Examples:

height = 8; Good Style: Don’t use operate and assign if it makes your program difficult volume = height * length * width; to understand. i=j=k=0; /*Equivalent: i=(j=(k=0));*/ abinary operators are arithmetic operators that require two ( left and right) operands, e,g, + 12 = i; /*** WRONG, lValue can’t be a constant ***/ I + j = 0; /*** WRONG, lValue can’t be an expression **

85 86

Increment and Decrement Operators ++ and --

¦ The increment ( ++ ) and decrement ( -- ) operators can be used to efficiently add or Logical Expression in C subtract one from a variable

The C language contains a number of operators that work on logical values, Both of these unarya operators take an lValue as an operand. i.e. true and false . Logical values are often called Boolean values. They add or subtract one from value of the variable in memory.

The result of the operation is a rValue which can be used like any other rValue . The relational operators compare the values of two expressions and produce a logical value.

¦ If the operator occurs before the lValue then the rValue is the value of the variable after it is incremented or decremented. The logical operators can be used to combine logical values to produce a If the operator occurs after the lValue then the rValue is the value of the variable logic valued result. before it is incremented or decremented.

Historically false is represented internally by the value zero and true is

¦ k=1;Example: represented by any non-zero value. printf("k is %d\n", ++k); /* prints "k is 2" */

printf("k is %d\n", k); /* prints "k is 2" */ Many programmers use the definitions k=1; #define FALSE ( 0 ) printf("k is %d\n", k--); /* prints "k is 1" */ #define TRUE ( 1 ) printf("k is %d\n", k); /* prints "k is 0" */ typedef int Bool ;

aA unary operator has one operand.

87 88 Relational Operators

¦ The relational (comparison) operators in C are Logical Operators == equal != not equal The logical operators in C are used to combine simple logical values into > greater than >= greater than or equal more complicated logical values. The logical operators are < less than <= less than or equal && logical and The operators are typically used as || logical or expressionleft relationalOperator expressionright ! logical not

¦ The relational operators compare the values of expressionleft and expressionright

and produce a logic value describing the result of the comparison. The definitions for these operators are

¦ All scalar types can be compared. Only scalar types can be compared. AB! A A || B A && B

¦ WARNING: BE REALLY REALLY CAREFUL false false true false false don’t confuse assignment ( = ) with equality compare ( == ) false true true true false K = 3 is always true AND CHANGES K !!! true false false true false

true true false true true

89 90

Logical Operators are CONDITIONAL in C Example: Relational and Logical Operators

Assume that ¨ ¨ ¨ . Operator Meaning x 1 © y 4 © z 14

A && BifAistrue then B else false

A || BifAistrue then true else B x <= 1 && y == 3 false Note that the right expression ( B ) is only evaluated if it is needed. x <= 1 || y == 3 true Many C programs use the conditional nature of the logical operators. !(x > 1) true ¥

Example: 0 § K&&K ARRAY SIZE && A[ K ] == 0 ! x > 1 false (!!) WARNING: Be VERY careful to not use the bitwise operatorsa when you want the !(x <= 1 || y == 3) false logical operators. Operator Logical Bitwise x >= 1 && y == 3 || z < 14 false

Not !˜

And && &

Or || |

aTo be discussed later, See King Section 20.1

91 92 HOW TO Use Logical Operators Use order of operands to guarantee safe evaluation of the right operand. ¥

0 § K&&K ARRAY SIZE && A[ K ] == 0

Variables (usually char or int ) can store logical values for later use.

int xTooLow ; Use parentheses generously to make logical expressions easy to read.

xTooLow = X ¥ 0.75 ; Use formatting to make long logical expressions easier to read (A<3 ||!B ||C!=17)

Use && , || and ! to combine logical results. && ( !D || E <= 1.375 || F )

– ! inverts the logical sense of the expression && ( X < Y || Z >= 13.8 )

– && produces true if BOTH operands are true Long logical expressions MAY indicate BAD THINKING make sure each long – || produces true if EITHER operand is true expression is really necessary. Sometimes computing the logical inverse of an expression and using ! is simpler.

For efficiency order operands to produce an early result put term most likely to be false first for && put term most likely to be true first for ||

93 94

Try to minimize the number of ! operators in a logical expression to make it Conditional Expression easier to understand. ( boolExpn ? expntrue : expn false ) DeMorgan Laws ! A || ! B replace with ! (A &&B)

! A&&! B replace with ! (A|| B)

Invert Relations ! (X== Y ) replace with X != Y The value of the boolean expression boolExpn selects one of expntrue or as the value of the entire construct ! (X< Y ) replace with X >= Y expn false

Cancellation !!X replace with X Good Style: Always enclose conditional expressions in parentheses for readability and to avoid operator precedence problems

Examples: (X> Y?X:Y) /*max(X,Y)*/ (1<= N&&N<= LIMIT ? N : 1 ) /* Bounded N */

95 96 Comma Operator Sizeof Operator

expressionleft , expressionright sizeof ( object )

The comma operator is used to put several expressions in places where normally only a single expression is allowed.

The sizeof operator returns the size in bytes of the object.

expressionleft is evaluated and its value is discarded. In the most common case object is a type-name , but object can also be a

expressionright is then evaluated and then becomes the value of the entire constant, variable or expression. expression (an rValue ).

Example:

Good Style: Use the comma operator sparingly when you really need a list of intI; expressions. Do not use it to write hard to understand programs. sizeof ( int ) ; sizeof ( I ) ; sizeof ( 23 ) ; sizeof ( I + 32768 ) ;

97 98

Operator Precedence Operator Associativity

Operator Precedence determines the order in which operators in an Operator Associativity determines the order in which operators of equal expression are evaluated. An operator with higher precedence will be precedence will be evaluated in an expression. evaluated before an operator of lower precedence.

left -> right associativity means the operators will be evaluated from left to

Examples: * has higher precedence than + so right as the occur in the expression so A * B + C means ( A * B ) + C and not A * ( B + C ) A * B / C means ( A * B ) / C and not A * ( B / C ) Arithmetic operators have higher precedence than relational operators so A - C + 3 means ( A - C ) + 3 and not A - ( C + 3 ) A+B< C * D means ( A + B ) < ( C * D ) and not A + ( B < C)*D

right -> left associativity means the operators will be evaluated from right to

The precedence rules in C are mostly intuitive and sensible. left as they occur in the expression, so Use parentheses when in doubt or to force a particular order of evaluation. I = J = K means I = (J= K ) and not ( I = J)= K

WARNING: Be careful when mixing operators from different precedence Use parentheses if the default associativity isn’t what you want. classes in an expression.

99 100 Operator Precedencea Type Conversions

Operators Associativity C does reasonable automatic type conversions () [] -> . -> left right narrower operand -> wider operand ! ˜ ++ -- + - * & (type) sizeof right -> left when information is not lost */% left -> right +- left -> right Examples: << >> left -> right char -> int <<=>>= left -> right == != left -> right short -> intorlong & left -> right float -> double ˆ left -> right int -> float or double | left -> right

&& left -> right See King Section 7.5 for full details || left -> right ?: right -> left

=+=-=*=/=%=&=ˆ=|=<<=>>= right -> left

, left -> right aSee King Appendix B. Some of these operators will be discussed later.

101 102