Arithmetic Expressions - PowerPoint PPT Presentation

About This Presentation
Title:

Arithmetic Expressions

Description:

Variable on the left-hand side of an assignment is of type int, while the ... If the variable on the left-hand side is a double, but the expression on the ... – PowerPoint PPT presentation

Number of Views:113
Avg rating:3.0/5.0
Slides: 9
Provided by: arup2
Learn more at: http://www.cs.ucf.edu
Category:

less

Transcript and Presenter's Notes

Title: Arithmetic Expressions


1
Arithmetic Expressions
  • Addition ()
  • Subtraction (-)
  • Multiplication ()
  • Division (/)
  • Integer
  • Real Number
  • Mod Operator ()
  • Same as regular
  • Same as regular
  • Same as regular
  • Depends on the types of the operands involved.
  • Remainder operator

2
Order of Operations
  • Same as math class
  • ()
  • , /,
  • , -
  • Heres an example
  • 3 45 - 6/34/8 26 - 432
  • 3 20 - 1 12 24
  • 10

3
Integer Division
  • If the two operands are of type int, then integer
    division occurs
  • An integer division truncates (chops off) any
    fractional part of the answer.
  • Examples
  • 13/4 3
  • 7/8 0
  • 19/3 6

4
Real Number Division
  • If at least one of the two operands is a double
    (or float), then a real number division occurs
  • Examples
  • 13/4.0 3.25
  • 13.0/4.0 3.25
  • 19.0/5 3.8

5
Other Issues with Division
  • Variable on the left-hand side of an assignment
    is of type int, while the expression on the right
    is a real number
  • val 8/5.0, will set val to 1, if val is an int.
  • If the variable on the left-hand side is a
    double, but the expression on the right is an
    int, the variable gets set to an int.
  • val 8/5, will set val to 1, if val is a double.

6
Mod Operator
  • A B returns the remainder when A is divided by
    B, and A and B MUST BE ints!
  • Examples
  • 125 2
  • 196 1
  • 147 0
  • 19200 19

7
Initializing Variables
  • If you declare a variable without a value, then
    that variable initially could equal anything
  • Usually, its a good practice to give your
    variables initial values.
  • Examples
  • int sum 0, value 1
  • double price 0.0

8
Defining Constants
  • Use define
  • Examples
  • define FEET_IN_YARD 3
  • define PI 3.14159
  • Benefits
  • Code is Easier to Read
  • If a constant needs to be changed, you only
    need to change it at the top of your program.
    (ie. if you find out that some program parameter
    has changed)
Write a Comment
User Comments (0)
About PowerShow.com