Arithmetic Operator Operation Example + addition x + y

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

Arithmetic Calculations
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.
Friday, December 08, 2006 “Experience is something you don't get until just after you need it.” - Olivier.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
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.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Arithmetic in Pascal A Short Glance We will learn the followings in this chapter Arithmetic operators Order of precedence Assignment statements Arithmetic.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Mathematical Calculations in Java Mrs. C. Furman.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
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 *
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Doing math In java.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
Operators & Expressions
CSE 220 – C Programming Expressions.
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
BIL 104E Introduction to Scientific and Engineering Computing
Relational Operations
Chapter 7: Expressions and Assignment Statements
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
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Assignment statement and Arithmetic operation 2
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
Arithmetic Expressions in C
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Arithmetic Expressions & Data Conversions
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Introduction to Programming – 4 Operators
Expressions and Assignment
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
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.
Chapter 3 Operators and Expressions
Data Types and Expressions
Chapter 4: Expression and Operator
OPERATORS in C Programming
Operator King Saud University
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Data Types and Expressions
OPERATORS in C Programming
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Arithmetic Operator Operation Example + addition x + y - subtraction x - y * multiplication x * y / division x / y Mathematical Formula C Expressions b2 – 4ac b * b – 4 * a * c a + bc a + b * c a + b (a + b) / (c + d) c + d 1 1 / (1 + x * x) 1 + x2 ab – (b + c) a * b - (b + c)

Integer Division and Remainders When applied to two positive integers, the division operator computes the integral quotient. 7 / 2 is equal to 3 7.0 / 2.0 is equal to 3.5 % (Modulo) Must be applied to integers Returns the integer remainder of the result of dividing the first operand by the second. 7 % 2 is equal to 1

Data types of expressions The data type of an expression depends on the data type of its operands. If both operands are of type integer, the result is integer. If both operands are of type double, the result is double. If one operand is integer and the other is double, the integer value is converted to double and the result of the operation is double. Placing a minus sign (unary - ) in front of an arithmetic expression does not change its type. - ( 3/2 ) is the value –1 - ( 3.0 / 2 ) is the value –1.5

Mixed-Type Assignment int m; double x; If an int value is assigned to a double variable, the integer value is converted to double. x = 3; is the same as x 3.0 If a double value is assigned to an int variable, the fractional part is lost. m = 3.9 is the same as m 3 When an assignment statement is executed, the expression is evaluated and then the result is assigned to the variable on the left hand side of the = operator. m = 4 * 2.5 m 10 x = 17 / 2 x 8.0 x = 17 / 2.0 x 8.5 m = 17 / 2.0 m 8

Operator Precedence Decreasing priority All operators of the same priority are listed on the same line Highest priority: unary+, unary- High priority: *, /, % Low priority: +, - Lowest priority: = Operations of the same priority are evaluated from left to right. 9 – 6 – 2 3 – 2 1 8 / 4 / 2 2 / 2 1 12.0 / 4.0 * 3.0 3.0 * 3.0 9.0 4 + 3 * 5 / 2 4 + 15 / 2 4 + 7 11 Decreasing priority

Operator Precedence May use ( )’s to modify the order of evaluation. 12.0 / (4.0 * 3.0) 12.0 / 12.0 1.0 (4 + 3) * 5 / 2 7 * 5 / 2 35 / 2 17 (4 + 3) * (5 / 2) 7 * (5 / 2) 7 * 2 14 4 + 3 * ( 5 / 2) 4 + (3 * 2) 4 + 6 10

Compound operations a = a + b can be written as Increment / decrement Similarly, for *, -, / Increment / decrement a = a + 1 can be written as a++ : means a is incremented after it is used in that expression ++a : means a is incremented before it is used in that expression E.g.: a = 4; y = a++ * 4; stores 16 in y and 5 in a But: a = 4; y = ++a * 4; stores 20 in y and 5 in a

Sources of Error in Real Arithmetic Round-off error in simple addition and subtraction Loss of extra digits in multiplication and division Adding a large number to a small number Subtracting almost equal numbers Overflow and underflow Overflow Example: Using only numbers 0..99, what’s 90 + 50? 90 +50 ----- 140 40 Underflow Example: Using only integers, what’s 1/3?