Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Similar documents
Operators. Lecture 3 COP 3014 Spring January 16, 2018

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Expressions and Casting

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Computer Programming CS F111

LECTURE 3 C++ Basics Part 2

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Lecture 3 Tao Wang 1

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

COMP Primitive and Class Types. Yi Hong May 14, 2015

Expression Evaluation and Control Flow. Outline

Section we will not cover section 2.11 feel free to read it on your own

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Lesson 3: Arithmetic & Casting. Pic 10A Ricardo Salazar

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

C-1. Overview. CSE 142 Computer Programming I. Review: Computer Organization. Review: Memory. Declaring Variables. Memory example

Chapter 4: Basic C Operators

Chapter 3. Numeric Types, Expressions, and Output

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Basic Operations jgrasp debugger Writing Programs & Checkstyle

C Programming

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

Full file at

Programming in C++ 6. Floating point data types

CIS133J. Working with Numbers in Java

Midterms Save the Dates!

Full file at

CSE / ENGR 142 Programming I

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Data Types and Variables in C language

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

Information Science 1

(2-1) Numeric Expressions in C H&K Chapter 2. Instructor - Andrew S. O Fallon CptS 121 (August 27, 2018) Washington State University

Information Science 1

3. Java - Language Constructs I

Operators and Expressions:

CS Programming I: Primitives and Expressions

Basics of Java Programming

Arithmetic Expressions in C

Fundamentals of Programming Session 7

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Programming for Engineers Iteration

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Visual C# Instructor s Manual Table of Contents

CHAPTER 3 Expressions, Functions, Output

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Midterms Save the Dates!

Midterms Save the Dates!

CS112 Lecture: Working with Numbers

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

Zheng-Liang Lu Java Programming 45 / 79

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Elementary Programming

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Informatics Ingeniería en Electrónica y Automática Industrial

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Computer System and programming in C

Chapter 3 Structure of a C Program

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Learning the Language - V

Chapter 3: Operators, Expressions and Type Conversion

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

C++ Programming: From Problem Analysis to Program Design, Third Edition

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Variables and Operators 2/20/01 Lecture #

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Declaration and Memory

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Python Numbers. Learning Outcomes 9/19/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

CS112 Lecture: Primitive Types, Operators, Strings

Introduction to Programming, Aug-Dec 2008

Operators. Java operators are classified into three categories:

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Lecture 2: Python Arithmetic

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Type Conversion. and. Statements

Selection Statements. Pseudocode

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

>>> * *(25**0.16) *10*(25**0.16)

Transcription:

CPE 101 mod/reusing slides from a UW course Overview (4) Lecture 4: Arithmetic Expressions Arithmetic expressions Integer and floating-point (double) types Unary and binary operators Precedence Associativity Conversions and casts Symbolic constants 2000 UW CSE D-1 D-2 Why Study Expressions? We need precise rules that define exactly what an expression means: What is the value of 4-4 * 4 + 4? Arithmetic on a computer may differ from everyday arithmetic or math: (1.0 / 9.0) * 9.0 could be 0.99999998213 2 / 3 is zero in C, not.667 (!) D-3 Assignment Statement: Review 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 D-4 D-1

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; D-5 *Expression Evaluation (start Fri 3 Oct) Some terminology: Data or operand means the integer or floatingpoint 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-6 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, -6.023e23) Type double Values are approximate (12-14 digits precision typical) Constants must have decimal point and/or Operator Jargon Binary: operates on two operands 3.0 * b zebra + giraffe Unary: operates on one operand -23.4 C operators are unary or binary Puzzle: what about expressions like a+b +c? Answer: this is two binary ops, in sequence exponent D-7 D-8 D-2

Expressions with doubles 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 D-9 Example Expressions with doubles 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 - 3.0 + c1 * x - c2 * x * x D-10 Expressions with ints int Division and Remainder Constants of type int: 0, 1, -17, 42 not 0.0 or 1e3 Operators on ints: unary: - binary: +, -, *, /, % D-11 Integer operators include integer division and integer remainder: symbols / and % Caution: division looks like an old friend, but there is a new wrinkle! 100 ) 299 2 rem 199 200 99 D-12 D-3

