Working of pre-fix increment in Java -
this question has answer here:
what control flow of post-increment operator?
public class postincrement { public static void main(string[] args) { int = 0; for(int i=0;i< 2 ;i++) { =a++; } for(int i=0 ;i< 1;i++) { a++; } system.out.println("result2 :"+" "+a); } } the results 0 , 1
why so?
the postfix operator ++ executes after statement completes.
the first time print value of a prints 0 because nothing has changed. next, enter loop executes once, incrementing value of a. doesn't matter postincrement because there no other instructions in statement. value of a 1.
you print out 1.
as aside, code doesn't work because inner variable i hiding outer variable , compiler error redeclaring i. assumed mean different variable here such j.
Comments
Post a Comment