[HD Pg 164, Sec. 7.4.3 - the-updation-section]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

[HD Pg 164, Sec. 7.4.3 - the-updation-section]

Post by enthunoob »

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.

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]

Post by enthunoob »

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

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]

Post by enthunoob »

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++);
}
}

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]

Post by enthunoob »

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 :oops:
}
System.out.println("j =" +j++) ; //prints 1

}
}

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]

Post by enthunoob »

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]

Post by admin »

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 you like our products and services, please help us by posting your review here.

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]

Post by enthunoob »

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.
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?

Update, after googling: it does. x -> x++ is the same as writing x -> x.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 32 guests