D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
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
1 Chapter 2 Basic Elements of Fortran Programming.
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.
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.
C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.
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.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
C-1 University of Washington Computer Programming I Lecture 3: Variables, Values, and Types © 2000 UW CSE.
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.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
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.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Expressions, Evaluation and Assignments
A First Book of ANSI C Fourth Edition
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Input, Output, and Processing
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.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
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.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
7.2 Arithmetic Expressions
CSE 220 – C Programming Expressions.
Expressions.
Expressions.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
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
Arithmetic Operator Operation Example + addition x + y
Arithmetic Expressions in C
A First Book of ANSI C Fourth Edition
College of Computer Science and Engineering
Arithmetic Expressions & Data Conversions
CSE 100 Data Types Declarations Displays.
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.
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE

D-2 Arithmetic expressions Integer and floating-point (double) types Unary and binary operators Precedence Associativity Conversions and casts Symbolic constants Overview

D-3 We need precise rules that define exactly what an expression means: What is the value of * 4 + 4? Arithmetic on a computer may differ from everyday arithmetic or math: (1.0 / 9.0) * 9.0 could be / 3 is zero in C, not.667 (!) Why Study Expressions?

D-4 double area, radius; area = 3.14 * radius * radius; assignment statement expression Execution of an assignment statement: Evaluate the expression on the right hand side Store the value of the expression into the variable named on the left hand side Assignment Statement: Review

D-5 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; Expressions

D-6 Expression Evaluation Some terminology: Data or operand means the integer or floating- point constants and/or variables in the expression. Operators are things like addition, multiplication, etc. The value of an expression will depend on the data types and values and on the operators used Additionally, the final result of an assignment statement will depend on the type of the assignment variable.

D-7 Arithmetic Types: Review C provides two different kinds of numeric values Integers (0, 12, -17, 142) Type int Values are exact Constants have no decimal point or exponent Floating-point numbers (3.14, e23) Type double Values are approximate (12-14 digits precision typical) Constants must have decimal point and/or exponent

D-8 Operator Jargon Binary: operates on two operands 3.0 * b zebra + giraffe Unary: operates on one operand C operators are unary or binary Puzzle: what about expressions like a+b+c? Answer: this is two binary ops, in sequence

D-9 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: +, -, *, / Note: no exponentiation operator in C Expressions with doubles

D-10 Declarations 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 Example Expressions with doubles

D-11 Constants of type int: 0, 1, -17, 42 not 0.0 or 1e3 Operators on ints: unary: - binary: +, -, *, /, % Expressions with ints

D-12 Integer operators include integer division and integer remainder: symbols / and % Caution: division looks like an old friend, but there is a new wrinkle! int Division and Remainder 2 rem ) rem )

D-13 / is integer division: no remainder, no rounding 299 / / / 6 0 int Division and Remainder

D-14 / is integer division: no remainder, no rounding 299 / / / 6 0 % is mod or remainder: 299 % % % 6 5 int Division and Remainder

D-15 Given:total_minutes359 Find:hours5 minutes59 Solution in C: Expressions with ints: Time Example

D-16 Given:total_minutes359 Find:hours5 minutes59 Solution in C: Expressions with ints: Time Example hours = total_minutes / 60 ; minutes = total_minutes % 60 ;

D-17 int radius; double volume; double pi = ;. volume = ( 4/3 ) * pi * radius *radius * radius; A Cautionary Example

D-18 Sometimes only ints make sense the 15 th spreadsheet cell, not the th cell Doubles may be inaccurate representing “ints” In mathematics 3 15 (1/3) = 15 But, 3.0 * 15.0 * (1.0 / 3.0) might be Last, and least operations with doubles is slower on some computers doubles often require more memory Why Use ints? Why Not doubles Always?

D-19 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: * Order of Evaluation

D-20 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: * Order of Evaluation

D-21 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 = Order of Evaluation

D-22 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 = Order of Evaluation 7

D-23 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 = Order of Evaluation 7 9

D-24 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 +, - Operator Precedence Rules

D-25 Precedence doesn’t help if all the operators have the same precedence Is a / b * c equal to a / ( b * c ) or ( a / b ) * c ?? Associativity determines the order among consecutive operators of equal precedence Does it matter? Try this: 15 / 4 * 2 Precedence Isn’t Enough

D-26 Associativity determines the order among consecutive operators of equal precedence Does it matter? Try this: 15 / 4 * 2 (15 / 4) * 2 = 3 * 2 = 6 15 / (4 * 2) = 15 / 8 = 1 Associativity Matters

D-27 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. Associativity Rules

D-28 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

D-29 Mathematical formula: ________ - b +  b a c a C formula: (- b + sqrt ( b * b * a * c) ) / ( 2.0 * a ) Precedence and Associativity: Example

D-30 b*b-4.0*a*c Depicting Expressions

D-31 b*b-4.0*a*c2.5 * 6.25 Depicting Expressions

D-32 b*b-4.0*a*c ** * Depicting Expressions

D-33 b*b-4.0*a*c ** * Depicting Expressions

D-34 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

D-35 int total, count, value; double avg; total = 97 ; count = 10; avg = total / count ; /*avg is 9.0!*/ value = total*2.2; /*bad news*/ Conversions in Assignments implicit conversion to int – drops fraction with no warning implicit conversion to double

D-36 Use a cast to explicitly 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 itself (types, etc.) Good style, because it shows the reader that the conversion was intentional, not an accident Explicit Conversions

D-37 int total, count ; double avg; total = 97 ; count = 10 ; /* explicit conversion to double (right way)*/ avg = (double) total / (double) count); /*avg is 9.7 */ Using Casts

D-38 int total, count ; double avg; total = 97 ; count = 10 ; /* explicit conversion to double (right way)*/ avg = (double) total / (double) count); /*avg is 9.7 */ /* explicit conversion to double (wrong way)*/ avg = (double) (total / count) ; /*avg is 9.0*/ Using Casts

D-39 Named constants: #define PI #define HEIGHT 50 #define WIDTH 80 #define AREA (HEIGHT * WIDTH)... circle_area = PI * radius * radius ; volume = length * AREA; #define - Symbolic Constants Note: = and ; are not used for #define () can be used in #define

D-40 Centralize changes No "magic numbers" (unexplained constants) use good names instead Avoid typing errors Avoid accidental assignments to constants double pi ;vs. pi = 3.14 ;#define PI pi = 17.2 ;PI = 17.2 ; syntax error Why #define?

D-41 Every variable, value, and expression in C has a type Types matter - they control how things behave (results of expressions, etc.) Lots of cases where types have to match up Start now: be constantly aware of the type of everything in your programs! Types are Important

D-42 Write in the clearest way possible to help the reader Keep it simple; break very complex expressions into multiple assignment statements Use parentheses to indicate your desired precedence for operators when it is not clear Use explicit casts to avoid (hidden) implicit conversions in mixed mode expressions and assignments Be aware of types Advice on Writing Expressions

D-43 We’ll discuss input and output See you then! Next Time