Sometimes Either Order Works

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:

x: 100  y: 100

Sometimes Either Order Works

When a variable is not part of a larger expression it makes no difference if you use the prefix operator or the postfix operator. For example:

count++ ;

++count ;

The following loop prints the values from 0 to 9, just as did the previous version (which used a postfix operator):

int counter = 0;

while ( counter < 10 )
{
  System.out.println("counter is now " + counter );
  ++counter ;
}

It is wise to use the ++ operator only with an isolated variable, as above. Using it in more complicated expressions is never necessary, but can lead to nasty bugs. Java allows this only because it is familiar to "C" programmers. The language "C" allows this because it was familiar to assembly language programmers of the PDP-8 computer, a common minicomputer of the 1970s.

Our 21st century programming language Java inherits some of its features from the architecture of an ancient computer!

QUESTION 7:

Is subtracting one from a variable a common operation?