CSC300 / CSC402: Loop [2/6] 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 values of x (line 14) 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