C-1 9/30/99 CSE / ENGR 142 Programming I Arithmetic Expressions © 1999 UW CSE.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
COEN Expressions and Assignment
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
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.
ISBN Chapter 7 Expressions and Assignment Statements.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
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.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
ISBN Chapter 7 Expressions and Assignment Statements.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Expressions, Data Conversion, and Input
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
C H A P T E R S E V E N Expressions and Assignment Statements.
Chapter 2 part #4 Operator
LESSON 6 – Arithmetic Operators
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
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.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
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 *
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
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 =.
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.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
CSE 220 – C Programming Expressions.
Expressions and Assignment Statements
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Relational Operations
University of Washington Computer Programming I
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.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Expressions and Assignment Statements
Arithmetic Operator Operation Example + addition x + y
Arithmetic Expressions in C
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Expressions and Assignment Statements
College of Computer Science and Engineering
Arithmetic Expressions & Data Conversions
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.
Data Types and Expressions
Chapter 7 Expressions and Assignment Statements.
Operator King Saud University
PRESENTED BY ADNAN M. UZAIR NOMAN
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

C-1 9/30/99 CSE / ENGR 142 Programming I Arithmetic Expressions © 1999 UW CSE

C-2 9/30/99 Assignment Statement Review double area, radius; area = 3.14 * radius * radius; assignment statement expression

C-3 9/30/99 Expressions  Expressions are things that have values  A variable by itself is an expression: radius  A constant by itself is an expression: 3.14  Often expressions are combinations of variables, constants, and operators. area = 3.14 * radius * radius;  The overall value of the expression is based on the data and operators specified.  Data means the integer or floating-point constants and/or variables in the expression.  Operators are things like addition, multiplication, etc.

C-4 9/30/99 The Big Picture  In an assignment statement,  the expression (right hand side) is first evaluated,  then its value is assigned to (stored in) the assignment variable (left hand side).  How this happens depends on the data types in the expression, the operators, and the type of the assignment variable. my_int = int1 + int2; int1 int2 my_int 1 3

C-5 9/30/99 Unary and Binary Binary: operates on two things 3.0 * b zebra + giraffe Unary: operates on one thing C operators are unary or binary Then what about expressions like a+b+c?

C-6 9/30/99 Expressions with doubles REVIEW: Doubles are floating-point values that represent real numbers within the computer. Constants of type double: 0.0, 3.14, -2.1, 5.0, 6.02e23, 1.0e-3 not 0 or 17 Operators on doubles: unary: - binary: +, -, *, /

C-7 9/30/99 Expressions with doubles: Examples double height, base, radius, x, c1, c2 ; Sample expressions (not statements): 0.5 * height * base ( 4.0 / 3.0 ) * 3.14 * radius * radius * radius c1 * x - c2 * x * x

C-8 9/30/99 Expressions with ints REVIEW: An integer represents a whole number with no fractional part. Constants of type int: 0, 1, -17, 42 not 0.0 or 1e3 Operators on ints: unary: - binary: +, -, *, /, %

C-9 9/30/99 / is integer division: no remainder, no rounding 299 / 1002,6 / 41,5 / 60 % is mod or remainder: 299 % 10099,6 % 42,5 % 65 int division and remainder Integer operators include integer division and integer remainder )

C-10 9/30/99 Expressions with ints: Examples Given:total_minutes359 Find:hours5 minutes59 Solution: hours = total_minutes / 60 ; minutes = total_minutes % 60 ;

C-11 9/30/99 A Cautionary Example int radius; double area;. area = ( 22 / 7 ) * radius * radius;

C-12 9/30/99 Why Use ints? Why Not doubles Always?  Sometimes only ints make sense  “give me the 15 th spreadsheet cell”  “give me the ( ) th cell” ??  Doubles may be inaccurate representing “ints”  In mathematics 3 15 (1/3) = 15  In computer arithmetic 3.0 * 15.0 * (1.0 / 3.0) might be  Last, and least  arithmetic with doubles is often slower  doubles often require more memory

C-13 9/30/99 Operator Precedence Precedence determines the order of evaluation of operators. Is a + b * a - b equal to ( a + b ) * ( a - b ) or a + ( b * a ) - b ?? And does it matter? Try this: * (4 + 3) * (2 - 1) = 4 + (3 * 2) - 1 =

C-14 9/30/99 Operator Precedence Precedence rules: 1. do ( )’s first, starting with innermost 2. then do unary minus (negation): - 3. then do multiplicative ops: *, /, % 4. lastly do additive ops: binary +, -

C-15 9/30/99 Associativity Associativity determines the order among consecutive operators of equal precedence Is a / b * c equal to a / ( b * c ) or ( a / b ) * c ?? Most C arithmetic operators are "left associative", within the same precedence level a / b * c equals (a / b) * c a + b - c + d equals ( ( a + b ) - c ) + d C also has a few operators that are right associative.

C-16 9/30/99 The Full Story... C has about 50 operators & 18 precedence levels… A "Precedence Table" shows all the operators, their precedence and associativity. –Look on inside front cover of our textbook –Look in any C reference manual When in doubt: check the table When faced with an unknown operator: check the table

C-17 9/30/99 Precedence and Associativity: Example Mathematical formula: ________ - b +  b a c a C formula: (- b + sqrt ( b * b * a * c) ) / ( 2.0 * a )

C-18 9/30/99 Expressions & Values b*b-4.0*a*c ** *

C-19 9/30/99 What is 2 * 3.14 ? Compiler will implicitly (automatically) convert int to double when they occur together: int + double double + double (likewise -, *, /) 2*3 * 3.14(2*3) * * * /3 * 3.14(2/3) * * * We strongly recommend you avoid mixed types: e.g., use 2.0 / 3.0 * 3.14 instead. Mixed Type Expressions

C-20 9/30/99 Conversions in Assignments int total, count ; double avg; total = 97 ; count = 10 ; avg = total / count ; /*avg is 9.0*/ total = avg; /*BAD*/ implicit conversion to double

C-21 9/30/99 Explicit Conversions (Section 7.1)  To be explicit in the program, you can use a cast  convert the result of an expression to a different type.  Format: (type) expression  Examples: (double) myage (int) (balance + deposit)  This does not change the rules for evaluating the expression (types, etc.)

C-22 9/30/99 Using Casts int total, count ; double avg; total = 97 ; count = 10 ; avg = total / count ;/*avg is 9.0*/ avg = (double) total / (double) count ; /*avg is 9.7*/ avg = (double) (total / count) ;/*avg is 9.0*/ explicit conversion to double implicit conversion to double

C-23 9/30/99 C is "Strongly Typed"  Every value has a type  C cares a lot about what the type of each thing is  Lots of cases where types have to match up  Start now: be constantly aware of the type of everything in your programs!

C-24 9/30/99 Basic Lessons  Write in the clearest way possible for the reader.  Keep it simple; for very complex expressions, break them up into multiple statements.  Use parentheses to indicate your desired precedence for operators where it may be ambiguous.  Use explicit casts to avoid implicit conversions in mixed mode expressions and assignments.  Be aware of types.