Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Constants and Data Types Constants Data Types Reading for this class: L&L,
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.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
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.
String Escape Sequences
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
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
LESSON 6 – Arithmetic Operators
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
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.
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.
© 2006 Pearson Education 1 Obj: to use assignment operators HW: Prelab Exercises 2 Quiz 3.1 – 3.4 in two class days  Do Now: 1.Read p.144 – 145 (3.4 “more.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Doing math In java.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
2-1 Character Strings A string of characters can be represented as a string literal by putting double quotes around the text: Examples: "This is a string.
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.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Expressions.
Expressions.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 2 Assignment and Interactive Input
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Creating Objects & String Class
Arithmetic Operator Operation Example + addition x + y
Escape Sequences What if we wanted to print the quote character?
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
With Assignment Operator
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Arithmetic Expressions & Data Conversions
Expressions and Assignment
Data Types and Expressions
Chap 7. Advanced Control Statements in Java
Assignment Operators Topics Increment and Decrement Operators
Instructor: Alexander Stoytchev
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Assignment Statements Operator Precedence

ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of a variable  The assignment operator is the = sign total = 55; The expression on the right is evaluated and the result is stored in the variable on the left The value that was in total is overwritten You can only assign a value to a variable that is consistent with the variable's declared type.

ICS111-Java Programming Blanca Polo 3 Constants  A constant is an identifier that is similar to a variable except that it holds the same value during its entire existence  As the name implies, it is constant, not variable  The compiler will issue an error if you try to change the value of a constant  In Java, we use the final modifier to declare a constant final int MIN_HEIGHT = 62;

ICS111-Java Programming Blanca Polo 4 Constants  Constants are useful for three important reasons  First, they give meaning to otherwise unclear literal values For example, MAX_LOAD means more than the literal 250  Second, they facilitate program maintenance If a constant is used in multiple places, its value need only be updated in one place  Third, they formally establish that a value should not change, avoiding inadvertent errors by other programmers

ICS111-Java Programming Blanca Polo 5 No Magic Numbers

ICS111-Java Programming Blanca Polo 6 Expressions  An expression is a combination of one or more operators and operands  Arithmetic expressions compute numeric results and make use of the arithmetic operators: If either or both operands used by an arithmetic operator are floating point, then the result is a floating point Addition Subtraction Multiplication Division Remainder +-*/%+-*/%

ICS111-Java Programming Blanca Polo 7 Division and Remainder If both operands to the division operator are integers, the result is an integer (the fractional part is discarded) The remainder operator (%) returns the remainder after dividing the second operand into the first 14 / 3 equals 8 / 12 equals % 3 equals 8 % 12 equals 2 8

ICS111-Java Programming Blanca Polo 8 Division with Double numbers If both operands to the division operator are doubles, the result is double number The remainder operator (%) should not be used with double numbers. In Java 1.5 it is allowed but it is unpredictable / 3.1 equals 8.6 / 12.7 equals

ICS111-Java Programming Blanca Polo 9 Mixing double and int numbers  We will go over this in our next class.

ICS111-Java Programming Blanca Polo 10 Operator Precedence  Operators can be combined into complex expressions result = total + count / max - offset;  Operators have a well-defined precedence which determines the order in which they are evaluated

ICS111-Java Programming Blanca Polo 11 Operator Precedence  Arithmetic operators with the same precedence are evaluated from left to right, but parentheses can be used to force the evaluation order

ICS111-Java Programming Blanca Polo 12 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 Then the result is stored in the variable on the left hand side answer = sum / 4 + MAX * lowest; 1432

ICS111-Java Programming Blanca Polo 13 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 Then the result is stored back into count (overwriting the original value) count = count + 1;

ICS111-Java Programming Blanca Polo 14 How does this work?  JAVA works the right side of the assignment first  It then stores the result in a temporary memory space  Now it assigns that value into the left side Into a temporary memory space Assigning the value to the left side count = count + 1;

ICS111-Java Programming Blanca Polo 15 Increment and Decrement  The increment and decrement operators use only one operand  The increment operator ( ++ ) adds one to its operand  The decrement operator ( -- ) subtracts one from its operand  The statement count++ is functionally equivalent to count = count + 1;

ICS111-Java Programming Blanca Polo 16 Increment and Decrement  The increment and decrement operators can be applied in postfix form: count++  or prefix form: ++count  When used alone it, the postfix is the form used

ICS111-Java Programming Blanca Polo 17 Increment and Decrement  When used as part of a larger expression, the two forms can have different effects so be careful. int total; int count = 2; total = count++; Postfix increments after the assignment: Assigns the value of count(2) to total making total =2 Increments count by making count = 3 At the end total is 2 and count is 3. total = count; count=count+1;

ICS111-Java Programming Blanca Polo 18 Increment and Decrement int total; int count = 2; Total = ++ count; Prefix increments before the assignment: Increments count by one making count = 3 Assigns count (3) to total making total =3 At the end both total and count are 3. count = count+1; count = total;

ICS111-Java Programming Blanca Polo 19 Assignment Operators  Often we perform an operation on a variable, and then store the result back into that variable  Java provides assignment operators to simplify that process  For example, the statement num += count; is equivalent to num = num + count;

ICS111-Java Programming Blanca Polo 20 Assignment Operators – shortcuts  There are many assignment operators in Java, including the following: Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y No need to use shortcuts just take the long way. It works!!!

ICS111-Java Programming Blanca Polo 21 Concatenating or Adding?  The behavior of some assignment operators depends on the types of the operands  If the operands to the += operator are strings, the assignment operator performs string concatenation  The behavior of an assignment operator ( += ) is always consistent with the behavior of the corresponding operator ( + )

ICS111-Java Programming Blanca Polo 22