Unformatted text preview: EECS 110: 2b How to Change Numbers: Operators and Expressions
Jack Tumblin [email protected] 1 C is a `Structured' Language: Literals and Variables
(with their `data types') Operators Expressions Statements Functions Libraries Programs `Systems' (e.g. MacOS, TCP/IP, Web)
2 C is a `Structured' Language: Literals and Variables
(with their `data types') Operators Expressions Statements Functions Libraries Programs `Systems' (e.g. MacOS, TCP/IP, Web)
3 Operators: How to Change Numbers
You've met the `Assignment' operator = ... Assignment All the rest fit in these three big categories: Arithmetic (+,,*,...) given number(s) make a new number Examples? 3.0+5.2, 1/3, 1.0/3.0,
... Relational (>,<,...) (later in book: section 5.1) given number(s), make a new true/false Examples: 1.0 > 2.0, 5<10, 7.00001 >=
7.0, ... Logical (&&,,...) (later in book: section 5.1) given true/false(s), make a new true/false Examples? TRUE && TRUE, FALSE  TRUE,
TRUE && FALSE, FALSE  FALSE
4 Arithmetic Operators For all of math, C has just 5 basic, builtin arithmetic operators:
(look for the fancy math functions (sin,cos,log, etc.) in librariesmath.h) + * / % Add Subtract, or negate Multiply Divide Modulus
! ew N
5 A New Arithmetic Operator: % % computes the remainder after the first operand is divided by the second. Examples: (5 % 2) is 1, and (6 % 3) is 0 Useful for making cycles of numbers: For an int variable `x',
if x is: (x%4)is: 0 0 1 1 2 2 3 3 4 0 5 1 6 2 7 3 8 0 9 1 10 ... 2 ... 6 Arithmetic Operators: and The dash has TWO meanings: subtraction operator (binary : uses 2 operands) Example: 30 5 negation operator (unary : uses 1 operand) Example: 4, 5 + 9 (? So what's an "operand" ?)
7 And More Assignment Operators... To avoid tedium, C has extra assignment ops:
just write just write just write just write just write a += b; instead of a = a + b; a = b; instead of a = a  b; a *= b; instead of a = a * b; a /= b; instead of a = a / b; a %= b; instead of a = a % b; 8 Precedence and Associativity
(arithmetic operators) How would you evaluate the expression
17 * 8  2 ? Is it 17 * (8  2) or (17 * 8)  2 ? These two forms give different results. We need rules! 9 Precedence and Associativity
(arithmetic operators) (inner parintheses) unary minus * + /  % Higher precedence (more precedence? computed sooner) Leftto right associativity (except unary minus) (compute lefttoright for operators of same precedence)
Find a complete chart on the book's inside cover...
10 Precedence and Associativity
(arithmetic operators) Examples:
X =17  2 * 8 ? Y = 17  2  8? Ans: X=17(2*8) , X=1 Ans: Y = (172)8, Y=7 Z = 10 + 9 * ((8 + 7) % 6) + 5 * 4 % 3 *2 + 1 ? Not sure? Confused? then use parentheses in your code! (write code for forgetful humans, not perfect machines)
11 Two Very Strange Operators:
prefix, postfix notation Increment, decrement operators: ++ ,  Instead of statement you can use either Instead of statement you can use either
num = 10; ans1 = ++num; First, incr. num to 11, then assign num to ans1. num = num + 1; num++; or ++num; num = num  1; num; or num;
Do you see how C++ got its name? **BUT BE VERY CAREFUL!** (see book)
num = 10; ans2 = num++; First assign num to ans2, then incr. num to 11.
12 What is a `Side Effect' ?
Side Effect == any value change(s) that are done by an operator Obvious Side Effects; `=' changes hats value
hats = 4; hats = hats*2; NotSoObvious Side Effects; what happens?
scanf(" %d", &hats); printf("and get %d coats", ++hats);
13 VERY BAD IDEA: Complex Embedded Side Effects
Legal in C (book: pg. 112), but DON'T! CONFUSING, ANNOYING to human readers (can hide nasty bugs in a program) OBSOLETE, UNNECESSARY (no longer makes C programs faster) Examples:
gloves = 2*(hats = coats++); printf("washed %d, got %d",socks,socks); scanf("look for %d",&(hats++)); Bug!
14 Operators Expressions Expressions: the smallest `grains' of computing, the single units of evaluation, made of one operator and its input terms (or `operands') Examples: n+5 a<7.2 c=d (Book) `terms' or `operands' include: Constants, variables, functions, or other expressions ... C programs are executed stepbystep: just one expression at a time, one after the other.
15 Expressions Expressions are often NESTED:
(((n+5)<=a) && q) This expression is evaluated and becomes a term in... 16 Expressions Expressions are often NESTED:
(((n+5)<=a) && q) this expression, and it is evaluated and becomes a term in... 17 Expressions Expressions are often NESTED:
(((n+5)<=a) && q) this expression. (NOTE: `nesting' means red circles NEVER overlap) A+B+C
18 Expressions Compiler makes a `dummy' (or `hidden' or `temporary') variable to hold value of each expression:
(((n+5)<=a) && q) d1 d2 d3
19 Expressions: Assignment =
= is an operator! It means `copy
' It's part of an expression (operator, terms) with 2 Terms: righthand side(RHS): source value lefthand side (LHS): dest. variable myvar = ((n+5)<=a) && q )
C OP Y
d3
20 Expressions: Assignment =
= is an operator! It means `copy
ISE !!! ISE !!! !!! SURPR !!! SURPR o! too! mmy var to ummy var Makes a du Makes a d ' It's part of an expression (operator, terms) with 2 Terms: righthand side(RHS): source value lefthand side (LHS): dest. variable myvar = ((n+5)<=a) && q )
C OP Y
d3
21 d4 Expressions "Why should we care? The compiler does it all for us!"
myvar = (((n+5)<=a) && q) Because: Every nested step uses `phantom' variables, COMPILER decides on types for each one. Unintended decision? Unintended Answer! (a source of subtle, frustrating errors)
22 Expressions: Type Promotion
Hidden variable's data type is chosen by simple rules: All terms have same type? Then so does the expression's dummy variable. Terms have mixed types? May be trouble! C chooses the `largest' term type (saves data) char int float double ( larger) But sometimes that's not enough:
my_float = 5/2; /*result: 2.0 */
23 SOLUTION: `Type Casting' (pg. 118) FORCES a temporary, local type change (each cast sets type of one term in one expression)
Int i,j; float f1,f2,f3; i=5; fl = f2 = f3 = f2 = j=2; i/ j; (float)i/(float)j; (float)(i/j); (float)i/ j; /* /* /* /* result: result: result: lucky!; 2.0 2.5 2.0 2.5 */ */ */ */ `Cast' i and j to type float When in doubt, USE CASTING. It shows your intentions, CASTING and stops some subtle bugs before they start!
24 Expressions Statements Expressions are often NESTED:
(((n+5)<=a) && q) this is an expression. But is this a statement? (have you read Chapter 3 yet?) (((n+5)<=a) && q); Why, or why not?
25 Expressions Statements (((n+5)<=a) && q); A `statement' in C is: zero or more expression(s) terminated by a semicolon ; For each expression (each red circle), C makes a (hidden) temporary variable (Recall: some expressions have sideeffects) Semicolon means: `Done! Destroy all temporary variables!'
26 Statements Statement An action to be performed by the program; and A set of expressions, possibly nested. Statements always terminate in a semicolon Examples:
counter = 3; // an assignment statement. printf("Hello"); // messageprinting statement. counter = 3 // NOT a statement: this is just an // expression. It will become a statement when we // add a semicolon at the end.
27 ...
View
Full Document
 Spring '11
 Tumblin
 Computer Science, Algebra, relational operator, Associativity, arithmetic operators

Click to edit the document details