You are on page 1of 16

Plant Simulation

For loop variables bug


Code

Bug is here

Table
“Should be” values of i and j loop variable Output of the code for i and j loop variable
Debug - step 1

i=2
Next step should be:
i=2
j=1
Debug - step 2

i should be 2, In the table, the values are


instead: copied wrong. But they are
i=1 correct as if there’s no the
j=2 variables switch.
Debug - step 3

i is incremented instead of j
Debug - step 4

i returns to its correct value


Debug - step 5

Again

i should be 3, In the table, the values are


instead: copied wrong. But they are
i=1 correct as if there’s no the
j=3 varaibles switch.
Debug - step 6

Again

In the table, the values are


i is
copied wrong. But they are
incremented
correct as if there’s no the
instead of j
variables switch.
Debug - step 7

Again

In the table, the values are


i is
copied wrong. But they are
incremented
correct as if there’s no the
instead of j
variables switch.
Code with exitloop

Bug is here
Debug with exitloop - step 1

i should be 2, But the condition i = 2 is


instead: satisfied (see next page)
i=1
j=2
Debug with exitloop - step 2

i should be 2, Condition satisfied


instead:
i=1
j=2
Debug with exitloop - step 3

i should be 3, instead it has been dumped as if


it was j outside the loop (see next page)
Debug with exitloop - step 4

i should be 3, instead it has been dumped as if


it was j outside the loop (see next page)
Debug with exitloop - step 5

i should be 3, instead it has been dumped as if


it was j outside the loop
Debug with exitloop - step n

Error if you don’t interrupt the code with the


messageBox. j still exists but not i.

You might also like