Top Banner
The assignment expressions
21

The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

Jan 18, 2016

Download

Documents

Dwayne Atkinson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

The assignment expressions

Page 2: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

The assignment operator in an assignment statement

• We have seen the assignment statement:

• Effect:

var = expr;

• Stores the value of expression expr in the variable named var

Page 3: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

The assignment expression

• Consider the following arithmetic expression:

int a = 3; int b = 4;

Arithmetic expression: a + b Returns the result: 7

Page 4: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

The assignment expression (cont.)

• Definition: assignment expression:

Result returned by an assignment expression:

var = expr

Result of var = expr is equal to the value of expr

Page 5: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

The assignment expression (cont.)

• Note:

• The assignment operator will (still) update the value stored inside the variable var

• In addition to updating the variable. the assignment operator also returns a value

Page 6: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

The assignment expression (cont.)

• Example:

public class AssignExpr01 { public static void main(String[] args) { int a; a = 0; System.out.println(a); System.out.println(a = 2); // Prints 2 System.out.println(a); // Prints 2 } }

Page 7: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

The assignment expression (cont.)

• Explanation:

• System.out.println(a = 2) will print the value between its brackets

• The expression between the brackets is:

which is an assignment expression

• The assignment expression returns the value 2

Therefore, System.out.println will print the value 2

a = 2

Page 8: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

The assignment expression (cont.)

• Example Program: (Demo above code) – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/AssignExpr01.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac AssignExpr01.java

• To run:          java AssignExpr01

Page 9: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

The result returned by the assignment operators

• We have seen many different assignment operators:

• =

• +=         

• -=

• *=

• /=

• %=

Page 10: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

The result returned by the assignment operators (cont.)

• Result returned by the assignment operators:

Operator  Example   Returned value 

= a = 2 2

+= a += 2 a + 2

-=  a -= 2 a - 2

*= a *= 2 a * 2

/=  a /= 2 a / 2

%= a %= 2 a % 2

Page 11: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

The result returned by the assignment operators (cont.)

• Example: public class AssignExpr02 { public static void main(String[] args) { int a; a = 5; System.out.println(a = 2); a = 5; System.out.println(a += 2); a = 5; System.out.println(a -= 2); a = 5; System.out.println(a *= 2); a = 5; System.out.println(a /= 2); a = 5; System.out.println(a %= 2); } }

Page 12: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

The result returned by the assignment operators (cont.)

• Output:

2 7 3 10 2 1

Page 13: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

The result returned by the assignment operators (cont.)

• Example Program: (Demo above code) – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/AssignExpr02.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac AssignExpr02.java

• To run:          java AssignExpr02

Page 14: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

Priority and associativity of the assignment operators

• Priority of the assignment operators:

• All of the assignment operators have the lowest priority among all operators in Java

Page 15: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

Priority and associativity of the assignment operators

• Associativity of the assignment operators:

• All of the assignment operators are right associative

• Meaning:

• When there are multiple assignment operators in an expression, the expression is evaluate from right to left

Page 16: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

Priority and associativity of the assignment operators (cont.)

• Example: cascaded assignment

public class AssignExpr03 { public static void main(String[] args) { int a, b, c; c = b = a = 4 + 2; // Cascade assignment System.out.println(a); // Prints 6 System.out.println(b); // Prints 6 System.out.println(c); // Prints 6 } }

Page 17: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

Priority and associativity of the assignment operators (cont.)

• Explanation: c = b = a = 4 + 2; is evaluated as follows:

c = b = a = 4 + 2; higher priority ^^^^^ Reduces to:

c = b = a = 6; from right to left ^^^^^ assigns 6 to variable a and returns 6

Reduces to:

c = b = 6; from right to left ^^^^^ assigns 6 to variable b and returns 6

Reduces to:

c = 6; assigns 6 to variable c (and returns 6) ^^^^^

Page 18: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

Priority and associativity of the assignment operators (cont.)

• Example Program: (Demo above code) – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/AssignExpr03.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac AssignExpr03.java

• To run:          java AssignExpr03

Page 19: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

Exercise

• What will the following Java program print:

public class AssignExpr04 { public static void main(String[] args) { int a, b, c; a = 1; b = 1; c = 2; c *= b -= a += 1 + 2; System.out.println(a); System.out.println(b); System.out.println(c); } }

Page 20: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

Exercise (cont.)

• Answer:

4

-3

-6

Page 21: The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

Priority and associativity of the assignment operators (cont.)

• Example Program: (Verify for yourself...)             – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/AssignExpr04.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac AssignExpr04.java

• To run:          java AssignExpr04