Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.

Similar presentations


Presentation on theme: "Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older."— Presentation transcript:

1 Chapter 3 – Variables and Arithmetic Operations

2 Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older compiler restriction u Give numeric values to variables with assignment statement Lesson 3.1 variable_name = value; assignment operator

3 Naming Identifiers u First character must be letter –a-z, A-Z or _ u Other characters –letters (a-z, A-Z, _ ) or digits 0-9 u Cannot use C++ keywords (reserved words) u Cannot have blank within identifies Lesson 3.1

4 Keywords u Words with special meaning to C++ u Also includes alternative representations of certain operators and punctuators u Full listing in Table 3.1 u Examples: –auto, bool, float, inline, union, delete –namespace, private, void Lesson 3.1

5 Declaring Variables u Variables MUST be declared u List name and data type u Variables of same type may be declared in same statement (separate by comma) u Causes C++ compiler to know size of space to be reserved for storing variable’s value Lesson 3.1 double radius, diameter;data type variable names separator

6 Assignment Statements u Causes value to be stored in variable’s memory cell variable_name = value; u Variable name MUST appear on left u Equal sign is assignment operator Note: be careful = does NOT mean equal u Example: temperature = 78; Lesson 3.1

7 Constant Qualified Variables u Use const qualifier const double PI = 3.14159; u Cannot modify later in program u Style tip: use all uppercase characters to name constant –Makes constants easy to identify Lesson 3.2

8 Formatting Output u Insert I/O manipulators (parameterized) into cout statements for printing –declared in header iomanip #include u Basic form cout << manipulator(parameter); what manipulator uses to modify output Lesson 3.2 Listed in Table 3.2

9 setw( ) u Sets field width u Right justifies contents u C++ automatically expands if set width too small Lesson 3.2 cout<<“number =“<<setw(7)<<num<<endl; number = 5 ******* Field size

10 setprecision( ) u Sets number of digits after decimal point u All digits retained in memory u Once set may or may not be used until another statement changes it (compiler) Lesson 3.2 num = 5.3415; cout<<“num = “<<setprecision(2)<<num; num = 5.34

11 setfill( ) u Specifies character for blank space in field u Single quotes required around character enclosed in parentheses Lesson 3.2 num = 5.34; cout<<setw(10)<<setfill(‘*’)<<num; ******5.34

12 setiosflags(ios:: ) u Perform number of different actions based on flag that is set u Table 3.3 u Example: Lesson 3.2 num = 5.34; cout<<setiosflags(ios::left) << setfill(‘*’)<<setw(10)<<num; 5.34****** left justifies in field

13 Printing “dollar” Format u Necessary to use I/O manipulators Lesson 3.2 cout<<setprecision(2) <<setiosflags(ios::fixed|ios::showpoint) <<“Income = $” <<income; Income = $7842.00

14 Character Data u Lowercase and uppercase characters u Also graphic characters (!, #, ^) and “space” u Escape characters (\n) regarded as single characters u Numbers 0-9 can also be characters u Declare character variable:char c1,c2; u Assign value:c1 = ‘g’; Lesson 3.3 Can hold ONE character Enclose in single quotes

15 Assigning Characters to int u C++ assigns ASCII code value for the char u Does not use numeric value if assign 0-9 character u Table 3.5 gives characters and ASCII code/values Lesson 3.3

16 Arithmetic Operations u Look like algebraic expressions u Expression consists of sequence of operand(s) and operator(s) –Operand (variable, constant, any value) –Operators (+, -, *, /, % ) t Represent operation to be done t Increment (++) and decrement (--) Lesson 3.4

17 Problems u Uninitialized variables –C++ assigns own value –Does not trigger as an error u Exceeding integer range –int type range –32768 to 32767 –Due to limit of two bytes of memory –Overflow error u Division by zero Lesson 3.4

18 Pre- and Post- Operators u ++ or -- u Place in front, incrementing or decrementing occurs BEFORE value assigned Lesson 3.5 k = i++; i = 2 and k = 1 k = ++i; u Place in back, occurs AFTER value assigned i = 2 and k = 1 k =--i; k = i--; i = i + 1; k = i; 3333 i = i - 1; k = i; 1111 i = i + 1; 1313 k = i; i = i - 1; 2121

19 Mixed Type Arithmetic u Assign real to int –Cut off fractional part of real u Assign int value to real –Put decimal point at end, converts method of storage to exponential binary form u Modify with cast operators –Change type of expression –Keywordstatic_cast –Old form: (type) expression Lesson 3.5

20 static_cast Operator u General form Lesson 3.5 static_cast (expression) u Keyword requires underscore u Expression for which temporary copy is made of type type u Type can be any valid C++ data type

21 Operator Precedence Lesson 3.5 ( )parenthesesunaryprefix L to R 1 ++, --post-(in/de)crementunarypostfix L to R 2 ++, --pre-(in/de)crementunaryprefix R to L 3 +positive signunaryprefix R to L 3 -negative signunaryprefix R to L 3 static_castcastunaryprefix R to L 4 %, *, /remainder/multi/divbinaryinfix L to R 5 +, -add/subtractbinaryinfix L to R 6 +=, -=, *=math & assignmentbinaryinfix R to L 7 /=, %=math & assignmentbinaryinfix R to L 7 =assignmentbinaryinfix R to L 7

22 Real data types u Decimal numbers u float –4 bytes of memory, 6 digit precision u double –8 bytes of memory, 15 digits precision u long double –10 bytes of memory, 19 digits precision Lesson 3.6

23 Integer data types u Whole numbers u int, signed int, short int, signed short int –2 bytes, range: -32768 to 32767 u unsigned int, unsigned short int –2 bytes, range: 0 to 65535 u long int, signed long int –4 bytes, range: -2147483648 to 2147483645 u unsigned long int –4 bytes, range: 0 to 4294967295 Lesson 3.6

24 Math Functions u Need cmath or cstlib headers #include or #include u General form: name(parameters) u Note what type and form parameters take –Trig functions use radian measure not angle u Table 3.11 lists math library functions Lesson 3.6

25 Summary u Declare variables and constants u Format output u Work with character data u Create mathematical expressions u Work with mixed data types and casting u Use different data types for precision u Utilize available math functions Chapter 3 Learned how to:


Download ppt "Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older."

Similar presentations


Ads by Google