The ++ and -- expres..

Download Report

Transcript The ++ and -- expres..

The ++ and -- expressions
The ++ and -- operators
• You guessed it:
• The ++ and -- are operators that return a value
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
operator
pre-increment
--var
operator
pre-decrement
The ++ and – operators (cont.)
• Pre-operation: the ++ and -- operator appear before
the variable
var++
operator
post-increment
var-operator
post-decrement
The ++ and – operators (cont.)
• Difference between a ++var and var++:
• 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.
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)
The result returned by the assignment
operators
• Result returned by the ++ and -- operators:
Value in a
Operation
Value in a
after
operation
Returned
value
4
++a
5
5
4
a++
5
4
4
--a
3
3
4
a--
3
4
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
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)
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
}
}
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
Priority of the ++ and -- operators
• Priority of the ++ and -- operators:
• The ++ and -- operators have higher priority than any
arithmetic operators in Java
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
}
}
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;
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
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);
}
}
Exercise (cont.)
Answer:
3
7
3
10
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
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 ++) -^^^^^^
is a value
-- needs to operate on a variable
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/