Presentation is loading. Please wait.

Presentation is loading. Please wait.

The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.

Similar presentations


Presentation on theme: "The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value."— Presentation transcript:

1 The ++ and -- expressions

2 The ++ and -- operators You guessed it: The ++ and -- are operators that return a value

3 The ++ and – operators (cont.) Pre- and post operators: There are two versions of the ++ and the -- operators: Pre-operation: the ++ and -- operator appear before the variable ++var pre-increment operator --var pre-decrement operator

4 The ++ and – operators (cont.) Pre-operation: the ++ and -- operator appear before the variable var++ post-increment operator var-- post-decrement operator

5 The ++ and – operators (cont.) Difference between a ++var and var++: The above also apply for --var and var--. (Except that that instead of incrementing the variable var by 1, the -- operators will decrement by 1) Both operations (++var and var++) will increment the variable var by 1 (so no different here) The only difference between ++var and var++ is: the value that is returned by the expression.

6 The result returned by the assignment operators Result returned by the ++ and -- operators: Value in aOperation Value in a after operation Returned value 4 ++a55 4 a++54 4 --a33 4 a--34

7 The result returned by the assignment operators (cont.) In other words: The pre-operations ++a and --a will: Apply the increment/decrement operation before (pre) returning the value in the variable a

8 The result returned by the assignment operators (cont.) The post-operations a++ and a-- will: Apply the increment/decrement operation after (pre) returning the value in the variable a (The computer will saved the original value of the variable a, perform the operation and then returned the saved value)

9 The result returned by the assignment operators (cont.) Example: public class Increment01 { public static void main(String[] args) { int a; a = 4; System.out.println(++a); // Prints 5 System.out.println(a); // Prints 5 a = 4; System.out.println(a++); // Prints 4 System.out.println(a); // Prints 5 a = 4; System.out.println(--a); // Prints 3 System.out.println(a); // Prints 3 a = 4; System.out.println(a--); // Prints 4 System.out.println(a); // Prints 3 }

10 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/I ncrement01.java How to run the program: Right click on link and save in a scratch directory To compile: javac Increment01.java To run: java Increment01

11 Priority of the ++ and -- operators Priority of the ++ and -- operators: The ++ and -- operators have higher priority than any arithmetic operators in Java

12 Priority of the ++ and – operators (cont.) Example: public class AssignExpr05 { public static void main(String[] args) { int a, b; a = 4; b = ++a + 1; // ++a evaluates to 5, so: 5 + 1 = 6 System.out.println(a); // Prints 5 System.out.println(b); // Prints 6 a = 4; b = a++ + 1; // a++ evaluates to 4, so: 4 + 1 = 5 System.out.println(a); // Prints 5 System.out.println(b); // Prints 5 }

13 Priority of the ++ and – operators (cont.) Explanation: b = ++a + 1; is evaluated as follows: b = ++a + 1; higher priority ^^^ ++a evaluates to 5 Reduces to: b = 5 + 1; = 6; b = a++ + 1; is evaluated as follows: b = a++ + 1; higher priority ^^^ ++a evaluates to 4 Reduces to: b = 4 + 1; = 5;

14 Priority of the ++ and – operators (cont.) Example Program: (Try it out yourself) –Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/ AssignExpr05.java How to run the program: Right click on link and save in a scratch directory To compile: javac AssignExpr05.java To run: java AssignExpr05

15 Exercise What will the following Java program print: public class AssignExpr06 { public static void main(String[] args) { int a, b; a = 4; b = 2 * --a + 1; System.out.println(a); System.out.println(b); a = 4; b = 2 * (a-- + 1); System.out.println(a); System.out.println(b); }

16 Exercise (cont.) Answer: 3 7 3 10

17 Exercise (cont.) Example Program: (Verify for yourself…) –Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/ AssignExpr06.java How to run the program: Right click on link and save in a scratch directory To compile: javac AssignExpr06.java To run: java AssignExpr06

18 Associativity of the ++ and -- operators The following expressions are syntactically incorrect: a ++ -- or ++ -- a Error: unexpected type required: variable found : value a ++ --; ^ Why it is syntactically incorrect: a ++ -- ===> (a ++) -- -- needs to operate on a variable ^^^^^^ is a value

19 Associativity of the ++ and – operators (cont.) Because we don't encounter these constructs, I will omit the discussion of the associativity rules of the ++ and -- operators. I can refer you to a website with the information: http://introcs.cs.princeton.edu/java/11precedence/


Download ppt "The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value."

Similar presentations


Ads by Google