UPDATING VARIABLES ADDITIONAL OPERATORS AND CASTING MR CRONE
UPDATING VARIABLES, ADDITIONAL OPERATORS, AND CASTING MR. CRONE
VARIABLE ASSIGNMENTS • The value of variables can change over time • Variables can be initialized to a starting value and can be changed later in the program int x = 4; x = 5; // x now has the value of 5 cout << x << endl; // prints 5
VARIABLE ASSIGNMENTS Example) double x = 4, y = 5, z = 7; z = 10; cout << z << endl; // prints 10
VARIABLE ASSIGNMENTS Normally take the form: variable = expression; Example) int num = 12; Can also take the form: variable = diff. Variable + expression Example) num 1 = num 2 + 25;
VARIABLE ASSIGNMENTS double x = 4, y = 5, z = 6; z = x + 6; // z takes the value of 4 + 6 cout << z << endl; // prints 10
ASSIGNMENT OPERATOR • “=“ is the assignment operator • It assigns the expression/variable on the right to the variable on the left int x = 4, y = 5; y = x; // y takes on the value of x cout << y << endl; // prints 4
ASSIGNMENT OPERATOR Example) double x = 7; x = x + 5; // x = 7 + 5 or x = 12 cout << x << endl; // prints 12
SHORTCUT ASSIGNMENT OPERATORS • C++ provides useful, shortcut assignment operators for assignment statements that use the same variable on both sides of the equation • Shortcut assignment operators: • • • += -= *= /= %=
SHORTCUT ASSIGNMENT OPERATORS Example) Let x = 5: x = x + 5; is equivalent to x += 5;
SHORTCUT ASSIGNMENT OPERATORS Equivalent Shortcut Operator Original Statement • • • x=x+5 y=y– 5 z=z*5 p=p/5 q=q%5 • • • x +=5 y-=5 z*=5 p /= 5 q %= 5
SHORTCUT ASSIGNMENT OPERATORS Example) double x = 20; x += 5; // x = 20 + 5 or cout << x << endl; // prints 25 x = 25
SHORTCUT ASSIGNMENT OPERATORS Example) double x = 15; x /= 3; cout << x << endl; // prints 5
INCREMENT AND DECREMENT OPERATORS • C++ provides unary operators specifically designed for adding and subtracting 1 • Increment operator: ++ • Decrement operator: --
INCREMENT AND DECREMENT OPERATORS Original Statement • y=y+1 • x=x-1 Equivalent Shortcut • y++ • x--
INCREMENT AND DECREMENT OPERATORS Example) double x = 5; x ++; // x = 5 + 1 cout << x << endl; // prints 6
MODULUS • “%” is used to represent modulus in C++ • Modulus is used to find the remainder when one number is divided by another Example) 5%3 =2 // 5 / 3 = 1 R 2
MODULUS Example) 20 % 3 = 2 // 20/3 = 6 R 2 Example) 21 % 3 = 0 // 21/3 = 7 R 0 Example) 13 % 4 = 1 // 13/4 = 3 R 1
CASTING • Numbers and variables can be temporarily cast to another type • Uses the format: Casting. Type (number or expression) Example) double x = 3. 5; cout << int(x) << endl; // prints 3
CASTING Example) double x = 3. 5, y = 4; cout << int(x + y) << endl;
CASTING Example) double x = 3. 5, y = 4; cout << int(x + y) << endl; // prints 7
CASTING Example) int x = 7, z = 2; cout << double(x/z) << endl;
CASTING Example) int x = 7, z = 2; cout << double(x/z) << endl; // prints 3
CASTING Example) int x = 7, z = 2; cout << double(x)/z << endl;
CASTING Example) int x = 7, z = 2; cout << double(x)/z << endl; // prints 3. 5
CONSTANT VARIABLES • Constant or “read only” variables, are variables that cannot be changed within the program • Constant variables are not capitalized the same as other variables • Constant variables are designated by using all caps and the const prefix For example) const double INTEREST = 3. 4; **Remember to include the variable type!
CONSTANT VARIABLES What is wrong with this code? const int AGE = 52; int height = 72; AGE += 5; cout << AGE << endl;
CONSTANT VARIABLES What is wrong with this code? const int AGE = 52; int height = 72; AGE += 5; // the AGE variable cannot change! cout << AGE << endl;
CONSTANT VARIABLES • Attempts to change constant variables will be flagged by the compiler • Constant variables are frequently used with known math constants that stay the same throughout the program • For Example) PI, e, and gravity
CONSTANT VARIABLES • What is wrong with this code? int x = 4; const y = 3. 5; cout << x + y << endl;
CONSTANT VARIABLES • What is wrong with this code? int x = 4; const y = 3. 5; // Does not declare variable type! Error! cout << x + y << endl;
CONSTANT VARIABLES • Constant variables can be used inside mathematical expressions For Example) const double PI = 3. 14; cout << PI * 3 << endl; // perfectly legal
ORDER OF DECLARATION int main(){ // constant variables // other variables // COUT / CIN Statements // Calculation Statements // COUT Statements }
- Slides: 32