Presentation is loading. Please wait.

Presentation is loading. Please wait.

Integer Arithmetic. Operator Priority Real Number Arithmetic.

Similar presentations


Presentation on theme: "Integer Arithmetic. Operator Priority Real Number Arithmetic."— Presentation transcript:

1 Integer Arithmetic

2 Operator Priority

3 Real Number Arithmetic

4 Mixed-Mode Arithmetic When the operands are of the same type, the result is also of that type When the operands are of different types, the result is of the more inclusive type double int char More inclusive

5 Assignment Statement Form: = ; Example: x = y * z; 3 y 4 z 7 x Variables are names for memory locations

6 Mixed-Mode Assignment When the variable is of a more inclusive type, the value copied to it is promoted to that type When the variable is of a less inclusive type, the value copied to it is truncated and information may be lost

7 Mixed-Mode Assignment int i; double d; i = 2; d = i; // d contains 2.0

8 Mixed-Mode Assignment int i; double d; d = 3.56; i = d; // i contains 3

9 Type Cast Operations int i; double d; d = 3.56; i = d; // i contains 3 i = int(d); // same effect as above Form: ( )

10 Compound Assignment Statement

11 Interactive Input double payRate, hoursWorked, weeklyPay; cout << "Enter the pay rate: "; cin >> payRate; cout << "Enter the hours worked: "; cin >> hoursWorked; weeklyPay = hoursWorked * payRate; cout << "Weekly pay = " << weeklyPay << endl;

12 The ASCII Character Set 41 = ')'

13 Using apstring: Input and Output #include "apstring.h" apstring name; cout << "Enter your last name: "; cin >> name; cout << "Your last name is " << name << endl; >> reads a word up to a space or newline character

14 Using apstring: getline #include "apstring.h" apstring name; cout << "Enter your full name: "; getline(cin, name); cout << "Your full name is " << name << endl; getline reads an entire line, including blanks, up to a newline character

15 Using >> Before getline #include "apstring.h" apstring name; int age; cout << "Enter your age: "; cin >> age; cout << "Enter your full name: "; getline(cin, name); cout << "Your age is " << age << endl; cout << "Your full name is " << name << endl; getline encounters the trailing newline after the age, stores an empty string in name, and does not wait for user input

16 Put >> After getline, or Do This #include "apstring.h" apstring name; int age; cout << "Enter your age: "; cin >> age; cin.ignore(); // consume \n cout << "Enter your full name: "; getline(cin, name); cout << "Your age is " << age << endl; cout << "Your full name is " << name << endl;

17 Library Constants

18 Math Library Functions

19 apstring Member Functions


Download ppt "Integer Arithmetic. Operator Priority Real Number Arithmetic."

Similar presentations


Ads by Google