




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Electrical Engr Computations; Subject: Electrical And Computer Engr; University: University of Tennessee - Knoxville; Term: Fall 2007;
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





2
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