chapter 3 – variables and arithmetic operations. variable rules u must declare all variable names...
TRANSCRIPT
![Page 1: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/1.jpg)
Chapter 3 – Variables and Arithmetic Operations
![Page 2: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/2.jpg)
Variable Rules
Must declare all variable names– List name and type
Keep length to 31 characters– Older compiler restriction
Give numeric values to variables with assignment statement
Lesson 3.1
variable_name = value;assignment operator
![Page 3: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/3.jpg)
Naming Identifiers
First character must be letter– a-z, A-Z or _
Other characters– letters (a-z, A-Z, _ ) or digits 0-9
Cannot use C++ keywords (reserved words) Cannot have blank within identifies
Lesson 3.1
![Page 4: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/4.jpg)
Keywords
Words with special meaning to C++ Also includes alternative representations of
certain operators and punctuators Full listing in Table 3.1 Examples:
– auto, bool, float, inline, union, delete– namespace, private, void
Lesson 3.1
![Page 5: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/5.jpg)
Declaring Variables
Variables MUST be declared List name and data type Variables of same type may be declared in
same statement (separate by comma) 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 namesseparator
![Page 6: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/6.jpg)
Assignment Statements
Causes value to be stored in variable’s memory cell
variable_name = value; Variable name MUST appear on left Equal sign is assignment operator
Note: be careful = does NOT mean equal Example: temperature = 78;
Lesson 3.1
![Page 7: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/7.jpg)
Constant Qualified Variables
Use const qualifier
const double PI = 3.14159; Cannot modify later in program Style tip: use all uppercase characters to
name constant– Makes constants easy to identify
Lesson 3.2
![Page 8: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/8.jpg)
Formatting Output
Insert I/O manipulators (parameterized) into cout statements for printing– declared in header iomanip
#include <iomanip> Basic form
cout << manipulator(parameter);
what manipulator uses to modify output
Lesson 3.2
Listed in Table 3.2
![Page 9: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/9.jpg)
setw( )
Sets field width Right justifies contents C++ automatically expands if set width too
small
Lesson 3.2
cout<<“number =“<<setw(7)<<num<<endl;
number = 5******* Field size
![Page 10: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/10.jpg)
setprecision( )
Sets number of digits after decimal point All digits retained in memory 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
![Page 11: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/11.jpg)
setfill( )
Specifies character for blank space in field Single quotes required around character
enclosed in parentheses
Lesson 3.2
num = 5.34;cout<<setw(10)<<setfill(‘*’)<<num;
******5.34
![Page 12: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/12.jpg)
setiosflags(ios:: )
Perform number of different actions based on flag that is set
Table 3.3 Example:
Lesson 3.2
num = 5.34;cout<<setiosflags(ios::left) << setfill(‘*’)<<setw(10)<<num;
5.34******
left justifies in field
![Page 13: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/13.jpg)
Printing “dollar” Format
Necessary to use I/O manipulators
Lesson 3.2
cout<<setprecision(2) <<setiosflags(ios::fixed|ios::showpoint) <<“Income = $” <<income;
Income = $7842.00
![Page 14: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/14.jpg)
Character Data
Lowercase and uppercase characters Also graphic characters (!, #, ^) and “space” Escape characters (\n) regarded as single
characters Numbers 0-9 can also be characters Declare character variable: char c1,c2; Assign value: c1 = ‘g’;
Lesson 3.3
Can hold ONE characterEnclose in single quotes
![Page 15: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/15.jpg)
Assigning Characters to int
C++ assigns ASCII code value for the char Does not use numeric value if assign 0-9
character Table 3.5 gives characters and ASCII
code/values
Lesson 3.3
![Page 16: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/16.jpg)
Arithmetic Operations
Look like algebraic expressions Expression consists of sequence of
operand(s) and operator(s) – Operand (variable, constant, any value)– Operators (+, - , * , / , % )
Represent operation to be done Increment (++) and decrement (--)
Lesson 3.4
![Page 17: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/17.jpg)
Problems
Uninitialized variables– C++ assigns own value– Does not trigger as an error
Exceeding integer range– int type range –32768 to 32767– Due to limit of two bytes of memory– Overflow error
Division by zero
Lesson 3.4
![Page 18: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/18.jpg)
Pre- and Post- Operators ++ or -- Place in front, incrementing or decrementing
occurs BEFORE value assigned
Lesson 3.5
k = i++;
i = 2 and k = 1k = ++i;
Place in back, occurs AFTER value assigned
i = 2 and k = 1
k =--i;
k = i--;
i = i + 1;k = i;
33
i = i - 1;k = i;
11
k = i;i = i + 1;
13
k = i;i = i - 1;
21
![Page 19: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/19.jpg)
Mixed Type Arithmetic Assign real to int
– Cut off fractional part of real Assign int value to real
– Put decimal point at end, converts method of storage to exponential binary form
Modify with cast operators– Change type of expression– Keyword static_cast– Old form: (type) expression
Lesson 3.5
![Page 20: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/20.jpg)
static_cast Operator
General form
Lesson 3.5
static_cast <type> (expression)
Keyword requires underscore Expression for which temporary copy is
made of type type Type can be any valid C++ data type
![Page 21: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/21.jpg)
Operator Precedence
Lesson 3.5
( ) parentheses unary prefix L to R 1++, -- post-(in/de)crement unary postfix L to R 2++, -- pre-(in/de)crement unary prefix R to L 3+ positive sign unary prefix R to L 3- negative sign unary prefix R to L 3static_cast cast unary prefix R to L 4%, *, / remainder/multi/div binary infix L to R 5+, - add/subtract binary infix L to R 6+=, -=, *= math & assignment binary infix R to L 7/=, %= math & assignment binary infix R to L 7= assignment binary infix R to L 7
![Page 22: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/22.jpg)
Real data types
Decimal numbers float
– 4 bytes of memory, 6 digit precision double
– 8 bytes of memory, 15 digits precision long double
– 10 bytes of memory, 19 digits precision
Lesson 3.6
![Page 23: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/23.jpg)
Integer data types Whole numbers int, signed int, short int, signed short int
– 2 bytes, range: -32768 to 32767 unsigned int, unsigned short int
– 2 bytes, range: 0 to 65535 long int, signed long int
– 4 bytes, range: -2147483648 to 2147483645 unsigned long int
– 4 bytes, range: 0 to 4294967295
Lesson 3.6
![Page 24: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/24.jpg)
Math Functions
Need cmath or cstlib headers
#include <cmath> or #include <cstlib> General form: name(parameters) Note what type and form parameters take
– Trig functions use radian measure not angle Table 3.11 lists math library functions
Lesson 3.6
![Page 25: 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](https://reader038.vdocument.in/reader038/viewer/2022110321/56649f505503460f94c737bb/html5/thumbnails/25.jpg)
Summary
Declare variables and constants Format output Work with character data Create mathematical expressions Work with mixed data types and casting Use different data types for precision Utilize available math functions
Chapter 3
Learned how to: