A Crash Course in C Today: Operators and Control Statements - PowerPoint PPT Presentation

About This Presentation
Title:

A Crash Course in C Today: Operators and Control Statements

Description:

A Crash Course in C. Today: Operators and Control Statements. Tuesday, ... postfix: gives the old value as the result and then ... Expression and semi ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 19
Provided by: michae684
Learn more at: https://stuff.mit.edu
Category:

less

Transcript and Presenter's Notes

Title: A Crash Course in C Today: Operators and Control Statements


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

2
A Brief Survey of Operators
  • expression operators
  • ( )
  • unary operators
  • !, , , --, , -
  • ! negation
  • !(0) gt 1
  • !(any non-zero value) gt 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
  • lt,gt, lt, gt
  • , !
  • 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
  • , , -, , /, !
  • x2 ?? xx2
  • 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 doesnt
  • Sometimes, order isnt specified
  • X f(x) g(x) f OR g can be evaluated
    first
  • BAD code
  • printf(d d \n, n, power(2,n))
  • ai i
  • So, never depend upon order of operations in code

9
Playing with Conditionals I
  • int main(void)
  • int x0, y10, w20, z, T1, F0
  • z (x 0) / logical operator result --gt
    0 or 1 /
  • z (x 0) / assignment operator result
    --gt ltxgt /
  • z (x 1)
  • z (x 15)
  • z (x ! 2)
  • z (x lt 10)
  • z (x lt 50)
  • return 0

10
Playing with Conditionals II
  • int main(void)
  • z ((xy) lt 10) /performs assignment,
    compares ltxgt with 10/
  • z (x5 ylt15)
  • z (x0 ygt5 w10)
  • z (x0 ygt5 w20)
  • z (T T F x x) / gt F /
  • z (F T x x) / gt 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 gt A c lt Z)
  • return c a A
  • else
  • return c

Possible characters in ASCII
abcxyz ABCXYZ
13
Beware of nesting errors
  • Wheres the mistake?
  • if(n gt0)
  • for (i 0 i lt n i)
  • if(si gt 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 gt 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(i0 i lt n i)
  • for(j 0 j lt m j)
  • if(ai bj)
  • goto found
  • / do this if not found /
  • found
  • / got a match! /

18
C U all tomorrow _at_ 8!!
Write a Comment
User Comments (0)
About PowerShow.com