Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 49 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
49
Dung lượng
1,38 MB
Nội dung
C++ Programming Language Lecture C++ Basics – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides) Outline Arithmetic operators Logical operators Assignment operator Increment and decrement operators Bitwise Operators Relational Operators ‘Size of’ operator Comma operator Operators Precedence cout and cin cascading Escape Sequences The Hashemite University 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 Division Result Examples: E.g: E.g: int a= 5; int b = 2; double c = a/b; c= double a= 5; double b = 2; double c = a/b; c = 2.5 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 = 2.5 E.g: double c = 5/2.0; c = 2.5 The Hashemite University Arithmetic Operators II (1) Result = number1 / number2 Division Operator General Rules: Operand data type Operand data type Temporary result Holder data type Saved Result int int integer int integer int int integer double/float integer int double/float Floating point int integer int double/float Floating point double/float Floating point double/float int Floating point int integer double/float int Floating point double/float Floating point double/float Hashemite University double/float The Floating int integer 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) double c= 5-6 * /3 %3+7/3 , c= ?? 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 The Hashemite University Example #include int main() { int a = 9, b = 30, c = 89, d = 35, x; double e = 3, y; cout