int Division and Remainder int Division and Remainder / is integer division: no remainder, no rounding 299 / 100 2 6 / 4 1 5 / 6 0 / is integer division: no remainder, no rounding 299 / 100 2 6 / 4 1 5 / 6 0 D-13 % is mod or remainder: 299 % 100 99 6 % 4 2 5 % 6 5 D-14 Expressions with ints: Time Example Expressions with ints: Time Example Given: total_minutes 359 Find: hours 5 minutes 59 Given: total_minutes 359 Find: hours 5 minutes 59 Solution in C: D-15 Solution in C: hours = total_minutes / 60 ; minutes = total_minutes % 60 ; D-16 D-4

A Cautionary Example int radius; double volume; double pi = 3.141596;.. volume = ( 4/3 ) * pi * radius *radius * radius; D-17 Why Use ints? Why Not doubles Always? Sometimes only ints make sense the 15 th spreadsheet cell, not the 14.997 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 14.9999997 Last, and least operations with doubles is slower on some computers doubles often require more memory D-18 Order of Evaluation 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 Order of Evaluation 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 D-19 D-20 D-5

Order of Evaluation Order of Evaluation 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) = 4 + (3 * 2) - 1 = 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) = 7 4 + (3 * 2) - 1 = D-22 Order of Evaluation Operator Precedence Rules 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) = 7 4 + (3 * 2) - 1 = 9 D-23 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 +, - D-24 D-6

Precedence Isn t Enough 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 Associativity Matters 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 Does it matter? Try this: 15 / 4 * 2 D-25 D-26 Associativity Rules 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. D-27 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: D-28 check the table D-7

Precedence and Associativity: Example Depicting Expressions b * b - 4.0 * a * c Mathematical formula: - b + b 2-4 a c ---------------------- 2 a C formula: (- b + sqrt ( b * b - 4.0 * a * c) ) / ( 2.0 * a ) D-29 D-30 Depicting Expressions b * b - 4.0 * a * c 2.5 2.5 * 6.25 Depicting Expressions b * b - 4.0 * a * c 2.5 2.5-1.0 15.2 * * 6.25-4.0 * -60.8 D-31 D-32 D-8

Depicting Expressions b * b - 4.0 * a * c 2.5 2.5-1.0 15.2 * * 6.25-4.0 * -60.8-67.05 D-33 Mixed Type Expressions 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.14 6 * 3.14 6.0 * 3.14 18.84 2/3 * 3.14 (2/3) * 3.14 0 * 3.14 0.0 * 3.14 0.0 We strongly recommend you avoid mixed types: e.g., use 2.0 / 3.0 * 3.14 instead. D-34 Conversions in Assignments Explicit Conversions implicit conversion int total, count, value; double avg; total = 97 ; count = 10; avg = total / count ; /*avg is 9.0!*/ value = total*2.2; /*bad news*/ implicit conversion to int drops fraction with no warning 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 to double D-35 D-36 that the conversion was intentional, not an accident D-9

Using Casts int total, count ; double avg; total = 97 ; count = 10 ; Using Casts int total, count ; double avg; total = 97 ; count = 10 ; /* explicit conversion to double (right way)*/ avg = (double) total / (double) count); /*avg is 9.7 */ D-37 /* 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*/ D-38 #define - Symbolic Constants Why #define? Named constants: #define PI 3.14159265 #define HEIGHT 50 #define WIDTH 80 #define AREA (HEIGHT * WIDTH)... circle_area = PI * radius * radius ; volume = length * AREA; Note: = and ; are not used for #define 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 3.14...... () can be used in #define D-39 D-40 pi = 17.2 ; PI = 17.2 ; syntax error D-10

Types are Important Every variable, value, and expression in C has a type Note that #define (symbolic constants) are simple text substitutions, their type is defined by the text Types matter - they control how things behave (results of expressions, etc.) Lots of cases where types have to match up D-41 Advice on Writing Expressions 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 D-42 Next Time We ll discuss input and output D-43 D-11