180 likes | 281 Views
A Crash Course in C Today: Operators and Control Statements. Tuesday, January 4 th , 2005 8:00PM Michael Shaw Jen Selby Sponsored by SIPB. A Brief Survey of Operators. expression operators ( ) unary operators !, ~, ++, --, +, - !: negation !(0) ==> 1 !(any non-zero value) ==> 0
E N D
A Crash Course in CToday: Operators and Control Statements Tuesday, January 4th, 2005 8:00PM Michael Shaw Jen Selby Sponsored by SIPB
A Brief Survey of Operators • expression operators • ( ) • unary operators • !, ~, ++, --, +, - • !: negation • !(0) ==> 1 • !(any non-zero value) ==> 0 • ++/--: increment/decrement by 1 • prefix: increments the variable and gives the new value as the result • postfix: gives the old value as the result and then increments the variable
Statements and Expressions • Statement: Fundamental unit of C code • Expression: part of a statement • Expression and semi-colon is a statement • Analogy: Statement is to expression as sentence is to clause
A Brief Survey of Operators • arithmetic operators • *, / , % • % is called modulus division (remainder) • +, - • Examples: X = 5 + 3; Expressions Statement
Comparison Operators • comparison operators • the logical operators return a 1 (true) or 0 (false) • for any conditional test, any non-zero value is true and a 0 is false • <,>, <=, >= • ==, != • == is the equality comparision (not assignment =) • A common error is using the assignment operator = when the logical operator == is required, e.g. (x = 2) instead of (x == 2); both are valid expressions, so the compiler will not indicate an error.
Assignment Operators • assignment operators • =, +=, -=, *=, /=, %= ! • x+=2 x=x+2 • NOTE: = operator is VERY DIFFERENT from the == operator
Where order matters … • logical operators • &&, || • logical operators are evaluated until an expression is known to be true or false • , (comma) • combines separate expressions into one • evaluates them from left to right • the value of the whole expression is the value of the right most sub-expression
… and where it doesn’t • Sometimes, order isn’t specified • X = f(x) + g(x) … f OR g can be evaluated first • BAD code • printf(“%d $d \n”, ++n, power(2,n)); • a[i] = i++; • So, never depend upon order of operations in code
Playing with Conditionals I • int main(void){ • int x=0, y=10, w=20, z, T=1, F=0; • z = (x == 0); /* logical operator; result --> 0 or 1 */ • z = (x = 0); /* assignment operator; result --> <x> */ • z = (x == 1); • z = (x = 15); • z = (x != 2); • z = (x < 10); • z = (x <= 50); • return 0; }
Playing with Conditionals II • int main(void) { • z = ((x=y) < 10); /*performs assignment, compares <x> with 10*/ • z = (x==5 && y<15); • z = (x==0 && y>5 && w==10); • z = (x==0 || y>5 && w==20); • z = (T && T && F && x && x); /*** ==> F; ***/ • z = (F || T || x || x); /*** ==> T; ***/ • return 0; • }
Conditionals: if and else • Syntax: if (expression) statement else if (expression) statement else statement
Our own tolower int lower(int c) { if (c >= ‘A’ && c <= ‘Z’) return c + ‘a’ – ‘A’; else return c; } Possible characters in ASCII … … … abc…xyz … … … ABC…XYZ … … …
Beware of nesting errors Where’s the mistake? if(n >=0) for (i = 0; i < n; i++) if(s[i] > 0) { printf(“…”); return i; } else printf(“error – n is negative \n”);
Conditional Expressions • expression1 ? expression2 : expression3 • Shorthand for conditional if(expression1) expression2 ; else expression3 ; Example: z = (a > b) ? a : b ; /* z = max(a,b) */
More on statements • Expression + semi-colon = statement • ie: x; is a statement • “block of statements” • { statement; … statement; } = statement • Like in a loop … whats a loop?
Looping around … while (expression) statement for(expression1; expression2; expression3;) statement Same as: expression1; while(expression2) { statement; expression3; }
(Almost) Never use a goto • Goto statements move you to another piece of code for(i=0; i < n; i++) for(j = 0; j < m; j++) if(a[i] == b[j]) goto found; /* do this if not found */ found: /* got a match! */