Trace continue and break in one loop

The method below scans the whole numbers from 1 to limit, skipping some and possibly stopping early.


Java
public static int process(int limit) {
    int score = 0;
    for (int i = 1; i <= limit; i++) {
        if (i % 3 == 0) {
            continue;
        } else if (score > 10) {
            break;
        }
        score += i;
    }
    return score;
}


Trace the call process(10). For every value of i from 1 to 10, write the action as one word: add (the statement score += i; runs), continue, break, or skipped (the loop body never starts with that value of i). Then give the value score holds after that row: at the end of the pass, at the moment the loop is left, or unchanged when the row is skipped. The first row is filled in as an example. In the last row, give the value the call returns.

iActionscore after this row
1add1
2
3
4
5
6
7
8
9
10
after the loopreturned value

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Methods, Conditionals, and Loops in Java Questions: