Expressions.

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

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
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.
Expressions, Data Conversion, and Input
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
LESSON 6 – Arithmetic Operators
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
CPS120: Introduction to Computer Science Operations Lecture 9.
Mathematical Calculations in Java Mrs. G. Chapman.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Mathematical Calculations in Java Mrs. C. Furman.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
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.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Doing math In java.
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.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
Expressions and Assignment Statements
CompSci 230 S Programming Techniques
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Data Types, Variables & Arithmetic
Expressions.
Expressions and Assignment Statements
BIL 104E Introduction to Scientific and Engineering Computing
Relational Operations
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
Expressions and Assignment Statements
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Expressions and Assignment Statements
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
Arithmetic Operators in C
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
With Assignment Operator
Expressions and Assignment Statements
Chapter 2: Basic Elements of Java
Arithmetic Operators in C
Arithmetic Expressions & Data Conversions
C Operators, Operands, Expressions & Statements
Expressions and Assignment
elementary programming
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.
CS150 Introduction to Computer Science 1
Data Types and Expressions
Operator King Saud University
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Expressions

Topics Arithmetic expressions Conversions Operator precedence

Objectives At the completion of this topic, students should be able to: Correctly use all arithmetic operators in a Java program Correctly write arithmetic expressions in a Java program and be able to explain how expressions are evaluated Explain how and when data type conversions are done in Java Correctly use type casting in a Java program Understand how operator precedence affects the evaluation of an expression in a Java program, and know the precedence of arithmetic operators in Java Correctly use objects of the String class in a program

Arithmetic Expressions An expression is a combination one or more operands and operators that define some operation on data to be performed by the computer. sum = numOne + numTwo; area = PI * radius * radius; we usually put a space on either side of the operator to make the expression more readable.

Arithmetic Operators operator meaning example + plus c = a + b; - minus c = a – b; * times c = a * b; / divide by c = a / b;

Integer Division int varOne = 5; int varTwo = 7; double result = varTwo / varOne; the value of results will be 1.0. Why? When doing integer division, the result will always be an integer. Any fractional part is truncated, even when it is stored in a real variable.

Dividing By Zero From your math classes you will remember that any number divided by zero is infinity. Computers can’t divide by zero, because there is no way to represent infinity in binary. When doing integer division, if the divisor is zero, your program will throw an exception.

Remainder Operator The remainder operator is the % symbol We most often use it with integers It I sometimes called the modulus operator int a = 5; int b = 3; int c = a % b; the result is 2 … 5 divided by 3 leaves a remainder of 2. The sign of the result is the same as the sign of the numerator,

An example of using integer division and the remainder operator I have 57 quarters. I want to divide them equally among my four children. When I am done, how many will each child have and how many will be left over? int numQuarters = 57; int numChildren = 4; int eachChild = numQuarters / numChildren; int leftOver = numQuarters % numChildren; 57 / 4 = 14 57 % 4 = 1

An example of using integer division and the remainder operator I have 1267 pennies. How much is that in Dollars and cents? int pennies = 1267; int pPerD = 100; int dollars = pennies / pPerD; int cents = pennies % pPerD; 1267 / 100 = 12 1267 % 100 = 67

Arithmetic Assignment Instead of writing total = total + 3; we can use the arithmetic assignment operator total += 3; total -= 3; total = total -3; total *= 3; total = total * 3; total /= 3; total = total / 3; total %= 3; total = total % 3; the expression is the same as

Increment Operator Adding one to a variable is done so often in programs that a shortcut method has been provided in Java to write it. Instead of writing total = total + 1; we can write total++;

pre- and post-increment Using the increment operator is complicated by the fact that you can do a pre-increment or a post-increment. total++ post-increment ++total pre-increment What’s the difference? This is best illustrated by example.

Consider the following statements: int height = 5; int length = 4; int total = height * length++; total height length 20 5 4 5 The increment is done after the multiplication.

Consider the following statements: int height = 5; int length = 4; int total = height * ++length; total height length 25 5 4 5 The increment is done before the multiplication.

So, given that a = 4, b = 6, and c = 0, what are the values of a, b, and c after executing the following: c = ++a + b++; a = 5, b = 7, c = 11 (5 + 6)

Decrement Operator Instead of writing total = total – 1; we can write There is a pre and a post-decrement.

Mixed Data Types area = width * height; double int short an operator, like * has two operands. It is called a binary operator. the operands may not be of the same type. If they are not, Java tries to make sense of the operation by converting one operand so that it matches the other. It will always convert to a ‘higher’ data type if required. double int short

Example = * * int count = 7; double avgWeight = 155.5; double totalWeight = count * avgWeight; totalWeight avgWeight count = 1088.5 155.5 * 7 avgWeight 155.5 * 7.0 temporary double variable 1088.5 temporary double variable

Data Conversion Remember that all data in Java is typed Sometimes it is necessary to change data from one data type to another. There are two types of conversions: Widening Conversions the new type provides an equal or greater amount of storage. For example, converting an int to an double. Narrowing Conversions the new type uses less storage or loses information. For example, converting a double to an int.

Widening Conversions For the numerica data types we will use in this class, the only widening conversion to worry about is from int to double

Narrowing Conversions For the data types we will use in this class, the only narrowing conversion to worry about is from double to int In general a narrowing conversion loses information

Conversions Occur When a value of one type is assigned to a variable of a different type. When a value must be promoted to a different type in order for an operation to work correctly. When the programmer explicitly casts a value to a different type.

Assignment Conversion Assignment conversions only work if the conversion is a widening conversion! int dollars = 42; float money; money = dollars; float money = 42.50; int dollars; dollars = money; compiler error

Arithmetic Promotion double sum = 25.50; int count = 5; result = sum / count; In order for the computer to do this division, both numerator and denominator must be real numbers. So, count is first promoted to a double, then the division is performed.

Try This One int sum = 24; int count = 5; double base = 2.5; double result = (sum / count) + base; In this case note that the division takes place first. It is integer division, so the result of the division is 4 The we add 2.5. The value of 4 is promoted to a double. The result is 6.5

Type Casting Casting is used to explicitly convert from one data type to another. Casts can do both widening and narrowing conversions. int dollars; double money = 35.50; dollars = (int) money; the data type in parentheses tells the computer what data type money is to be converted to. In this case, the value of 35.50 will be converted to an integer. This results in the .50 being truncated. The resulting integer is then assigned to dollars.

Operator Precedence What is the result of the expression It depends upon whether we do the addition first or the division first! 14 + ( 8 / 2 ) = 14 + 4 = 18 (14 + 8 ) / 2 = 22 / 2 = 11

In Java multiplication, division, and the remainder operator have the same precedence. Addition and subtraction have the same precedence. Multiplication, division and remainder are always done before addition and subtraction. If two operators have the same precedence they are evaluated left to right. You can change the order of evaluation by using parentheses.