lesson 3: arithmetic & castingrsalazar/pic10a/lessons/lesson03.pdf · division and data types...

13
Lesson 3: Arithmetic & Casting Pic 10A Ricardo Salazar

Upload: others

Post on 19-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson 3: Arithmetic & Castingrsalazar/pic10a/lessons/lesson03.pdf · Division and data types The result of a division depends on what type of values are being divided. If at least

Lesson 3: Arithmetic & Casting

Pic 10A

Ricardo Salazar

Page 2: Lesson 3: Arithmetic & Castingrsalazar/pic10a/lessons/lesson03.pdf · Division and data types The result of a division depends on what type of values are being divided. If at least

(2.4) Constants

● Sometimes we want a 'variable' that does not vary!?

(OK that does not make sense... but how about a 'house' whose guest is always the same)

We call such 'houses' constants.

● We can declare them with the reserved word const.

● The value of such constant WILL NEVER change.(The compiler will not let you change the value)

● The convention is to CAPITALIZE their names

Examples:const double PI = 3.1416;const int NUMBER_OF_LESSONS = 27;

Page 3: Lesson 3: Arithmetic & Castingrsalazar/pic10a/lessons/lesson03.pdf · Division and data types The result of a division depends on what type of values are being divided. If at least

(2.5) Basic arithmetic

Symbol Operation Example Value*

+ Addition 1 + 5; 6

- Subtraction 1 – 5; –4

* Multiplication 3 * 5; 15

/ Division 7 / 3; 2

% Mod (residue) 7 % 3; 1

++ Increment one int i=7;i++; /* ++i; also works */

78

* some values make no sense, we will explain this later.

Page 4: Lesson 3: Arithmetic & Castingrsalazar/pic10a/lessons/lesson03.pdf · Division and data types The result of a division depends on what type of values are being divided. If at least

Assignment, not equality!

● Unlike math where the symbol = denotes equality, in C++ = is used to assign values.

● More precisely: = assigns the value on the right to the variable on the left.

● Example

int count = 4;double total = 0.03;total = count * 0.05 + total;

Page 5: Lesson 3: Arithmetic & Castingrsalazar/pic10a/lessons/lesson03.pdf · Division and data types The result of a division depends on what type of values are being divided. If at least

Example: How much soda?

● A fridge contains soda in 2-liter bottles and 12-oz cans. How much soda there is?

2.0 is the volume (in liters) of a bottle,0.355 is the volume of a can.

The formula is correct but the code is not very clear!

Page 6: Lesson 3: Arithmetic & Castingrsalazar/pic10a/lessons/lesson03.pdf · Division and data types The result of a division depends on what type of values are being divided. If at least

Shortcuts

● From the previous program:soda = soda + userInput * BOTTLE_VOLUME;soda = soda + userInput * CAN_VOLUME;

● Such expressions are very common. To write less usesoda += userInput * BOTTLE_VOLUME;soda += userInput * CAN_VOLUME;

● Some other shortcuts:a –= 4; is the same as a = a – 4;a *= 2; is the same as a = a * 2;a /= 3; is the same as a = a / 3;

It looks weird but it does save time!

Page 7: Lesson 3: Arithmetic & Castingrsalazar/pic10a/lessons/lesson03.pdf · Division and data types The result of a division depends on what type of values are being divided. If at least

The 'increment' operator

● Later we'll use particular int variables called counters. They are used to count and are often increased or decreased by one.

int currentLesson = 3;currentLesson += 1; // sets currentLesson=4

● This operation is so common, it has a special syntax:

int currentLesson = 3;currentLesson++; // sets currentLesson=4

● There are 4 increment/decrement operators:

● Does the name C++ make sense now?

x++; ++x; x--; --x;

Page 8: Lesson 3: Arithmetic & Castingrsalazar/pic10a/lessons/lesson03.pdf · Division and data types The result of a division depends on what type of values are being divided. If at least

Order of operations

