CS-120 Lecture 3 January 21 Admin stuff

• Homework 1 due Friday • Section 3 and 4 wait lists will be added today or tomorrow Review

• Variables! • Must be declared before used • Must have a type (only in the declaration) • Have a scope (we’ll talk more about this later) • Global vs. local • Can be declared as a constant by using the const keyword • Can be initialized as part of the declaration • Nearly always a very good idea • Using uninitialized variables can cause weird, unpredictable behavior in your program Examples:

• VarExample.cpp (on the Documents page of the website) • Naming • Note the variable with the leading underscore, _dayName First program out of the text: Fortune Teller!

• Enter the program in from the web. • Let’s take an initial look at the overall program • Lots of cout << “…”; statements • Lots of if(…){} statements (we’ll get to these soon) • Basic premise: 1. Get a couple of numbers from the user 2. Calculate the user’s lucky number based on their input 3. Output a “fortune” based upon the lucky number Fortune Teller analysis

• Some parts should look familiar at this point: • Input statements • Output statements • Variable declarations • We need to talk about the IF statements (branching) still, but first let’s talk about how to do math and other fun calculations in ++! Expressions – Math, logic, and other stuff

• Expressions are how we do the real work in a program. • Calculate values • Compare values • Set up logical conditions • Assign a value to a variable • Reference a variable • Call functions to perform a task (we’ll talk about this a bit later) • Key concept: Expressions ALWAYS return a value • What does this mean? • You can use an expression “inside” another expression • Some expressions can be “chained” together Expressions: Assignment • The “=“ operator is the assignment operator in C++ • This is not your algebraic equality • Take whatever is on the right-hand side of the “=“, and place it in the variable on the left- hand side. • Note: assignment expressions must have some sort of variable on the left-hand side of the “=“ • We’ve already seen one example of an assignment with variable initializations. • const double PI = 3.14159 • Example: x = 5; • Take the value 5, and place it in the memory location labeled “x” • Swap problem • int x,y; • x = 5; • y = 7; • x = y; • y = x; • Does this properly swap x and y? Expressions: Assignment (cont’d)

• Swap problem (cont’d) • NO! • Initial: X: 5 Y: 7

• After “x = y;”: X: 7 Y: 7

• Note how the value in x is overwritten by the value in y • The last assignment, “y = x;” effectively does nothing. • What would we need in order to make this work? • Return value: Assignments return the value that was assigned Expressions: Mathematical

• Math in C++ looks similar to (but not exactly like) what you would see in a math class. • The biggest differences are that you have to explicitly use each operator you want. For example: • Algebra: 3x2 • C++: 3 * x * x • Order of operations is what you would expect for “normal” math operations, but C++ has many more operators than what you may be used to. • C++ operator precedence Expressions: Mathematical (cont’d)

• Some useful operators: • ++, --: increment and decrement • Can be placed either before or after a variable, which will change the timing of the operation • +=, -=, *=, /=: math and assignment shorthand • Equivalence: • x = x + 2; • x += 2; • Note: there is no operator! • We will discuss how to accomplish exponentiation in the general case later Expressions: Mathematical (cont’d)

• Special topic: , thou art a heartless *bleep*... • Mechanics of division, a review of grade school • Terms: • Divisor: the number you are dividing by • Dividend: the number you are dividing • Quotient: the “answer” (generally an integer) • Remainder: what’s left over (also generally an integer) • Most of the time in mathematics, all we are interested in is the quotient, which we usually give as an exact (or as exact as we can get it) real number. • However, in programming, we have some additional concerns, specifically the types of the numbers involved. Also, many times we are more interested in the remainder than in the quotient, so much so that there’s a special operator called the modulus. Expression: Math: Division (cont’d)

• Integer division • In C++, when you divide two integers, it will return the integer quotient, and ignore (drop) the remainder. • Examples: • Division.cpp • Integer division leads to some very common errors • Area of a triangle: ½ base*height • C++: area = (1/2)*base*height; • What’s the problem? • Correct C++: area = (1.0/2)*base*height; Expression: Math: Division (cont’d)

• Modulus • The “%” operator in C++ is called the modulus. • X % Y returns the remainder when X is divided by Y • Example: X % 10 will give me the remainder when X is divided by 10. What does this mean? • Both X and Y must be integers. • This allows us to do what is called modular , which we will most likely cover in a lab in the next couple of weeks. • Back to Fortune Teller Objectives

• You should now be able to: • Declare a variable of the appropriate type, initialize it, and use it in a program. • Recognize expressions in a program and broadly categorize them • Assignment • Mathematical • Logical (or comparison) • Function call • Write an expression using mathematical operators to calculate a value. • Take an existing equation and translate it into C++ • Read a mathematical expression and given values for the variables, determine the result. • Recognize integer division and determine the correct result.