What we want to do is execute one line of code at a time, to see how each line of code affects the program's output and the state of its variables. This process is called stepping. There are two types of stepping: step over and step into. We will have to use both kinds of stepping to solve our problems, and each kind will be discused in turn.
Step over executes one line of code in its
entirety. For example, if the line contains a statement such as a method call,
the method is run entirely in the background and execution continues within
the calling block of code. This is the kind of stepping that we want to do on
line 22 of our program (we are not interested in the way that
works). So, in the Gild Debug View, press the
"Step Over" button. Line 22 will execute, and the program should stop
on the next executable line (line 24). Notice that some output in the Console
View at the bottom of your screen has changed. It now displays:
This is the output that we expect: the first power of two that we should print (20) is 1.
The documentation of
getNextPower(int) tells us this:
getNextPower(int) is working correctly, given that the
current value of
powerOfTwo is 1,
should assign 2 to
powerOfTwo. Let's step over line 24
to see what is assigned to
powerOfTwo. Press the
"Step Over" button.
Two things of note will happen: first, the debugger will execute line 24, and stop on the next executable line (line 26); second, the information in the Gild Debug View will change. Let's look at that information:
Notice how the colour of
powerOfTwo's label has changed to red.
This is to signal to us that the value of
powerOfTwo has changed.
getNextPower(int) worked as we should expect, then
value should now be 2. But it is not 2, it is 3. Something is wrong with
We are going to want to inspect what
getNextPower(int) is doing.
To see what
getNextPower(int) is doing, use the
"Step Over" button three times so that execution is stopped on line
24 (the one that contains the call to
time, we do not want to step over the line. We want to see what is happening
getNextPower(int). That is what
step into is for.
Step into will execute one line of code, but it will stop execution within
whatever calls that line makes (for example, a method call). We want execution
to stop within
getNextPower(int). Click on the
"Step Into" button of the Gild Debug View.
Two things will happen: first, execution will stop on line
40 of our program (the first line
of getNextPower(int)); second,
the data in the Gild Debug View will change. The data will change to this:
args and i are no longer included, and that
is new to the data. This is because the former two were local variables
main(String) method of our program. We are no longer in
main(String) method, so those two variables are no longer
currentPower, however, is local to
so it appears in the Gild Debug View.
Now, let's inspect the code of
getNextPower(int). It only
has one line, and it is as follows:
The logic error is obvious: if we want to find powers of two, the arithmetic operation we need to use is multiplication; but we are using addition. We are going to have to change this line of code. But first, we must terminate our program.