Add Two Numbers - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Add Two Numbers

Description:

4. CS 1430. CSSE Dept. 4. Assignment Operator: = num1 num2 = Sum; ... Statement Terminator. Assignment Operator. total = total num1; // from right to left ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 18
Provided by: qya
Category:
Tags: add | numbers | terminator | two

less

Transcript and Presenter's Notes

Title: Add Two Numbers


1
Add Two Numbers
  • In Math
  • S x y
  • Or
  • x y S

2
Add Two Numbers In C
  • Sum num1 num2
  • // This is a comment
  • // Semicolon Statement terminator
  • // Use meaningful names
  • Sum num1 num2

3
Assignment Operator
  • num1 num2 Sum
  • num1 num2 Sum // Wrong!
  • //
    Assignment statement MUST
  • // be from
    right to left
  • // Not
    equal
  • Sum num1 num2 // Correct!
  • // Not
    equal

4
Are the two statements the same?
  • Sum Num1 Num2
  • Sum num1 num2
  • // NO!
  • // C is case sensitive!

5
Arithmetic Operators
  • Addition
  • Subtraction -
  • Multiplication
  • Division /
  • What is in C?
  • x y // Not good!
  • What is in C?
  • x y // Not good!

6
Semantics, Syntax, and Style
  • x x 2
  • Semantics
  • Increase x by 2
  • Assignment (right to left)
  • Syntax
  • Statement terminator
  • Case sensitive
  • Style
  • one space before and after
  • any operator

7
Precedence Rules
  • From low to high
  • , -
  • , /
  • ( )

8
Examples
  • x 8 - 4 2
  • // result
  • x (8 - 4) 2
  • // result
  • y 8 / 4 2
  • // result
  • y 8 / (4 2)
  • // result

9
More Examples
  • z 8 / 4 2
  • // No!
  • z 8 / 4 4
  • // NO!
  • z 8 / 4 4
  • // Result ?
  • z 5 / (2 5)
  • // Result 0.5 or 0?

10
Integer division vs. Float division
  • In Math
  • 5 is almost always the same as 5.0
  • In C
  • 5 is almost never the same as 5.0
  • Why?
  • Store value in computer as bits, bytes.
  • 5 is stored as an integer
  • 5.0 is stored as a float number

11
Integer Division
  • 7 / 5
  • Quotient 1
  • Remainder 2
  • 5 / 7
  • Quotient 0
  • Remainder 5

12
Float Division
  • 7.0 / 5.0
  • Quotient 1.4
  • 7 / 5.0 (Same)
  • 7.0 / 5 (Same)
  • 5.0 / 7
  • Quotient 0.714285

13
Quotient (Division) Operator
  • Expression Result
  • 7 / 5 1
  • 14 / 5 2
  • 143 / 10 14

14
Remainder (Modular) Operator
  • Expression Result
  • 7 5
  • 14 5
  • 143 10

15
Exercises
  • Expression Result
  • 12 20 3
  • 20 12 3
  • 12 / 20 3
  • 20 / 12 3
  • 20 / 12 3
  • 20 12 3

16
Summary
  • Statement Terminator
  • Assignment Operator
  • total total num1 // from right to
    left
  • Arithmetic Operators and Precedence Rules
  • -,
  • /, ,
  • ()
  • Semantics, Syntax, and Style

17
Quiz 1 (Part II)
  • Due Monday in class
Write a Comment
User Comments (0)
About PowerShow.com