ECE206 Programming - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

ECE206 Programming

Description:

prefix form: x and --x. postfix form: x and x-- Precedence of ... Round-off errors. Finite memory space to represent infinite digits of real numbers ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 17
Provided by: QI1
Category:

less

Transcript and Presenter's Notes

Title: ECE206 Programming


1
ECE206 - Programming
  • Lecture 3 Operators and Data Type
  • 08/30/07

2
Recap
  • Programming environment
  • Good programming style
  • ALU design

CPU
3
Arithmetic operators
  • , -, , /, (modulus), --,
  • Increment and Decrement
  • prefix form x and --x
  • postfix form x and x--
  • Precedence of arithmetic operators
  • highest , --
  • middle , /,
  • lowest , -

4
Examples for , -- operators
... x 10 y x cout ltlt y // whats
y? cout ltlt x // whats x? y y cout ltlt
y // whats y? y coutltlty //whats
y?
... x 10 y x cout ltlt y // whats
y? cout ltlt x // whats x? y y cout
ltlt y // whats y? y coutltlty //whats
y? ...
  • When using the prefix form, C performs the
    increment or decrement prior to obtaining the
    operands value
  • When using the postfix form, C will obtain the
    operands value before incrementing or
    decrementing it.

5
Examples for /, operators
include ltiostreamgt using namespace std int
main() int x, y x 10 y
3 cout ltlt x / y cout ltlt \n cout
ltlt x y return 0
  • ,-,,/ can be applied to any data type
  • the modulus operation yields the remainder of
    an integer division, so it can NOT be used on
    type float or double.
  • When / is applied to an integer or a character,
    any remainder will be truncated.

6
Relational operators
  • Compare values, mostly used in control structures
  • gt greater than
  • gt greater than or equal to
  • lt less than
  • lt less than or equal to
  • equal to
  • ! not equal to

7
Logical operators
  • Deal with multiple conditions, also common used
    in control structures
  • AND
  • OR
  • ! NOT
  • Short circuit evaluation
  • expr1 expr2 expr3
  • expr1 expr2 expr3
  • How to improve performance?

8
Assignments
  • a expr
  • the value of expr is computed and saved in
    variable a
  • a a b ? a b
  • the values of b and a are summed and the result
    is stored in a
  • Similar for a - b a b a / b a b

9
Conditional operator
  • ?
  • One of C most fascinating operator
  • Require three parameters
  • Condition is evaluated. If true, return exp1 If
    false, return exp2.
  • Can be used to replace if-else statement

condition ? exp1 exp2
cout ltlt (guess magic ? Right
...Sorry, you're wrong.)
10
Variable declaration
  • Specify name and type int a float b1.0
  • Reserve space in memory and optionally
    initialize.
  • Use reasonably long and informative names. DO NOT
    USE SINGLE-CHARACTER NAMES for variables other
    than iterators. Its case sensitive for C
    program.
  • Variable type int, short, long, float, double,
    char, boolean, pointers ()
  • The size of a variable is measured by how many
    bits it takes
  • Describe the variables purpose in a comment.
  • Variables need to be declared before the first use

11
Basic data types
Type Typical Bit Width Typical
Range char 8 -128 to 127 int
(32-bit) 32 -2,147,483,648 to 2,147,483,6
47 float 32 3.4E-38 to 3.4 E38 double 64
1.7E-308 to 1.7E308 bool 1 true or
false void N/A valueless
12
Round-off errors
  • Finite memory space to represent infinite digits
    of real numbers
  • Apply equality operator to float/double data is
    dangerous

double x 3.35e17 double y x20 if (ygtx)
cout ltlt y is greater than x"
Whats the output?
double x, y .. if (x y) cout ltlt x
equals to y"
double x, y .. if ( fabs(x-y) lt 1e-10 )
cout ltlt x equals to y"
13
Mixed Type Expression
  • Data of different types in a single expression
    are converted to the same type
  • Explicit type conversion cast

int m9, n5 float x, y x float(m)/n y
m/n cout ltlt x ltlt endl cout ltlt y ltlt endl
char ch int i float f double d result ch /
i f d (f i)
14
Constants
  • Constants refers to fixed values that cannot be
    altered by the program.
  • Example constants
  • define intCons 50
  • define charCons c
  • define USAGE Usage Hello\n
  • Another way
  • const int intCons50
  • const char charConsc
  • Backslash character constants
  • \n (new line), \t (tab)

15
Common errors
int x y 5 if (x y) cout ltlt x equals
to y\n if (3ltxlt7) cout ltlt x is between 3
and 7\n If (x gt y) cout ltlt x is greater
or equal than y\n cout ltlt x divided by y
equals to ltlt x/y ltlt \n
16
What do you need to know
  • Operators
  • Arithmetic
  • Relational
  • Logical
  • Precedence of operators
  • Assignment operators
  • Data type
  • Variable vs. Constant declaration
  • Basic data type and their size
Write a Comment
User Comments (0)
About PowerShow.com