Assignment and Arithmetic Operators http://cs.mst.edu What are - - PowerPoint PPT Presentation

assignment and arithmetic
SMART_READER_LITE
LIVE PREVIEW

Assignment and Arithmetic Operators http://cs.mst.edu What are - - PowerPoint PPT Presentation

Assignment and Arithmetic Operators http://cs.mst.edu What are operators? Operators allow us to modify and manipulate information. They are symbols that represent a commonly used operation, such as addition 2 + 2 http://cs.mst.edu


slide-1
SLIDE 1

http://cs.mst.edu

Assignment and Arithmetic Operators

slide-2
SLIDE 2

http://cs.mst.edu

What are operators?

  • Operators allow us to modify and manipulate

information.

  • They are symbols that represent a commonly

used operation, such as addition

  • 2 + 2
slide-3
SLIDE 3

http://cs.mst.edu

Assignment Operator

  • “=” is used to assign a value to a variable

int bob = 5; tax = income * RATE; RATE = .05; //NO; RATE is const 4 = income + 64; //NO,can’t modify 4

slide-4
SLIDE 4

http://cs.mst.edu

Steps of Execution

tax = income * RATE;

slide-5
SLIDE 5

http://cs.mst.edu

Steps of Execution

tax = income * RATE;

slide-6
SLIDE 6

http://cs.mst.edu

Steps of Execution

tax = income * RATE;

float float

slide-7
SLIDE 7

http://cs.mst.edu

Steps of Execution

tax = income * RATE;

slide-8
SLIDE 8

http://cs.mst.edu

Casting

int someValue; double Num1, Num2; someValue = Num1 + Num2; someValue = static_cast<int>(Num1 + Num2);

slide-9
SLIDE 9

http://cs.mst.edu

Arithmetic Operators

  • “+”, “-”, “*”, “/”, “%” (modular arithmetic)

someValue = num1 + num2; // addition someValue = num1 – num2; // subtraction someValue = num1 * num2; // multiplication someValue = num1 / num2; // division someValue = num1 % num2; // modulus

slide-10
SLIDE 10

http://cs.mst.edu

Type Promotion

int integer1; float float1; float1 + integer1 // gives a float float1 – integer1 // gives a float float1 * integer1 // gives a float float1 / integer1 // gives a float integer1 / float1 // gives a float float1 % integer1 // can’t be done integer1 % float1 // can’t be done

slide-11
SLIDE 11

http://cs.mst.edu

Example 1

float celc; int fahr; celc = (5/9)*(fahr-32); celc = (5.0/9)*(fahr-32);

slide-12
SLIDE 12

http://cs.mst.edu

Example 2

float average_age; int total_of_ages, num_people; average_age = total_of_ages / num_people; average_age = static_cast<float>(total_of_ages) / num_people;

slide-13
SLIDE 13

http://cs.mst.edu

Modular Arithmetic

  • If a % b, then the result is the remainder of a / b
  • 4%7 is 4 (since 4/7 is 0 with remainder 4)
  • 7%3 is 1 (since 7/3 is 2 with remainder 1)
  • 27%3 is 0 (since 27/3 is 9 with remainder 0)
  • Cool trick…

int tens_digit; tens_digit = (x%100)/10;

slide-14
SLIDE 14

http://cs.mst.edu

Increment and Decrement

  • val = val + 1;
  • val++; or ++val;
  • val = val – 1;
  • val--; or --val;
slide-15
SLIDE 15

http://cs.mst.edu

Pre- vs. Post-

int val = 6; int num; num = ++val; int val = 6; int num; num = val++;

slide-16
SLIDE 16

http://cs.mst.edu

Other Fast Operators

x += y; // equivalent to x = x + y; x -= y; // equivalent to x = x - y; x /= y; // equivalent to x = x / y; x *= y; // equivalent to x = x * y; x %= y; // equivalent to x = x % y;

slide-17
SLIDE 17

http://cs.mst.edu

End of Session