Arithmetic Operators and Expressions in CS150, Slides of Computer science

The topic of arithmetic operators and expressions in the context of the CS150 Introduction to Computer Science course. It includes examples of different operators, their meanings, and precedence rules. Students will learn about integer division, modulus, and mathematical expressions.

Typology: Slides

2021/2022

Uploaded on 09/27/2022

alenapool
alenapool 🇬🇧

4.6

(13)

223 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Arithmetic Operators
1
CS150 Introduction to Computer Science 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Arithmetic Operators and Expressions in CS150 and more Slides Computer science in PDF only on Docsity!

Arithmetic Operators

CS150 Introduction to Computer Science 1

Today •

Arithmetic Operators & Expressions o sections 2.15 & 3. o Computation o Precedence 9/15/ o Algebra vs C++ o Exponents CS150 Introduction to Computer Science 1

Assigning

float

s to

int

s

What is the output here? int intVariable; double doubleVariable 78.9; intVariable = doubleVariable; 9/15/ cout << intVariable; CS150 Introduction to Computer Science 1

Arithmetic Operators •

Operators allow us to manipulate data o Unary: operator operand o Binary: operand operator operand Operator Meaning Type Example

Negation Unary - 5 (left hand side) (right hand side) 9/15/

Negation Unary - 5 = Assignment Binary rate = 0.

Multiplication Binary **cost

rate** / Division Binary cost / 2 % Modulus Binary cost % 2

Addition Binary **cost

tax**

Subtraction Binary **total

tax** CS150 Introduction to Computer Science 1

Division •

grade

grade is 2 o If both operands of the division operator are integers, then integer division is performed.  the data type of grade is not considered, why? o We say the integer is truncated. Everything 9/15/ o We say the integer is truncated. Everything after the decimal point is dropped. No rounding.

grade

o grade is 2. o What data type should grade be declared as? CS150 Introduction to Computer Science 1

Modulus •

Modulus is the remainder after integerdivision

grade

o grade =? 9/15/

grade

o grade =?

rem = x

n; o What are the possible values for rem ? 9/14/ CS150 Introduction to Computer Science 1

Mathematical Expressions •

Complex mathematical expressions arecreated by using multiple operators andgrouping symbols o expression: programming statement that has value 9/15/ value o sum = 21 + 3; o number = 3; CS150 Introduction to Computer Science 1 expression In these two examples,we assign the value ofan expression to a variable

Examples •

result

x;

result

+ result;

result

result

number; 9/15/

result

number;

result

**a

  • b**

c;

result

**a

  • b**

d

c

- q

cout << “The value: “ << (sum / 2) << endl; CS150 Introduction to Computer Science 1

Operator Precedence

Precedence of Arithmetic Operators(Highest to Lowest) (unary negation) - 9/15/

/ %

  • CS150 Introduction to Computer Science 1 If two operators have the same precedence, evaluatethem from left to right as they appear in the expression

Q.2. Practice a.

b.

c.

d.

9/15/ d.

e.

CS150 Introduction to Computer Science 1