● Multiplication ( * ) and division ( / ) take precedence over addition ( + ) and subtraction ( – ).

● If you want to enforce a different precedence use parentheses.

int m = 2+3*4; // stores the number 2 + 12 = 14int n = (2+3)*4; // stores the number 6 * 4= 24

● You can improve readability by using blank spaces. x = (–1+2*(3–6)*(5–2)+3*2)/(2+2*(5–3));x = ( –1 + 2 * (3 – 6) * (5 – 2 ) + 3 * 2 ) / ( 2 + 2 * ( 5 – 3 ) );

some exceptions include:the unary minus sign: –b vs – bfunctions: sqrt(16) vs sqrt (16)

Page 9: Lesson 3: Arithmetic & Castingrsalazar/pic10a/lessons/lesson03.pdf · Division and data types The result of a division depends on what type of values are being divided. If at least

Division and data types

● The result of a division depends on what type of values are being divided.

● If at least one number is decimal (double), the result is decimal, otherwise the result is an integer.

E.g:double x = 5 / 3; // sets x = 1double y = 5.0 / 3; // sets y = 1.66667

In the first case the result of the division is an integer. The decimal part is discarded and it is stored (as a double).

In the second case the result is a decimal.

● Remember to use the .0 for decimals.

Page 10: Lesson 3: Arithmetic & Castingrsalazar/pic10a/lessons/lesson03.pdf · Division and data types The result of a division depends on what type of values are being divided. If at least

Static casts

● Also be careful with 'raw' datacout << 5/4; // shows 1

cout << 5.0/4; // shows 1.25

cout << 5/4.0; // shows 1.25

● The compiler does not give an error for mixing types... some of them only WARN you that some information might get lost.

● If you are prepared for the consequences you can turn off the warning message with a static cast.

Casts are also used to temporarily change the type of a variable.

Page 11: Lesson 3: Arithmetic & Castingrsalazar/pic10a/lessons/lesson03.pdf · Division and data types The result of a division depends on what type of values are being divided. If at least

Casts (cont)

● For older compilers use: (newtype) expressionfor newer ones use: static_cast<newtype>(expression)

E.g:int a = 3, b = 10;double c = 2.5;

int d = a / (int) c; // sets d = 3/2 = 1/* int d = a / static_cast<int>(c); also works */

double e = (double) a / b; // sets e = 3.0/10 = 0.3double f = a / b ; // sets f = 3/10 = 0

● The changes are temporary:c doe not store 2 and a does not store 3.0

Page 12: Lesson 3: Arithmetic & Castingrsalazar/pic10a/lessons/lesson03.pdf · Division and data types The result of a division depends on what type of values are being divided. If at least

Casts (applications )

● How can we round off a double to the nearest integer?

– Add 0.5 and then discard the decimal part.double x = 3.1;int a = static_cast<int>(x + 0.5);/* sets x=3, discards the decimal 0.6 */

double y = 1.7;int b = static_cast<int>(y + 0.5);/* sets y=2, discards the decimal 0.2 */

● If c and d are integers and the quotient (decimal) is needed:

double e = static_cast<double>(c) / d; // worksdouble e = static_cast<double>( c/d ); // doesn'tdouble e = 1.0 * c / d; // also works (avoid it)!

Page 13: Lesson 3: Arithmetic & Castingrsalazar/pic10a/lessons/lesson03.pdf · Division and data types The result of a division depends on what type of values are being divided. If at least

The mod (residue) operator %

● In elementary school we are taught that:7 divided by 3 is 2 and leaves 1 as a remainder.15 divided by 4 is 3 and leaves 3 as a remainder.

This remainder is also called a residue.

● The operator % is used to compute the residue.int a = 4 % 2; // sets a = 0int b = 7 % 3; // sets b = 1

● Problem: Convert 145 days to months and days.int d = 135;int months = d / 30; // sets months = 4int days = d % 30; // sets days = 15