Completing the Basic (L06) *Assignment Operations * Formatting Numbers for Program Output Completing the Basic Dr. Ming Zhang
Assignment Operations * General Form of Assignment Statement variable = operand; * Constant Operand for Assignment length = 25; (is read, length is assigned the value 25) * Overwritten with New Value length = 6.28; ( The 25 that was in length is overwritten with new value of 6.28, because a variable can only store one value at a time) Completing the Basic Dr. Ming Zhang
Expression as the Assignment Operand An expression is any combination of constants and variables that can be evaluated to yield a result. * Expression as the Operand of Assignment Statement The expression in an assignment statement can be used to perform calculations using the arithmetic operators. diff = 16 - 6; product = 5 * 6; new_value=diff+product+20; /*new_value=60*/ Completing the Basic Dr. Ming Zhang
Exercise 1: the Value of a Circle Area (C++) # Using C++, write a program to calculate # the value of a circle area. #include <iostream> using std::cout; int main( ) { … } Output: The value of the circle area is 314.160000 Completing the Basic Dr. Ming Zhang
Assignment Variations The variable on the left of the equal sign can also be used the right of the equal sign in the assignment statement sum = 20; sum = sum +10; /* sum = 30 */ * Assignment expressions like sum = sum + 25, which use the same variable on both sides of the assignment operator, can be written using the following assignment operators: += -= *= %= Completing the Basic Dr. Ming Zhang
Assignment Operators sum = 20 sum += 10; ( sum = sum + 10; sum = 30) rate = 5; sum *= rate; ( sum = sum*rate; sum = 10) sum *= sum+2; (sum = sum*(sum+2); sum = 120) sum *= rate+2; (sum = sum*(rate+2); sum = 840) Completing the Basic Dr. Ming Zhang
Accumulating #include <iostream> int main(void) { int sum = 0; cout <<“The value of sum is set to ”<<sum<<endl; sum = sum +10; cout << “sum is now ” << sum; sum = sum +20; return 0;} Output: The value of sum is set to 0 sum is now 10; sum is now 30; /* sum is NOT 20*/ Completing the Basic Dr. Ming Zhang
Counting * Counting Statement Form variable = variable + fixed_number; * Examples of Counting Statement i = i + 1; n = n + 1; count = count + 1; j = j +2; m = m + 3 kk = kk + 4; Completing the Basic Dr. Ming Zhang
Increment and Decrement Operators * Increment Operators i = i + 1; ++i; n = n + 1; ++n; count = count + 1; ++ count; * Decrement Operators i = i - 1; --i; n = n - 1; --n; count = count - 1; -- count; Completing the Basic Dr. Ming Zhang
Example of Increment Operators #include <iostream> int main(void) { int count = 0; cout <<“The initial value of count is ” << count; ++count; cout <<“count is now ” << count ; ++count; ++count; cout <<“count is now ” << count; return 0;} Completing the Basic Dr. Ming Zhang
Exercise2 : Increment Operators (C++) # Using increament operators and C++, write a # program to print out on the screen: # The initial value of count is 0. # count is now 1. # count is now 3. #include <iostream> using std::cout; int main( ) { … } Output: Completing the Basic Dr. Ming Zhang
Data Type Conversion * Case Operator for Data Type Conversion (data_type) (expression); * Example double a=2.3, b=5.0, product1; int product2; product1 = a * b; /* product1 =11.5 */ product2 = (int) (a*b); /* product2 = 11 */ product1 = (int) a * b; /*(int)a==2. product1 = 10.0*/ Completing the Basic Dr. Ming Zhang
Example of Data Type Conversion #inlude <iostream> using std::cout; int main( ) { double a=3.3, b=6.0, product1; int product2; product1 = a * b; /* product1 =19.8 */ product2 = (int) (a*b); /* product2 = 19 */ product1 = (int) a * b; /*(int)a==3. product1 = 18.0*/ cout << “product1=“ << product1 << endl; cout << “product2=“ << product2 << endl; return(0); } Completing the Basic Dr. Ming Zhang
Exercise 3 : Data Type Conversion # Read follwing program and find out the output #inlude <iostream> using std::cout; int main( ) { double a=5.3, b=4.0, product1; int product2; product1 = a * b; product2 = (int) (a*b); product1 = (int) a * b; cout << “product1=“ << product1 << endl; cout << “product2=“ << product2 << endl; return(0); } Completing the Basic Dr. Ming Zhang