Learning Objectives
By the end of today's class, you should know...

• What are some of the mistakes you could make when you write your conditional statements involving logical operators?
• How do you format decimal numbers for printing using fixed and setprecision(n)
• What is the library that you must include to use fixed and setprecision(n)
• What is a constant and how do you declare one?
• What is a magic number and why should they be avoided?
• What are the short cut assignment operators for add, subtract, multiply, divide, and modulus?
• What are the increment and decrement operators?
• What is the difference between pre and post increment and decrement?
• What is casting?
• How do you cast an integer to a double and a double to an integer?
• What happens when an integer is too big for its type?
• What is a loop?
• What is a while loop?
• What is the syntax of a while loop?
• How does the syntax of a while loop compare to an if statement?
• How do you use a while loop to count?

Announcements

• Excellent Midterm Results!
• As: 26 (5 100% scores!)
• Bs: 14
• Cs: 3
• Ds: 0
• Fs: 1
• Absent: 1
• `Return midterms at the end of class!`
• `Assignment 10 due as normal on Wednesday`
• Note: Now is the point in the quarter when things get more challenging!
• Loops can be tricky
• Get help right away if find you don't understand something!
• Where can you get help?
• Quiz 4 on Wednesday
• if - else if - else (multiway branches)
• logical operators
• numbers, operators and precision <--today!
• while loops <--today!
• No Lab this Friday due to the Presidents' Day holiday!

### Review Activity

Review of Conditions Involving Logical Operators:

• Imagine that a local radio station is holding a contest to give away a year's supply of free pet food to one lucky winner.
• To win the contest, you must meet the following criteria:
• You must be caller 19
• The pet your own must be one of the following: dog, cat, rabbit
• You must be between the ages of 18 and 65.
• Which of the following test conditions will correctly determine the winner of the contest
a. if (numCaller = 19 && (pet = "rabbit" || pet = "cat" || pet = "dog") && (age >= 18 && age <= 65))
cout << "You are the winner!";

b. if (numCaller == 19 && (pet == "rabbit" || "cat" || "dog") && (age >= 18 && <= 65))
cout << "You are the winner!";

c.
if (numCaller == 19 || (pet == "rabbit" && pet == "cat" && pet == "dog") || (age >= 18 && age <= 65))
cout << "You are the winner!";

d.
if (numCaller == 19 && (pet == "rabbit" || pet == "cat" || pet == "dog") && (age >= 18 && age <= 65))
cout << "You are the winner!";

e.
if (numCaller == 19 && (pet == "rabbit" || pet == "cat" || pet == "dog") && (age >= 18 || age <= 65))
cout << "You are the winner!";
• ### What will the following print to the console?

int age = 20;

bool is_student = true;

if (is_student || age > 21) {

cout << "Fi!";

}

if (!is_student && age < 21) {

cout << "Fo!";

}

if (is_student && age < 21) {

cout << "Fum!";

}

### Wrapping Up Conditionals

Conditional Pitfalls

• Unfortunately, you can write many things in C++ that should be incorrect but end up working for some obscure reason
• This means that you can code something that should create an error message but does not
• Thus, a program may compile and run with no error messages but still be wrong
• Since you may not realize that it is wrong, it can be hard to find and correct these types of errors

#### Strings of Inequalities

