When a total is wrong, inspect the current score and the running total at the addition that combines them. The Autos window can show values related to the current and nearby statements while you step.
Place both methods below inside the existing Game class. Its sample scores are 2, 0, and 3 for rounds 1 through 3.
Start debugging through an existing application action that calls this method. Open Debug > Windows > Autos while paused.
public int GetRoundGoals(int round)
{
if (round == 1)
{
return 2;
}
if (round == 3)
{
return 3;
}
return 0;
}
public int GetTrainingGoalsWithInspection()
{
int totalGoals = 0;
for (int round = 1; round <= 3; round++)
{
int roundGoals = this.GetRoundGoals(round);
totalGoals = totalGoals + roundGoals;
}
return totalGoals;
}| Round | roundGoals before addition |
totalGoals before addition |
totalGoals after F10 |
|---|---|---|---|
| 1 | 2 | 0 | 2 |
| 2 | 0 | 2 | 2 |
| 3 | 3 | 2 | 5 |
Pause before the addition executes
Set a breakpoint on totalGoals = totalGoals + roundGoals;. When it is reached, the preceding call has already supplied the current score.
On the first pause, roundGoals is 2 and totalGoals is still 0. The highlighted assignment is next to execute; its new total is not yet stored.
Step once to see the new total
Press F10 to step over the addition. The stored total becomes 2. Compare that value with the predicted calculation 0 + 2.
On round 2, adding zero leaves the total at 2. On round 3, adding 3 changes it to 5.
Autos can change which entries it shows
Autos selects values associated with the nearby code. At the loop header, its entries may differ from those at the addition.
An absent entry does not mean the program erased that value. Inspect the local in Locals or add it to Watch when you need a persistent view.
Respect the scope of the current score
roundGoals is defined inside the body and is assigned again on each pass. After leaving that block, the local is out of scope.
totalGoals is defined in the method block outside the loop, so it is still in scope at the return statement.
Describe the pause point as part of the evidence
An observation such as totalGoals is 2 is incomplete without saying where execution is paused. Before the second addition it is the old total; after that addition it is the new total, which happens to have the same value because the score is zero.
Record the round, current score, total, and whether the addition has executed. Those details make a debugger observation comparable with the worked trace.
The breakpoint is on the addition action, immediately after the score-reading action in the true route. Before stepping over the addition, compare the old total and current score.
After that action, the route advances the round and returns to the condition. The false route reaches the return, where the complete total is 5 and the body local is no longer in scope.
Your turn
On round 2, the total is 2 before the addition and 2 after it. Does that show that the assignment was skipped? Explain how the current score helps you decide.
Check your reasoning
No. The assignment executes and adds the current score, which is 0. The calculation 2 + 0 stores 2 again.
An unchanged value alone does not prove a statement was skipped. Check the paused line, the input score, and the step that executed.