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

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

View Full Document Right Arrow Icon
Lecture 4 C++ Basics – Part II By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
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:
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
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; }
Background image of page 6
The Hashemite University 7 Example Output
Background image of page 7

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

View Full DocumentRight Arrow Icon
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.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/29/2010 for the course COMPUTER E 2010 taught by Professor Ghadaal-mashaqbeh during the Spring '10 term at Hashemite University.

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 Right Arrow Icon
Ask a homework question - tutors are online