1 / 18

A Crash Course in C Today: Operators and Control Statements

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

orrick
Download Presentation

A Crash Course in C Today: Operators and Control Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Crash Course in CToday: Operators and Control Statements Tuesday, January 4th, 2005 8:00PM Michael Shaw Jen Selby Sponsored by SIPB

  2. 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

  3. 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

  4. A Brief Survey of Operators • arithmetic operators • *, / , % • % is called modulus division (remainder) • +, - • Examples: X = 5 + 3; Expressions Statement

  5. 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.

  6. Assignment Operators • assignment operators • =, +=, -=, *=, /=, %= ! • x+=2  x=x+2 • NOTE: = operator is VERY DIFFERENT from the == operator

  7. 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

  8. … 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

  9. 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; }

  10. 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; • }

  11. Conditionals: if and else • Syntax: if (expression) statement else if (expression) statement else statement

  12. 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 … … …

  13. 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”);

  14. Conditional Expressions • expression1 ? expression2 : expression3 • Shorthand for conditional if(expression1) expression2 ; else expression3 ; Example: z = (a > b) ? a : b ; /* z = max(a,b) */

  15. 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?

  16. Looping around … while (expression) statement for(expression1; expression2; expression3;) statement Same as: expression1; while(expression2) { statement; expression3; }

  17. (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! */

  18. C U all tomorrow @ 8!!

More Related