A Sequence that Counts

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

The program will print out:

value is: 5
value is: 15

A Sequence that Counts

Look at this program fragment:

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 fragement runs, it writes:

0
1

QUESTION 17:

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