Fall 2018 CS150 - Intro to CS I 1 VARIABLES & ASSIGNMENTS Sections 2.1, 2.2, 2.3, 2.4 Fall 2018 CS150 - Intro to CS I 2 Variables Named storage location for holding data named piece of memory You need to determine what variables you need in your program what data do we need to handle? 1
Fall 2018 CS150 - Intro to CS I 3 Variable Definition int number; Tells the compiler the variable s type (int) the variable s name (number) int is short for integer Variable definitions end with a semicolon Every variable must be defined Fall 2018 CS150 - Intro to CS I 4 C++ Assignment Statement number = 5; = is an operator that copies the value from the right into a variable on the left The item to the left of the = operator must be a variable 2
Fall 2018 CS150 - Intro to CS I 5 = is not equals!! You cannot write 5 = number; Fall 2018 CS150 - Intro to CS I 6 Variables in a program 1 // This program has a variable called number 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 6 int main() // what is the output of this program? 7 { 8 int number; 9 10 number = 5; 11 cout << Number is " << number << endl; 12 13 number = 7; 14 cout << "Now number is " << number << endl; 15 16 return EXIT_SUCCESS; 17 } 3
Fall 2018 CS150 - Intro to CS I 7 Variables on the left and right of assignments What is the output? #include <iostream> using namespace std; int main() { int x = 8; x = x + 1; x = x * 5; x = 7; cout << "x is: " << x << endl; } return 0; Fall 2018 CS150 - Intro to CS I 8 Variable Definition We now know that: What is an identifier?! 4
Fall 2018 CS150 - Intro to CS I 9 Identifiers (Variables) Programmer-defined names that represent some element of a program C++ limits on variable names: 1. Identifiers must begin with a letter or an underscore 2. Identifiers must consist of letters, numbers and underscore, nothing else 3. Identifiers cannot be a keyword Fall 2018 CS150 - Intro to CS I 10 Identifiers (Variables) Identifiers are case sensitive int totalcost; int TotalCost; Use meaningful variable names int width; int w; 5
Fall 2018 CS150 - Intro to CS I 11 Lower Camel Case You must use lower camel case for all of your CS 150 assignments In lower camel case, words are joined together, where the first letter of the entire is lowercase, but subsequent first letters are uppercase thisisanexample ThisIsNotAnExample Fall 2018 CS150 - Intro to CS I 12 Arithmetic Expressions Arithmetic expressions manipulate numeric data The main arithmetic operators are + addition - subtraction * multiplication / division 6
Fall 2018 CS150 - Intro to CS I 13 Mathematical Expressions Complex mathematical expressions are created by using multiple operators and grouping symbols expression: programming statement that has value sum = 21 + 3; expression number = 3; In these two examples, we assign the value of an expression to a variable Fall 2018 CS150 - Intro to CS I 14 Arithmetic Operators Operators allow us to manipulate data Unary: operator operand Binary: left operand operator right operand 7
Fall 2018 CS150 - Intro to CS I 15 Operator Precedence result = 4 * 2-3; result =? result = 12 + 6 / 3; result =? Rules on how to evaluate an arithmetic expression 1. arithmetic expressions are evaluated left to right 2. do them in order of precedence 3. grouping symbols ( ) Fall 2018 CS150 - Intro to CS I 16 Operator Precedence Operator Description () Items within parentheses are evaluated first unary - - used for negation (unary minus) is next * / % Next to be evaluated are *, /, and %, having equal precedence. + - Finally come + and - with equal precedence. left-to-right If more than one operator of equal precedence could be evaluated, evaluation occurs left to right. 8
Fall 2018 CS150 - Intro to CS I 17 Practice int x = 3; double y = 2.5; cout << 5 + 2 * 3; If you are unsure, you can always type up and run the code in Visual Studio cout << (10 / 2 y); cout << 3 + 12 * 2-3; cout << 4 + 17 / 3.0 + 9; cout << (6 - y) * 9 / x * 4-9; Fall 2018 CS150 - Intro to CS I 18 Problem Write a program that will compute the tip on a restaurant bill for a patron with a $42.80 meal charge. The tip must be 15% of the total bill. Read in the bill amount from the user, then display the meal cost, tip amount, and total bill on the screen. The program output should look like the following: Enter the cost of the meal: 42.80 The tip amount is: 6.42 The total cost is: 49.22 Press any key to continue... 9