CSC300: Loop [3/11] Previous pageContentsNext page

11
12
13
14
15
16
17
18
19
  public int numFives () {
    int result = 0;
    Node x = first;
    while (x != null) {
      if (x.item == 5) result = result + 1;
      x = x.next;
    }
    return result;
  }

For [5,11,5,5], the loop values (line 4) are

x==[5,11,5,5], result==0
  x==[11,5,5], result==1
     x==[5,5], result==1
       x==[5], result==2
        x==[], result==3 (x==null)

There is an image to the right of the code.
Click to animate in single-slide mode.
To enter single-slide mode, click the slide number in the title bar.

Previous pageContentsNext page