16. A Sequence that Counts


Answer:

The program will print out:

    value is: 5
    value is: 15

A Sequence that Counts

Look at this program fragment. Click inside the program to see how it works.

int count = 0; // statement 1
System.out.println( count ) ; // statement 2 count = count + 1; // statement 3
System.out.println( count ) ; // statement 4




    

Here is how the program works:

  1. Statement 1 puts 0 into count.
  2. Statement 2 writes out the 0 in count.
  3. Statement 3 first gets the 0 from count, adds 1 to it, and puts the result back in count.
  4. Statement 4 writes out the 1 that is now in count.

When the fragment runs, it writes:

0
1

Question 17:

Think of a way to write 0, 1, and 2.