Originally posted by nachiket deshpande:
i am not able to get the following ques.
public class test{
public static void main(String args[]){
int m=2;
int p=1;
int t=0;
for(;p<5;p++){<br /> if(T++>m){
m=p+t;
}
}
System.out.println("t="+t);
}
}
how t=4?
i thought t=3,because in the first iteration of for loop
t=1 and if(test) fails so next iteration takes place.
if()increments t to 2.and again t++<m<br /> third iteration makes t=3<br /> and inner if(){} exits.so final value of t should be 3 not 4.please help.
<br /> Hi,<br /> If you take a look at the if (t++ > m) statement, post-increment operator is used => hence the first value of t using in the test is 0 not 1. So here goes the iteration:
1. m = 2, p = 1, t = 0 (test value), t = 1 (post-increment value)
2. m = 2, p = 2, t = 1 (test value), t = 2 (p-i value)
3. m = 2, p = 3, t = 2 (test value), t = 3 (p-i value)
4. m = 2, p = 4, t = 3 (test value), t = 4 (p-i value), m = 4 + 2 = 6
5. m = 6, p = 5 => out of the loop => final value of t is 4.
Hope that answers your question.
Regards,
Lam