{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# LectureFour - C Programming Language Lecture 4 C Basics...

This preview shows pages 1–9. Sign up to view the full content.

Lecture 4 C++ Basics – Part II By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The Hashemite University 2 Outline ± Arithmetic operators. ± Logical operators. ± Assignment operator. ± Increment and decrement operators. ± Bitwise Operators. ± Relational Operators. ± ‘Size of’ operator. ± Comma operator. ± Operators Precedence. ± Escape Sequences.
The Hashemite University 3 Arithmetic Operators I ± Arithmetic operators: ± Addition + ± Subtraction - ± Division / ± Multiplication * ± Modulus % ± Arithmetic operators are binary operators (take two operands). ± Result of / (division) depends on the operands types. E.g: int a= 5; int b = 2; double c = a/b; c = ?? E.g: double a= 5; double b = 2; double c = a/b; c = ?? E.g: double a= 5; double b = 2; int c = a/b; c = ?? E.g: double c = 5/2; c = ?? E.g: double c = 5.0/2; c = ?? E.g: double c = 5/2.0; c = ?? ± Division Result Examples:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The Hashemite University 4 Arithmetic Operators II ± Division Operator General Rules: Operand 1 data type Operand 2 Temporary result Holder data type Saved Result int int integer int int int integer double/float integer int double/float Floating point double/float Floating point double/float int Floating point double/float Floating point double/float double/float Floating point int integer int int double/float integer int double/float Floating point integer double/float int Floating point integer double/float double/float Floating point Floating point
The Hashemite University 5 Arithmetic Operators III ± % (modulus which finds the remainder) is applied for integer values only. So, 9%4 = 1, but 9%2.5 Æ Syntax Error. ± Operators precedence: *, %, and / have the same priority which are higher than + and – (+ and – have the same priority). ± If multiple operators are combined which have the same priority you start implementation from left to right. ± Remember that parenthesis forces priority. ± For nested parenthesis you start with the most inner parenthesis pair. ± For multiple parenthesis start implementation from left to right.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The Hashemite University 6 Example #include <iostream.h> int main() { int a = 9, b = 30, c = 89, d = 35, x; double e = 3, y; cout <<"(a): "<< d/a << endl; cout <<"(b): "<< d/e << endl; x = d/a; y = d/a; cout <<"(c): "<< x << endl; cout <<"(d): "<< y << endl; x = d/e; y = d/e; cout <<"(e): "<< x << endl; cout <<"(f): "<< y << endl; x = d*a + c%b - b + d/e; y = d*a + c%b - b + d/e; cout <<"(g): "<< d*a + c%b - b + d/e << endl; cout <<"(h): "<< x << endl; cout <<"(i): "<< y << endl; x = d*a + c%b - (b + d/e); y = d*a + c%b - (b + d/e); cout <<"(j): "<< d*a + c%b - (b + d/e) << endl; cout <<"(k): "<< x << endl; cout <<"(l): "<< y << endl; return 0; }
The Hashemite University 7 Example Output

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The Hashemite University 8 Assignment Operator I ± = assigns the value on the right hand side to what present on the left hand side. ± E.g: int x = 6; ± Multiple assignments at the same time are allowed where implementation starts from right to left, e.g. x = y = u = 10; ± Common errors: 6 = x; x = y + 10 = 9; // you cannot use arithmetic operators between multiple assignments.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 40

LectureFour - C Programming Language Lecture 4 C Basics...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online