1 CS150 Introduction to Computer Science 1 Arithmetic Operators.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

Lilian Blot CORE ELEMENTS PART I Python Language Autumn 2013 TPOP 1.
1 C Fundamentals - I Math 130 Lecture # 2 B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Sp05: With discussion.
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
Types and Arithmetic Operators
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.
Friday, December 08, 2006 “Experience is something you don't get until just after you need it.” - Olivier.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
CS150 Introduction to Computer Science 1
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
Chapter 2 Data Types, Declarations, and Displays
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Operaciones y Variables
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
LESSON 6 – Arithmetic Operators
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
CPS120: Introduction to Computer Science Operations Lecture 9.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
CSC 107 – Programming For Science. The Week’s Goal.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Data Types Declarations Expressions Data storage C++ Basics.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Operators.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Operators & Expressions
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Introduction to Programming Lesson 3. #include #include main ( ) { cout
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CS 31 Discussion, Week 2 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
CSE 220 – C Programming Expressions.
Lecture 4: Expressions and Variables
for Repetition Structures
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Arithmetic operations & assignment statement
Assignment and Arithmetic expressions
Assignment statement and Arithmetic operation 2
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
OPERATORS (1) CSC 111.
Arithmetic Expressions in C
Arithmetic Expressions & Data Conversions
Alternate Version of STARTING OUT WITH C++ 4th Edition
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CS150 Introduction to Computer Science 1
Data Types and Expressions
Lecture 4: Expressions and Variables
Arithmetic Operations
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

1 CS150 Introduction to Computer Science 1 Arithmetic Operators

2 9/15/08 Today  Arithmetic Operators & Expressions o sections 2.15 & 3.2 o Computation o Precedence o Algebra vs C++ o Exponents CS150 Introduction to Computer Science 1

3 9/15/08 Assigning float s to int s  Look at the following situation. int intVariable; intVariable = 42.7; cout << intVariable;  What do you think is the output? CS150 Introduction to Computer Science 1

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

5 9/15/08 Arithmetic Operators  Operators allow us to manipulate data o Unary: operator operand o Binary: operand operator operand OperatorMeaningTypeExample -NegationUnary =AssignmentBinary rate = 0.05 *MultiplicationBinary cost * rate /DivisionBinary cost / 2 %ModulusBinary cost % 2 +AdditionBinary cost + tax -SubtractionBinary total - tax CS150 Introduction to Computer Science 1 (left hand side) (right hand side)

6 9/15/08 Integer Division  What is the output? o int grade; grade = 100 / 20; cout << grade; o int grade; grade = 100 / 30; cout << grade; CS150 Introduction to Computer Science 1

7 9/15/08 Division  grade = 100 / 40; 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 after the decimal point is dropped. No rounding.  grade = / 40; o grade is 2.5 o What data type should grade be declared as? CS150 Introduction to Computer Science 1

8 9/15/08 Modulus  Modulus is the remainder after integer division  grade = 100 % 20; o grade = ?  grade = 100 % 30; o grade = ?  rem = x % n; o What are the possible values for rem ? 9/14/07CS150 Introduction to Computer Science 1

9 9/15/08 Practice  Q.1. What value is assigned to x? a. x = 8 + 3; b. x = 8 - 3; c. x = 8 * 3; d. x = 8 % 3; e. x = 8 / 3; CS150 Introduction to Computer Science 1

10 9/15/08 Mathematical Expressions  Complex mathematical expressions are created by using multiple operators and grouping symbols o expression: programming statement that has value o sum = ; o number = 3; CS150 Introduction to Computer Science 1 expression In these two examples, we assign the value of an expression to a variable

11 9/15/08 Examples  result = x;  result = 4 + result;  result = 15 / 3;  result = 22 * number;  result = a + b % c;  result = a + b + d / c – q + 42;  cout << “The value: “ << (sum / 2) << endl; CS150 Introduction to Computer Science 1

12 9/15/08 Operator Precedence  result = a + b + d;  result = / 3; o result = ?  Rules on how to evaluate an arithmetic expression o arithmetic expressions are evaluated left to right o when there are two operators, do them in order of precedence CS150 Introduction to Computer Science 1

13 9/15/08 Operator Precedence Precedence of Arithmetic Operators (Highest to Lowest) (unary negation) - * / % + - CS150 Introduction to Computer Science 1 If two operators have the same precedence, evaluate them from left to right as they appear in the expression

14 9/15/08 Q.2. Practice a * 3 b. 10 / 2 -1 c * d % e * 9 / 3 * CS150 Introduction to Computer Science 1

15 9/15/08 Summary  Today we have looked at: o Arithmetic Operators & Expressions  Next time we will: o Continue looking at mathematic operators  Completed section 2.15 & started on section 3.2 CS150 Introduction to Computer Science 1