• Calculate statement execution counts and informal run-time comparison of iterative statements

Assignment


This section is all about tracing loops. If you are writing your own code, this is straightforward. Here’s the book’s first example, but with some print statements to show off what’s going on at the end of each loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int var1 = 3;
int var2 = 2;

int i = 0;
System.out.println("i\tvar1\tvar2");
System.out.println("----------------");

while ((var2 != 0) && ((var1 / var2) >= 0)) {
    var1 = var1 + 1;
    var2 = var2 - 1;

    System.out.println(i + "\t" + var1 + "\t" + var2);
    i++;
}

The output will look like this.

1
2
3
4
i       var1    var2
----------------
1       4       1
2       5       0

Naturally, you won’t have the opportunity to modify the code of a multiple-choice question, so you’ll have to do those manually. Some things to watch for when trying to determine how many times a loop runs.

  1. Watch for < versus <= in the condition to avoid off-by-one errors.
  2. Verify the update portion. Don’t assume it goes up by one each time.
  3. Look for any code in the loop that might be messing with the counter.

If the loops don’t involve counters, all the work is likely done inside the loop. Use paper and write out the values after each iteration to make sure you understand what it’s doing.