• One common mistake is to use `=` when you meant to use `==`
• For example, look at the test condition in the following code:
```if (guess = 7) {
cout << "*** Correct! ***\n";
} else {
cout << "Sorry, that is not correct.\n";
}
```
• Notice that the condition is really an assignment statement and not a test
• You would think that it would fail to compile -- but it does not
• However, it will not work as you might expect
• A way to prevent this type of problem is to reverse the order of your test condition:
`if (7 = guess) {`
• Now the compiler will give you an error message and your code will not compile:
```guess.cpp: In function `int main()":
guess.cpp:10: error: non-lvalue in assignment
```
• However, if you correctly use == then your code will compile
`if (7 == guess) {`

#### Strings of Inequalities

• Do NOT use a string of inequalities like the following:
```int a = 5, b = 1, c = 10;
if (a < b < c) {
cout << "b is between a and c\n";
} else {
cout << "b is NOT between a and c\n";
}
```
• Your code may compile and run but give incorrect results
• The test condition is evaluated by the computer from left to right
• The first condition is `a < b` which evaluates to `0` (`false`)
• The second condition is then `0 < c` which evaluates to `1` (`true`)
• Since the whole test condition evaluates to `true` you get an incorrect result
• Instead, the correct way is to use `&&` as follows:
```int a = 5, b = 1, c = 10;
if (a < b && b < c) {
cout << "b is between a and c\n";
} else {
cout << "b is NOT between a and c\n";
}
```

#### Strings of Logical Operators

• Logical expressions often read like "normal" English.
• However, C++ requires more exactness than English
• For example, the following code will compile and run but give wrong results:
```int guess;
cout << "Enter a guess: ";
cin >> guess;
if (guess == 7 || 8) {
cout << "*** Correct! ***\n";
} else {
cout << "Sorry, that is not correct.\n";
}
```
• The test condition is evaluated by the computer from left to right
• The left hand side is (`guess == 7`) which can evaluate to either `true` or `false`
• The right hand side is `8`, which is interpreted as `true` by C++
• Since (something` or true`) is always `true`, then the test condition always evaluates to `true`
• Instead, the correct way is to use `||` as follows:
• int guess;
```cout << "Enter a guess: ";
cin >> guess;
if (guess == 7 || guess == 8) {
cout << "*** Correct! ***\n";
} else {
cout << "Sorry, that is not correct.\n";
}```
• We will work more with test conditions and logical operators as we write programs containing loops.

### Decimal Formatting

• Sometimes programs may not display numbers as you would expect!
• Consider the following program and what it will display:
```#include<iostream>
using namespace std;

int main() {
double price = 78.50;
cout << "The price is \$" << price << endl;
}
```
• We must explicitly tell C++ how to output numbers in our programs!
• These commands do not produce any output but change how `cout` outputs floating-point numbers
• "Magic Formula" to force decimal places:
```cout << fixed             // fixed notation, not scientific
<< setprecision(2);  // show 2 decimal places
```
• The commands `fixed` and `setprecision` are known as manipulators because you can manually change how cout works
• You can put both commands on one line:
`cout << fixed << setprecision(2);`
• Also, you can combine the commands with other output:
```cout << "The price is \$"
<< fixed << setprecision(2)
<< price << endl;
```
• To use these commands, you must include the `iomanip` library:
```#include <iomanip>
```
• Once we set the decimal formatting, it stays set.

### Constants and Magic Numbers

• A constant variable (or constant) is a variable that cannot change after being assigned a value
• Sounds oxymoronic, but is actually quite useful
• To declare a constant, we use the keyword: `const`
`const int MY_CONST = 1;`
• Note that we must assign a value when the constant is declared
• Also note that the name is all uppercase letters with an underscore separator
• This is a common coding convention that you must follow

#### Magic Numbers

• Imagine that you are a programmer hired to modify a payroll program
• You come across the following section of code:
```double pay;
pay = hours * 7.5 + (hours / 40)
* (hours - 40) * 7.5 * 0.5;
```
• The numbers are important to the program, but what do they mean?
• Numbers like these are called Magic Numbers.
• They are magic because the value or presence is unexplainable without more knowledge
• Often, no one knows what they mean after 3 months, including the author
• A programmer can often infer the meaning of numbers after reading the code carefully
• A much better coding style is to use named constants rather than literal numbers
• For example:
```const double WAGE = 7.5;
const int HOURS_PER_WEEK = 40;
double pay;
pay = hours * WAGE + (hours / HOURS_PER_WEEK)
* (hours - HOURS_PER_WEEK) * WAGE * OVERTIME_ADDER;
```
• Now it is much easier to understand the code and see any problems or limitations in it
• Another reason to use named constants is that it is easier to change the value of the number
• In the above example, we can easily change the WAGE without making errors in other parts of our code

#### Programming Style: Constant Variables and Magic Numbers

• Since the meaning of literal (magic) numbers is hard to remember, we should declare constants instead:
```const int FEET_PER_YARD = 3;
const double PI = 3.14159265358979323846;
const double WAGE = 7.5;
const int HOURS_PER_WEEK = 40;
```
• Note that the name is all uppercase letters with an underscore word-separator
• This is a common coding convention that you must follow for your constants

### Assignment Operators

• As we discussed before, we assign values to variables using an equal (`=`) sign
`int sum = 0;`
• However, the equal sign is really an assignment operator and does not denote equality
• Thus, unlike math, we can have the same variable on both sides of an equals sign:
```int sum = 25;    // initialize sum to 25
sum = sum + 10;  // add to sum
```
• Note that the value of the variable is changed in the second line
• Reading variables from memory does not change them
• Values placed into a variable replace (overwrite) previous values:

#### Shortcut Assignment Operators

• We can use additional operators to calculate values and assign them to the variable on the left all in one statement
• Known as shortcut assignment operators
• The general syntax is:
`variable op= expression;`
• Where op is one of the five arithmetic operators: `+``-``*``/``%`
• For example, the following two statements create the same result:
```x = x + 3;
x += 3;
```
• Shown below are the assignment operators with examples of how they are used:

#### Summary of Assignment Operators

OperatorDescriptionExampleEquivalent To
`=`Assigns the value of the expression on the right to the variable on the left`x = 3`
`+=`Adds the expression on the right to the variable on the left`x += 3``x = x + 3`
`-=`Subtracts the expression on the right from the variable on the left`x -= 3``x = x - 3`
`*=`Multiplies the expression on the right to the variable on the left and saves the result in the variable on the left`x *= 3``x = x * 3`
`/=`Divides the variable on the left by the expression on the right and saves the result in the variable on the left`x /= 3``x = x / 3`
`%=`Calculates the remainder from dividing variable on the left by the expression on the right and saves the result in the variable on the left`x %= 3``x = x % 3`

#### Increment and Decrement Operators

• Adding or subtracting one is a common operation in programming
• C++ provides arithmetic shortcuts for these operations with the increment and decrement operators
• The increment operator (`++`) adds 1 to a variable's value
• Pre-increment adds 1 before evaluating an expression
`++sum;`
• Post-increment evaluates the expression and then adds 1
`sum++;`
• The decrement operator works like the increments operator, except it subtracts 1 from the variable:
```--sum
sum--
```
• Pre- and post- increment matters when the operation is part of a larger expression
• For example, consider the code:
```int x = 5;
int y = x++;
cout << "x=" << x << " y=" << y;
```
• We may expect y to be 6 after this code executes
• Instead, y has the value of 5
• The reason is that `++` after a variable (post-increment) is equivalent to:
```y = x;
x = x + 1;
```
• On the other hand, `++` before a variable (pre-increment) is equivalent to:
```x = x + 1;
y = x;
```

Casting

Cast: change the data type of the returned value of an expression

• Recall that different data types are stored in different forms
• Sometimes you need to change from one form to another
• For example: arithmetic adding a `double` and an `int` value
• C++ will automatically cast one value to another
• Known as implicit casting or type coercion
• Programmers can also explicitly cast data types
• Explicit casting changes the data type for a single use of the variable
• Precede the variable name with the new data type in parentheses:
```(dataType) variableName
```
• The type is changed only for the single use of the value
• For example:
```double x = 2.99999;
x = (int) x;
cout << x << endl;
```
• The value of `x` is converted from type `double` to `int` before assigning the converted value to `x`
• However, `x` remains a type `double` and the cast only applies to a single use of `x`
• The above example shows a common use of casting -- removing the decimal part of a floating-point number
• Note that the decimal portion of the number is truncated and NOT rounded
• Decimal part is lost (discarded, ignored, thrown away)
• Another use is to convert an `int` to a `double` when dividing two int numbers and a decimal result is desired
• For example:
```double x = (double) 9 / 5;
cout << x << endl;
```
• Still another use is to prevent compiler warnings
• For example:
```double x = 2.3;
int n = x;
cout << n << endl;
```
• The above may cause a compiler warning with the settings we use:
`warning: converting to 'int' from 'double'`
• To remove the warning, we use a cast:
```double x = 2.3;
int n = (int) x;
cout << n << endl;
```
• This tells the compiler that you intended to convert from `double` to `int`

#### Example Application Using Casting:

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 ``` ```#include using namespace std; int main() { double input; cout << "Enter hours: "; cin >> input; int hours = (int) input; // prevents warning int minutes = (int) ((input - (int) input) * 60); cout << "In hours and minutes, this is " << hours << ":" << minutes << endl; return 0; } ```

### Integer Overflow

• An integer is stored in a computer as a pure binary number:

Image source: Dan Gookin
• The sign bit sets whether the number is positive (0) or negative (1)
• The other bits represent the value, in this case `123`
• There are only a finite set of numbers in an integer value
• What happens when an integer is too big for its type?
```int bigPlus = 2147483647;
cout << "Big number: ";
cout << bigPlus + 1 << endl;
int bigMinus = -2147483647;
cout << "Small number: ";
cout << bigMinus - 2 << endl;
```
• The number "wraps around" from the highest number to the lowest
• You must be careful that your program will not go beyond the range of its data types
• Can't sleep: from xkcd

#### More Integer Types

• To increase the range, C++ has the `long` data type
• Originally, the `long` data type was 32 bits while the `int` was 16 bits
• However, with the development of 32 bit computers, the `int` value was extended to 32 bits but the `long` was left at 32 bits
• Thus, at the present time, `int` and `long` are the same size on most computers
• In addition, C++ has `unsigned` integer types you can use to change the range
• Rather than integer ranges from -2147483647 to 2147483647, `unsigned int` ranges from 0 to 4294967295
• New to C++11 (a newer version of C++) is the type long long which is a 64 bit type.

#### Floating-Point Precision and Range

• Floating-point numbers are not exact representations of real numbers
• Rounding errors occur in repeated calculations
• Type double has about twice the precision of type float
• However, even type double can have rounding errors
```cout << setprecision(17);
cout << .8F + .1F << endl;
cout << .8 + .1 << endl;
```
• When floating point numbers get too large, they are set to `inf`
• For instance:
`cout << 2E38F + 2E38F << endl;`
• Similarly, when numbers are too small they are set to `0.0`

#### The Moral

• Integer and floating-point data types work well most of the time
• However, if we work with large positive or negative integers, we must be sure we do not exceed the range of the data type
• Also, floating-point numbers have limited precision
• When math operations are performed repeatedly, they can become less precise
• Thus we must be careful of precision when using floating-point numbers

Activity 10.1: Prices (10pts)

• Find a partner for pair programming.
• Open up Eclipse and create a new projected named Prices with a file called price.cpp. Copy and paste the below code into it beneath the comment with your and your partner's names.
 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ``` ```#include #include using namespace std; int main() { string name; double price = 0; cout << "Enter the product name: "; cin >> name; cout << "Price of the " << name << ": "; cin >> price; // Insert new statements here cout << "Total price: \$" << price << endl; return 0; } ```
• Compile and run the starter program to make sure you entered it correctly.

When you run the program, the output should look like this:

```Enter the product name: iPod_Nano
Price of the iPod_Nano: 149.50
Total price: \$149.5
```

Note the format of the numbers output for the total price. We will address this formatting issue later in the exercise.

• Run the program again for a product with a very high cost, like a Boeing 777:
```Enter the product name: Boeing__777
Price of the Boeing_777: 212345678
Total price: \$2.12346e+08
```

Note the format of the numbers output for the total price. This format is called exponential notation. You may have encountered it in some of your math classes.

• Let us correct the formatting of the total price. Enter the following code before the statement that prints the total price:
```cout << fixed             // fixed notation, not scientific
<< setprecision(2);  // show 2 decimal places
```

These statements are referred to as the "magic formula" because they for C++ to output statements in a "standard" format. Note what each statement accomplishes.

• Compile and run your program again and verify the output looks like:
```Enter the product name: Boeing_777
Price of the Boeing__777: 212345678
Total price: \$212345678.00
```
• Let us add a constant that we will use later in our program. Enter the following code after the magic formula and before the statement that prints the total price:
```const int PERCENT = 100;
```

A constant variable (or constant) is a variable that cannot change after being assigned a value. Using a constant lets us avoid using a vague number.

• Now we will add sales tax to the price of the product. Enter the following code after the constant and before the statement that prints the total price:
```double taxRate = 0;
cout << "Enter sales tax rate (%): ";
cin >> taxRate;
double tax = price * taxRate / PERCENT;
price += tax;
```

Notice the last statement: `price += tax;`. This is an alternate way to code the statement: `price = price + tax;`.

• Compile and run your modified program and verify the output looks like:
```Enter the product name: iPod_nano
Price of the iPod_nano: 89.50
Enter sales tax rate (%): 9.5
Total price: \$98.00
```
• Now we will find the whole dollars and cents of the amount to demonstrate casting. Enter the following code after the statement that prints the total price and before the return statement:
```int dollars = (int) price;
cout << "In whole dollars: \$" << dollars << endl;
```

Notice the `(int)` in the first statement. This is known as a type cast or just cast

• Compile and run your modified program and verify the output looks like:
```Enter the product name: iPod_nano
Price of the iPod_nano: 89.50
Enter sales tax rate (%): 9.5
Total price: \$98.00
In whole dollars: \$98
```

Upcoming Assignments

• Assignment 10 due Wednesday at 9:20am
• No Lab due Friday
• Quiz 4 next class

~See You Wednesday!~