completing the basic (l06) *assignment operations * formatting numbers for program output completing...

Download Completing the Basic (L06) *Assignment Operations * Formatting Numbers for Program Output Completing the Basic Dr. Ming Zhang

If you can't read please download the document

Upload: briana-jordan

Post on 02-Jan-2016

225 views

Category:

Documents


1 download

TRANSCRIPT

  • Completing the Basic (L06)

    *Assignment Operations* Formatting Numbers for Program Output

    Completing the Basic Dr. Ming Zhang

  • Assignment Operations* General Form of Assignment Statementvariable = operand;* Constant Operand for Assignmentlength = 25; (is read, length is assigned the value 25) * Overwritten with New Valuelength = 25;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* ExpressionAn expression is any combination of constants and variables that can be evaluated to yield a result.* Expression as the Operand of Assignment StatementThe 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 using std::cout;int main( ){}Output: The value of the circle area is 314.160000 Completing the Basic Dr. Ming Zhang

  • Assignment Variations* Assignment VariationsThe 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)sum -= 12; (sum = sum -12; sum = 18)sum *= 5; (sum = sum* 5; sum = 90)sum %= 3; (sum = sum%3; sum = 0)sum = 2;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 int main(void){ int sum = 0;cout
  • Counting* Counting Statement Formvariable = variable + fixed_number;

    * Examples of Counting Statementi = i + 1;n = n + 1; count = count + 1;j = j +2;m = m + 3kk = 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 int main(void){ int count = 0; cout
  • 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 using std::cout;int main( ) {}Output:

    Completing the Basic Dr. Ming Zhang

  • Data Type Conversion* Case Operator for Data Type Conversion (data_type) (expression);

    * Exampledouble 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 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
  • Exercise 3 : Data Type Conversion# Read follwing program and find out the output#inlude 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