Lecture 3 Expressions Richard Gesick.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
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.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
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.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
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
Operaciones y Variables
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CPS120: Introduction to Computer Science Operations Lecture 9.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Doing math In java.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
CSE 220 – C Programming Expressions.
Expressions.
Chapter 7: Expressions and Assignment Statements
Identifiers - symbolic names
Lecture 3 Java Operators.
Expressions.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
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.
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Identifiers - symbolic names
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Arithmetic Operator Operation Example + addition x + y
Increment and Decrement
Arithmetic Operators in C
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Numerical Data Types.
With Assignment Operator
A First Book of ANSI C Fourth Edition
Arithmetic Operators in C
Arithmetic Expressions & Data Conversions
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.
Data Types and Expressions
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Assignment Operators Topics Increment and Decrement Operators
Chapter 2 Primitive Data Types and Operations
Operator King Saud University
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
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Lecture 3 Expressions Richard Gesick

Topics Expressions Data Conversion

Expression Combination of one or more operators and operands Has both data type & value Operands may be literals constants variables

Arithmetic Operators Operator Operation + addition - subtraction * multiplication / division % modulus (remainder after division)

Integer Division & Modulus When dividing two integers: the quotient is an integer the remainder is truncated (discarded) To get the remainder, use the modulus operator with the same operands

Examples 8 25 / 3 25 % 3 3 / 25 3 % 25 25.0 / 5 10 / 3.0 1 3 5.0 3.33333

Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e a + b * c - d / e 1 2 3 4 3 1 4 2 a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1

Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated answer = sum / 4 + MAX * lowest; 4 1 3 2 Then the result is stored in the variable on the left hand side

Assignment Revisited First the expression on the right hand side of the = operator is evaluated Expression has data type and value variable = expression; Then the result is stored in the variable on the left hand side (has data type), if types are compatible

Assignment Revisited The right and left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count count = count + 1; Then the result is stored back into count (overwriting the original value)

Shortcut Operators ++ increment by 1 -- decrement by 1 Example: count++; // count = count + 1; count--; // count = count - 1;

Increment and Decrement The increment and decrement operators can be applied in postfix form: count++ or prefix form: ++count When used as part of a larger expression, the two forms can have different effects Because of their subtleties, the increment and decrement operators should be used with care

Which form to use? when the increment (or decrement) operator is used in a “stand alone” statement solely to add one (or subtract one) from a variable’s value, it can be used in either prefix or postfix form USE EITHER dogs-- ; --dogs;

BUT... when the increment (or decrement) operator is used in a statement with other operators, the prefix and postfix forms can yield different results LET’S SEE HOW. . .

PREFIX FORM “First increment, then use ” int alpha ; int num ; num = 13; alpha = ++num * 3; 13 num 14 alpha 42

POSTFIX FORM “Use, then increment ” int alpha ; int num ; num = 13; alpha = num++ * 3; 13 num alpha 13 39 num alpha 14 num

More Shortcut Operators Example Equivalent += a += 3; a = a + 3; -= a -= 10; a = a - 10; *= a *= 4; a = a * 4; /= a /= 7; a = a / 7; %= a %= 10; a = a % 10;

Assignment Operators is equivalent to The right hand side of an assignment operator can be a complex expression The entire right-hand expression is evaluated first, then the result is combined with the original variable Therefore result /= (total-MIN) % num; is equivalent to result = result / ((total-MIN) % num);

Common Error Trap No spaces are allowed between the arithmetic operator and the equals sign Note that the correct sequence is +=, not =+ Example: add 2 to a // incorrect a =+ 2; // a = +2; assigns 2 to 2 // correct a += 2; // a = a + 2;

Assigning the Values of Other Variables Syntax: dataType variable2 = variable1; Rules: 1. variable1 needs to be defined before this statement appears in the source code 2. variable1 and variable2 need to be compatible data types; in other words, the precision of variable1 must be lower than or equal to that of variable2.

Mixed-Type Arithmetic When performing calculations with operands of different data types: Lower-precision operands are promoted to higher-precision data types, then the operation is performed Promotion is effective only for expression evaluation; not a permanent change Called "implicit type casting" Bottom line: any expression involving a floating-point operand will have a floating-point result.

Two types of data conversion Widening conversion (promotion) safe, do not lose data goes to a “wider” (more bits) data type may lose precision (long to float) Narrowing conversion (demotion) may lose data may lose precision should be avoided compiler error unless specific cast done

Conversion Techniques assignment conversion assign an int to a long promotion divide an int by a double casting

Explicit Type Casting Syntax: (dataType)( expression ) Note: parentheses around expression are optional if expression consists of 1 variable Useful for calculating averages double result = (double) 25 / 3; double result = (double) total / count;

Summary What did you learn?