| Summary: | do-while format corrupts local variables | ||
|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Steven Strazza <sstrazza> |
| Component: | Text | Assignee: | JDT-Text-Inbox <jdt-text-inbox> |
| Status: | CLOSED INVALID | QA Contact: | |
| Severity: | major | ||
| Priority: | P3 | CC: | jjohnstn, mistria |
| Version: | 4.7.1a | ||
| Target Milestone: | --- | ||
| Hardware: | PC | ||
| OS: | Windows 10 | ||
| Whiteboard: | stalebug | ||
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant. -- The automated Eclipse Genie. (In reply to Steven Strazza from comment #0) > int pa = 0, > k = 0, > a = 0, > b = 0, > ch = 0; > i = pa = 0; > while (i < 16) > { System.out.println("1p= "+pa); > for (k = 0 ; k < 8 ; k++) > { if (pa < 0) System.out.println("p= "+pa); > i += k; > // for (a = 0 ; a<4 ; a++) > // k += a; > do { k += pa; > } while (pa++ < 4); > } } > > The above code segment as run from public static void main(String[] args) > produces the following output: (value of pa varies if you move variables > around.) > 1p= 534431 > 1p= 534432 > 1p= 534433 > 1p= 534434 > 1p= 534435 (continues on until pa cycles through entire int range) > > changing the statement k += pa; to b = k + pa; generates the following > output: > 1p= 0 > > Return the statement b = k + pa; to k+= pa; commenting out the second > println produces: > > p= -2147483648 > p= -2147483648 > > The code segment: > i = pa = 0; > while (i < 16) > { System.out.println("1p= "+k); > for (k = 0 ; k < 8 ; k++) > { if (pa < 0) System.out.println("p= "+pa); > i += k; > for (a = 0 ; a < 4 ; a++) > k += a; > // do {k += pa; > // } while (pa++ < 4); > } } > > produces the following output: > > 1p= 0 > 1p= 14 > 1p= 14 The do/while vs for code is not the same. The do while loop always executes the block once. Then when it encounters the pa++ < 4, it will increment pa at least once even after pa is greater than equal to 4. So pa will continue to grow and grow because it is never reset. Once it is big enough, the for k loop will only execute the first time and that means i freezes because it just gets 0 added to it before the k loop ends. It actually gets to 6 which is < 16. This means the i loop never terminates and you keep incrementing pa. In the second case, i increments 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 so the k loop runs once and then you don't get past for i < 16. When you change do/while to a for loop, the value of a gets reset to 0 at the start each time so the loop always runs 4 times and ends up adding 6 to k whieh after first loop is 6, then the for loop increments to 7. Add another 6 to that and you get 13. The for loop tests but still increments k by one to get 14. At this point i is 7 so you print 14 and start again. You make k 14 again and i ends up 14. One more loop and i becomes 21. |
int pa = 0, k = 0, a = 0, b = 0, ch = 0; i = pa = 0; while (i < 16) { System.out.println("1p= "+pa); for (k = 0 ; k < 8 ; k++) { if (pa < 0) System.out.println("p= "+pa); i += k; // for (a = 0 ; a<4 ; a++) // k += a; do { k += pa; } while (pa++ < 4); } } The above code segment as run from public static void main(String[] args) produces the following output: (value of pa varies if you move variables around.) 1p= 534431 1p= 534432 1p= 534433 1p= 534434 1p= 534435 (continues on until pa cycles through entire int range) changing the statement k += pa; to b = k + pa; generates the following output: 1p= 0 Return the statement b = k + pa; to k+= pa; commenting out the second println produces: p= -2147483648 p= -2147483648 The code segment: i = pa = 0; while (i < 16) { System.out.println("1p= "+k); for (k = 0 ; k < 8 ; k++) { if (pa < 0) System.out.println("p= "+pa); i += k; for (a = 0 ; a < 4 ; a++) k += a; // do {k += pa; // } while (pa++ < 4); } } produces the following output: 1p= 0 1p= 14 1p= 14