[HD Pg 164, Sec. 7.4.3 - the-updation-section]
Moderator: admin
-
- Posts: 60
- Joined: Thu Apr 15, 2021 12:21 pm
- Contact:
[HD Pg 164, Sec. 7.4.3 - the-updation-section]
Why is the post incremented value (i++) passed after the increment is done in the update section of a for loop, but not in the conditional part of an if-statement?
Last edited by enthunoob on Wed Jul 21, 2021 6:02 pm, edited 1 time in total.
-
- Posts: 60
- Joined: Thu Apr 15, 2021 12:21 pm
- Contact:
Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]
Because you must see the for-loop as a while loop, as explained in chapter 7.4.1
while(i>0){ System.out.println("i is "+i);
i--; //seperate statement
}
As also explained here:
https://stackoverflow.com/a/4706239/6544310
while(i>0){ System.out.println("i is "+i);
i--; //seperate statement
}
As also explained here:
https://stackoverflow.com/a/4706239/6544310
-
- Posts: 60
- Joined: Thu Apr 15, 2021 12:21 pm
- Contact:
Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]
When passed as an argument to a method (fe method(i++)), the pre-incremented value is passed as argument.
From JLS section 15.14.2:
"The value of the postfix increment expression is the value of the variable before the new value is stored."
Example (not an if-statement but as a method argument):
class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
// prints 4
System.out.println(i);
++i;
// prints 5
System.out.println(i);
// prints 6
System.out.println(++i);
// prints 6
System.out.println(i++);
}
}
From JLS section 15.14.2:
"The value of the postfix increment expression is the value of the variable before the new value is stored."
Example (not an if-statement but as a method argument):
class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
// prints 4
System.out.println(i);
++i;
// prints 5
System.out.println(i);
// prints 6
System.out.println(++i);
// prints 6
System.out.println(i++);
}
}
-
- Posts: 60
- Joined: Thu Apr 15, 2021 12:21 pm
- Contact:
Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]
public class MyClass {
public static void main(String args[]) {
boolean b = true;
int i =0, j =0;
for(;b;i++){
b = false;
}
System.out.println("i =" +i); //prints 1
if(0==j++) {
System.out.println("j =" +j) ; //actually also prints 1
}
System.out.println("j =" +j++) ; //prints 1
}
}
public static void main(String args[]) {
boolean b = true;
int i =0, j =0;
for(;b;i++){
b = false;
}
System.out.println("i =" +i); //prints 1
if(0==j++) {
System.out.println("j =" +j) ; //actually also prints 1
}
System.out.println("j =" +j++) ; //prints 1
}
}
-
- Posts: 60
- Joined: Thu Apr 15, 2021 12:21 pm
- Contact:
Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]
Another example on this with switch statement (from mock question enthuware.ocajp.i.v8.2.1239):
public class ForSwitch{
public static void main(String args[]){
char i;
LOOP: for (i=0;i<5;i++){
switch(i++){
case '0': System.out.println("A");
case 1: System.out.println("B"); break LOOP;
case 2: System.out.println("C"); break;
case 3: System.out.println("D"); break;
case 4: System.out.println("E");
case 'E' : System.out.println("F");
}
}
}
}
Prints CEF
public class ForSwitch{
public static void main(String args[]){
char i;
LOOP: for (i=0;i<5;i++){
switch(i++){
case '0': System.out.println("A");
case 1: System.out.println("B"); break LOOP;
case 2: System.out.println("C"); break;
case 3: System.out.println("D"); break;
case 4: System.out.println("E");
case 'E' : System.out.println("F");
}
}
}
}
Prints CEF
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]
Updation part is an expression. Rules of expression evaluation apply here. The expression uses the value of i before i++ is executed (that is how post increment operator is designed to work!)
If/else is not an expression. It is a statement. i++ is already executed by the time the control goes to if or else block.
If/else is not an expression. It is a statement. i++ is already executed by the time the control goes to if or else block.
If you like our products and services, please help us by posting your review here.
-
- Posts: 60
- Joined: Thu Apr 15, 2021 12:21 pm
- Contact:
Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]
Reading this in the fundamentals book I'm now wondering how it would work with post increments. x - > x++. Wouldn't that just return x, without incrementing the value?The syntax of the code part of a lambda expression is simple. It can either be an expression or a block of code contained within curly braces.
Update, after googling: it does. x -> x++ is the same as writing x -> x.
Who is online
Users browsing this forum: No registered users and 13 guests