Lesson 3: Arithmetic & Casting Pic 10A Ricardo Salazar
(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;
(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 */ 7 8 * some values make no sense, we will explain this later.
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;
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!
Shortcuts From the previous program: soda = soda + userinput * BOTTLE_VOLUME; soda = soda + userinput * CAN_VOLUME; Such expressions are very common. To write less use soda += 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!
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: x++; ++x; x--; --x; Does the name C++ make sense now?
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 = 14 int 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 b functions: sqrt(16) vs sqrt (16)
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 = 1 double 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.
Static casts Also be careful with 'raw' data cout << 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.
Casts (cont) For older compilers use: (newtype) expression for 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.3 double f = a / b ; // sets f = 3/10 = 0 The changes are temporary: c doe not store 2 and a does not store 3.0
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; // works double e = static_cast<double>( c/d ); // doesn't double e = 1.0 * c / d; // also works (avoid it)!
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 = 0 int b = 7 % 3; // sets b = 1 Problem: Convert 145 days to months and days. int d = 135; int months = d / 30; // sets months = 4 int days = d % 30; // sets days = 15