Drawing a program graph: What line is visited after a conditional or loop is not called?

97 views Asked by At

Just wanted to check whether the way I am thinking is correct. Here is an example method:

1 public static boolean isCircle (int M, int m) {
2     boolean result = false;
3     if (M == m & M > 0 & m > 0) {
4         result = true;
5     }
6     return result;
7     }

If I was to draw a program graph of this method (each line being a node, their path being edges) with variables that do not satisfy the conditional e.g. M = 1, m = 0. Would the program go from line 3 -> line 6, or from line 3 -> line 5 (end of the conditional). I would think line 3 to line 6 but am unsure.

2

There are 2 answers

0
IceArdor On

It depends. Most debuggers in IDE's put the execution marker at the beginning of the line that it's about to execute. After executing the conditional in line 3 which evaluates to false, the next meaningful line to execute is line 6.

However, consider this code:

1 public static boolean isCircle (int M, int m) {
2     boolean result = false;
3     if (M == m & M > 0 & m > 0) {
4         result = true;
5     } else printf("false!");
6     return result;
7 }

If execution jumped to 6, that would imply that the printf was executed as part of the conditional, which would be frustrating for debugging.

You can verify this behavior with the debugger in any IDE. You may find one or two IDE's that put the execution at the beginning of the next statement (line 6), but in the case where there is something else to execute on line 5 besides the }, I'd hope it'd pause the execution before jumping over that line.

Any debugger worth its salt will ignore lines that don't have any meaning (whitespace, comments, open/close brackets), but pause at the beginning of each meaningful line so you can evaluate variables, set new breakpoints, etc.

0
Subhi Dweik On

It would jump to 6 since that is the next instruction.

the closing } isn't literally part of the program, but closes a block, so it doesn't do anything on it's own.

See this post for the bytecode that might make it clear. http://blog.jamesdbloom.com/JavaCodeToByteCode_PartOne.html#if_else

As you can see there, the closing bracket doesn't get translated, so it doesn't exist. It signals end of block, but isn't part of execution.