assignment and arithmetic operators n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Assignment and Arithmetic Operators PowerPoint Presentation
Download Presentation
Assignment and Arithmetic Operators

Loading in 2 Seconds...

play fullscreen
1 / 17

Assignment and Arithmetic Operators - PowerPoint PPT Presentation


  • 114 Views
  • Uploaded on

Assignment and Arithmetic Operators. 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. Assignment Operator. “=” is used to assign a value to a variable int bob = 5;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Assignment and Arithmetic Operators' - yanka


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
what are operators
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
assignment operator
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

steps of execution
Steps of Execution

tax = income * RATE;

steps of execution1
Steps of Execution

tax = income * RATE;

steps of execution2
Steps of Execution

float

float

tax = income * RATE;

steps of execution3
Steps of Execution

tax = income * RATE;

casting
Casting

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

arithmetic operators
Arithmetic Operators

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

someValue = num1 + num2; // addition

someValue = num1 – num2; // subtraction

someValue = num1 * num2; // multiplication

someValue = num1 / num2; // division

someValue = num1 % num2; // modulus

type promotion
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

example 1
Example 1

float celc;

intfahr;

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

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

example 2
Example 2

float average_age;

inttotal_of_ages, num_people;

average_age = total_of_ages / num_people;

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

modular arithmetic
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;

increment and decrement
Increment and Decrement
  • val = val + 1;
    • val++; or ++val;
  • val = val – 1;
    • val--; or --val;
pre vs post
Pre- vs. Post-

intval = 6;

int num;

num = ++val;

intval = 6;

int num;

num = val++;

other fast operators
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;