Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
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.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
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.
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 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
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.
CSE 220 – C Programming Expressions.
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Expressions.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Building Java Programs
Relational Operations
Chapter 7: Expressions and Assignment Statements
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.
Arithmetic operations & assignment statement
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Conversions of the type of the value of an expression
Structure of a C Program
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
Arithmetic Operators in C
Arithmetic Expressions in C
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
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.
Arithmetic Operators in C
Building Java Programs
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Building Java Programs Chapter 2
Expressions and Assignment
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
Chapter 3 Operators and Expressions
Data Types and Expressions
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Primitive Types and Expressions
Building Java Programs
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Building Java Programs Chapter 2
Building Java Programs
Operator King Saud University
Building Java Programs
Building Java Programs
is multiplied by a variable of type to yield a result of type
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Data Types and Expressions
Building Java Programs
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement: double area, radius; area = 3.14*radius*radius; operator expression assignment statement Expression: anything that has a value e.g. radius, 3.14 or a combination e.g. 3.14*radius*radius The value of an expression depends on _ the data: operands radius, 3.14... _ the operators: *, + ... 142 C-1

Processing an assignment statement Consider: area = 3.14*radius*radius; 1 Evaluate the right hand side (=expression) 3.14*radius*radius 2 The value is stored in the left hand side, the assignment variable area depends on: types in the right hand side operators in the right hand side type of the left hand side Note: can have year = year + 1; (NOT a mathematical equation!) 142 C-2

Expressions with doubles Reminder: double = type for a floating point variable e.g. 2.15, -1.28e+28, NOT 3 or -4 Operators on doubles unary: - acts on one piece of data -23.4 -temperature binary: +, -, /, * acts on two pieces of data 1.609 * miles add subtract divide multiply All operators in C are unary or binary If so, what happens in 3.14*radius*radius ? 142 C-3

Expressions with ints Reminder: int = type for an integer variable e.g. 3, -216, NOT 3.0 Operators on ints unary: - (same as for doubles) binary: +, -, *, add subtract multiply as usual integer division modulus /, % BUT / is the integer division 2/3 is 0, 3/2 is 1 % is the modulus operator: a%b is the remainder of the division of a by b 114%50 is 2%3 is 14 2 142 C-4

Examples convert inches to feet and inches e.g. 74’’ = 6’ 2’’ int total_inches, feet, inches; total_inches = 74; feet = total_inches/12; inches = total_inches%12; Beware: double half_distance, distance; half_distance = 1/2 * distance; always 0! Why using ints and not always doubles? int sometimes makes more sense int number_of_children; computations with ints are faster doubles may be inaccurate: mathematically 2 . 3 . = 2 not 1.9999 3 1 142 C-5

Operator Precedence What is a + b*c ? Is it (a+b)*c or a+(b*c) ? precedence rules: 1. evaluate expressions in parentheses: start with the innermost set of parentheses 2. do unary - 3. do *, /, % 4. do binary +, - a + b*c is a + (b*c) When coding, do not hesitate to use parentheses avoid errors and clearer! 142C-6

Associativity What is a*b/c? Is it a*(b/c) or (a*b)/c ? C binary arithmetic operators associate left to right within the same precedence level a*b/c is (a*b)/c (but, unary - is right associative: write -2 not 2-) Example Add parentheses to the following expression to show how it is evaluated: a + b - c + d Ans: ((a+b) - c)+d There are also C operators that associate right to left e.g. the assignment operator = (see table on the inside cover of the text) 142C-7

What happens in expressions with ints and doubles When adding an int and a double, the compiler automatically converts the int to a double int + double double + double (also with -, * and /) Beware: it can be tricky! 2*3*6.2 6*6.2 6.0*6.2 37.2 conversion occurs here NOT before Thus: 2/3*6.2 0*6.2 0.0*6.2 0.0 AVOID MIXED TYPES 142C-8

Conversions in assignments int total, count; double avg; total = 96; count = 10; avg = total/count; What is the value of avg? avg = total/count; implicit conversion of int to double avg is 9.0 DO NOT DO double TO int may not do what you expect! int val1; double val2; val2 = 4.0e+28; val1 = val2; /* BAD */ val1 is not 40….0 28 zeros 142C-9

Explicit conversions To perform a conversion explicitly, use a cast (type) expression e.g. (double) number_of_children declared as an int Example int total, count; double avg1, avg2, avg3, avg4; total = 96; count = 10; avg1 = total/count; avg2 = (double)total/(double)count; avg3 = (double)(total/count); avg4 = (double)total/count; printf(“%.1f, %.1f, %.1f, %.1f”, avg1, avg2, avg3, avg4); 9.0, 9.6, 9.0, 9.6 142C-10

A few words of advice know the type of the variables in your program C cares about types … so should you! There are lots of cases where types have to match up (e.g. in functions as we will see) Style counts !!! _ Be clear _ KIS: Keep It Simple (don’t write huge expressions, break them up) _ use parentheses and casts 142C-11