Operator and Data Type - Lecture Slides | ECE 206, Study notes of Electrical and Electronics Engineering

Material Type: Notes; Class: Electrical Engr Computations; Subject: Electrical And Computer Engr; University: University of Tennessee - Knoxville; Term: Fall 2007;

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-8vt-1
koofers-user-8vt-1 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
ECE206 - Programming
Lecture 3 – Operators and Data
Type
08/30/07
2
Recap
Programming environment
Good programming style
ALU design
int main()
{cout <<…
}
001001010
010010100
010010010
010100101
000110101
011111000
001010101
001001010
010010100
000110101
011111000
001010101
010100101
001001010
010010100
000110101
001010101
010100101
011111000
complier linker loader
Files saved on disk Primary memory
*.cpp *.o *.exe
Pre-complied lib
CPU
pf3
pf4
pf5
pf8

Partial preview of the text

Download Operator and Data Type - Lecture Slides | ECE 206 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

ECE206 - Programming

Lecture 3 – Operators and Data

Type

2

Recap

† Programming environment

† Good programming style

† ALU design

int main() { cout <<… }

001001010 010010100 010010010 010100101

000110101 011111000 001010101

001001010 010010100 000110101 011111000 001010101 010100101

001001010 010010100 000110101 001010101 010100101

complier (^011111000) linker (^) loader

Files saved on disk Primary memory

*.cpp (^) *.o *.exe

Pre-complied lib

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 << y; // what’s y? cout << x; // what’s x? y = ++y; cout << y; // what’s y? ++y; cout<<y; //what’s y? ...

... x = 10; y = x++; cout << y; // what’s y? cout << x; // what’s x? y = y++; cout << y; // what’s y? y++; cout<<y; //what’s y?

† When using the prefix form, C++ performs the increment or decrement prior to obtaining the operand’s value † When using the postfix form, C++ will obtain the operand’s value before incrementing or decrementing it.

7

Logical operators

† Deal with multiple conditions, also

common used in control structures „ && AND „ || OR „! NOT

† Short circuit evaluation

„ expr1 && expr2 && expr „ expr1 || expr2 || expr „ 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 << (guess == magic? “** Right **” : “...Sorry, you're wrong.”);

10

Variable declaration

† Specify name and type: int a; float b=1.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. It’s 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 variable’s purpose in a comment. † Variables need to be declared before the first use

13

Mixed Type Expression

† Data of different types in a single

expression are converted to the same type

† Explicit type conversion – cast

char ch; int i; float f; double d; result = ch / i + f * d – (f + i)

int m=9, n=5; float x, y;

x = float(m)/n; y = m/n;

cout << x << endl; cout << y << endl;

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 intCons=50; „ const char charCons=‘c’;

† Backslash character constants „ \n (new line), \t (tab)

15

Common errors

int x = y = 5;

if (x = y); cout << “x equals to y\n”;

if (3<x<7) cout << “x is between 3 and 7\n”;

If (x > = y) cout << “x is greater or equal than y\n”;

cout << “x divided by y equals to ” << x/y << “\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