Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *

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
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
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.
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.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
CSC Programming I Lecture 5 August 30, 2002.
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.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Mathematical Calculations in Java Mrs. G. Chapman.
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.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
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.
Executable Statements 1. Def. Executable statements are instructions to the computer to perform specific tasks. 2. Two examples: method calls and assignment.
Lesson 6 Getting Started in C Programming Hu Junfeng 2007/10/10.
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.
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.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
C-1 9/30/99 CSE / ENGR 142 Programming I Arithmetic Expressions © 1999 UW CSE.
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.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
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.
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
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
Arithmetic Operator Operation Example + addition x + y
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
Arithmetic Expressions in C
Arithmetic Expressions & Data Conversions
C Operators, Operands, Expressions & Statements
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
Chapter 3 Operators and Expressions
Data Types and Expressions
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
OPERATORS in C Programming
Data Types and Expressions
OPERATORS in C Programming
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb * b – 4 * a * c a + bca + b * c a + b (a + b) / (c + d) c + d 1 1 / (1 + x * x) 1 + x 2 ab – (b + c)a * b - (b + c)

Integer Division and Remainders / (Division)  When applied to two positive integers, the division operator computes the integral quotient. 7 / 2 is equal to / 2.0 is equal to 3.5 % (Mod)  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 the 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.5m 10 x = 17 / 2x 8.0 x = 17 / 2.0x 8.5 m = 17 / 2.0m 8

Operator Precedence  All operators of the same priority are listed on the same line High priority: *, /, % Low priority: +, - Operations of the same priority are evaluated from left to right. 9 – 6 – 2 3 – / 4 / 2 2 / / 4.0 * * * 5 / /

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

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 ? Underflow Example: Using only integers, what’s 1/3? 0