DOC PREVIEW
UIUC ECE 190 - Operators and control structures in C

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ECE 190 Lecture 04 January 27, 2011 1 V. Kindratenko Operators and control structures in C Lecture Topics - Operators - Conditional constructs - Example Lecture materials Textbook § 12.3., 12.4, 13.1, 13.2 Homework None Machine problem MP1.1 due February 2 at 5pm submitted electronicallyECE 190 Lecture 04 January 27, 2011 2 V. Kindratenko Operators - Operators are the language’s mechanisms for manipulating values o Example: z = x * y;  Expression x * y is evaluated and  the resulting value is assigned to variable z - three things to know about operators: o function – what it does o precedence – in which order are operators combined o associativity – in which order operators of the same precedence are executed Assignment operator - = is the assignment operator o The value on the right of the assignment operator will be assigned to the variable whose name is provided on the left side of the operator  The value actually will be copied to the memory associated with the variable name on the left of the assignment operator o Example: a = b + c; - If the type of the value on the right of the assignment operator is not the same as the type of the variable on the left of the assignment operator, data type conversion will take place - Example: o int x, a; double y, z; z = x + y; <- value of x will be converted to double type before the expression is evaluated a = x + y; <- once evaluated, the sum will be converted to int type before assigning it to variable a Arithmetic operators - Defined for int, float, and char data types - *, +, -, /, % (modulus) - Precedence: *, /, % are executed first, followed by + and – o 2+3x4 = 2+(3x4) - Associativity: operators with the same precedence are executed sequentially o 2+3-4+5=((2+3)-4)+5 - Parenthesis can be used to override the evaluation order - While floating-point addition and multiplication are both commutative (a + b = b + a and a*b = b*a), they are not necessarily associative. That is, (a + b) + c is not necessarily equal to a + (b + c). (Post/pre-) in-/de-crement - Borrowed from assembly - ++, --ECE 190 Lecture 04 January 27, 2011 3 V. Kindratenko - Examples o x = 4; y = x++; /* post increment: y = 4, x = 5 */ o z = ++x; /* pre-increment: z = 6, x = 6 */ Bitwise operators - normally used with unsigned int and unsigned char data types, they really just manipulate bits without regards to the numerical value/meaning - ~ bitwise NOT: ~x - & bitwise AND: y & x - | bitwise OR: x | y - ^ bitwise XOR: x ^ y - << left shift: x << y (for positive int same as x*2y) - >> right shift: x >> y (for positive int same as x/2y) - Examples o h = f & g; o h = g << 4; o h = ~f | ~g; Relational operators - > less - >= less or equal - < more - <= more or equal - == equal - != not equal - Examples o q = (321 == 83); /*q = 0 */ o q = (x == y); /* q is one if x == y, otherwise it is 0 */ o h = f <= g; /* h is 1 when f is less of equal g */ Logical operators - value of 0 is referred to as logically false - value of ~0 is referred to as logically true - ! - logical NOT - && logical AND - || logical OR - Example: y = ( 10 <= x) && (x <= 20) /* true if 10 <=x<=20, otherwise false */ Expressions with multiple operators - Example: y = x & z + 3 || 9 – w % 6; - Applying operators precedence rules, this is equal to o y = (x & (z + 3)) || (9 – (w%6));ECE 190 Lecture 04 January 27, 2011 4 V. Kindratenko - Table 12.5 from the textbook lists operators precedence sequence - To be sure the code does what you want it to do, just use parenthesis! Special operator: conditional - x = y ? z : t; - if y is TRUE, x = z, otherwise, x = t o works like a MUX - example: x = (y > 0) ? y : -y; /* x = |y| */ Compound-assignment operators - var1 operator= var2; in C is the same as var1 = var1 operator var2; - += addition assignment, a += b same as a = a + b - -= subtraction assignment - *= multiplication assignment - /= b division assignment - %= b modulo assignment - &= b bitwise AND assignment - |= b bitwise OR assignment - ^= b bitwise XOR assignment - <<= b bitwise left shift assignment - >>= b bitwise right shift assignment Conditional constructs - There are three basic programing constructs: sequential, conditional, iterative - Sequential construct means that C program instructions (statements) are executed sequentially, one after another: - Conditional construct means that one or another statement will be executed, but not both, depending on some condition: Statement 1 Statement 2 Statement 3ECE 190 Lecture 04 January 27, 2011 5 V. Kindratenko - In C, conditional constructs can be implemented using if, if-else, or switch statements if statement - if (condition) action; - examples o if (x < 0) x = -x; /* simple statement */ o if (x > 5 && x< 25) { y = x * x +5; /* compound statement */ printf(“y=%d\n”, y); } - action statement can be simple, as in fist example, or compound, as in second example if-else statement - if (condition) action_when_condition_is_true; else action_when_condition_is_false; Statement 1 Statement 2 Test condition action condition true falseECE 190 Lecture 04 January 27, 2011 6 V. Kindratenko - examples o if (x < 0) x = -x; else x = x * 2; o if (x > 5 && x< 25) { y = x * x +5; printf(“y=%d\n”, y); } else printf(“x=%f\n”, x); - associating ifs with elses o in a cascaded if-else statement, an else is associated with the closest if  that is, when not using braces, which is not a good practice if (x != 0) if (y > 3) z = z / 2; else z = z + 2; same as if (x != 0) { if (y > 3) z = z / 2; else z = z + 2; } o if we really want to associate else with the first if, then we should use braces:  if (x != 0) { if (y > 3) z = z / 2; } else z = z + 2; o use braces to write clear and readable code! - common programming errors o if (x = 2) using assignment operator instead of == action 1 condition true false action 2ECE 190 Lecture 04 January 27, 2011 7 V. Kindratenko Example …


View Full Document
Download Operators and control structures in C
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Operators and control structures in C and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Operators and control structures in C 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?