Page 1 of 1
					
				[HD Pg 164, Sec. 7.4.3 - the-updation-section]
				Posted: Wed Jul 21, 2021 5:26 pm
				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?
			 
			
					
				Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]
				Posted: Wed Jul 21, 2021 5:35 pm
				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 
			 
			
					
				Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]
				Posted: Wed Jul 21, 2021 5:45 pm
				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++);
    }
}
			 
			
					
				Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]
				Posted: Wed Jul 21, 2021 6:01 pm
				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   
 
    }
    System.out.println("j =" +j++) ; //prints 1
        
    }
}
 
			 
			
					
				Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]
				Posted: Thu Jul 22, 2021 3:25 am
				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
			 
			
					
				Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]
				Posted: Thu Jul 22, 2021 5:55 am
				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.
			 
			
					
				Re: [HD Pg 164, Sec. 7.4.3 - the-updation-section]
				Posted: Tue Jul 27, 2021 2:14 pm
				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.