Arithmetic Expressions in C

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
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.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
LESSON 6 – Arithmetic Operators
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
§ 1.7 Multiplication and Division of Real Numbers.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
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.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
Doing math In java.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
1 Variables and Arithmetic Operators in JavaScript.
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.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSE 220 – C Programming Expressions.
UMBC CMSC 104 – Section 01, Fall 2016
Expressions.
Chapter 7: Expressions and Assignment Statements
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
UMBC CMSC 104 – Section 01, Fall 2016
Expressions.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
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.
Assignment and Arithmetic expressions
Variables and Arithmetic Operators in JavaScript
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Arithmetic Operators in C
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Variables and Arithmetic Operators in JavaScript
Arithmetic Operators in C
Arithmetic Expressions & Data Conversions
Introduction to C Topics Compilation Using the gcc Compiler
Alternate Version of STARTING OUT WITH C++ 4th Edition
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.
CS150 Introduction to Computer Science 1
Data Types and Expressions
Introduction to C Topics Compilation Using the gcc Compiler
Arithmetic Operations
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Building Java Programs
Presentation transcript:

Arithmetic Expressions in C CMSC 104, Section 4 Richard Chang [Based on dblock spr’06lecture, heavily modified, reordered, many new slides added]

Arithmetic Operators in C Binary Operators E.g.: new_value = height + margin; area = length * width; Unary Operators E.g.: new_value = -old_value; negation = !true_value;

Arithmetic Operators in C Name Operator Example Addition + num1 + num2 Subtraction – initial – spent Multiplication * fathoms * 6 Division / sum / count Modulus % m % n

Mixing Types The value 22.5 is stored in total int num = 5; double avg_length = 4.5 ; double total ; total = num * avg_length ; The value 22.5 is stored in total

Mixing Types (cont) The value 4 is stored in total int num = 5 ; double avg_length ; int total = 22 ; avg_length = total / num ; The value 4 is stored in total

Division If both operands of a division expression are integers, you will get an integer answer. The fractional portion is thrown away. Examples : 17 / 5 = 3 4 / 3 = 1 35 / 9 = 3

Division (con’t) Division where at least one operand is a floating point number will produce a floating point answer. Examples : 17.0 / 5 = 3.4 4 / 3.2 = 1.25 35.2 / 9.1 = 3.86813

Division By Zero Division by zero is mathematically undefined. If you allow division by zero in a program, it will cause a fatal run-time error. Non-fatal errors do not cause program termination, just produce incorrect results.

Modulus The expression m % n yields the integer remainder after m is divided by n. Modulus is an integer operation -- both operands MUST be integers. Examples : 17 % 5 = 2 6 % 3 = 0 9 % 2 = 1 5 % 8 = 5

Uses for Modulus Used to determine if an integer value is even or odd 5 % 2 = 1 odd 4 % 2 = 0 even Converting inches to feet & inches (HW 2) 75 % 12 = 3

Arithmetic Operators Rules of Operator Precedence Operator(s) Precedence & Associativity ( ) Evaluated first. If nested (embedded), innermost first. If on same level, left to right. * / % Evaluated second. If there are several, evaluated left to right. + - Evaluated third. If there are several, evaluated left to right. = Evaluated last, right to left.

Using Parentheses Use parentheses to change the order in which an expression is evaluated. a + b * c (a + b) * c Also use parentheses to clarify a complex expression.

Practice With Evaluating Expressions Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4, evaluate the following expressions: a + b - c + d a * b / c 1 + a * b % c a + d % b - c e = b = d + c / b - a