Omitting the Test

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:

Yes. (Actually, a good answer would be "sounds dangerous", because it is.)

Omitting the Test

When the test part of a for is omitted it is as if the value true were put in its place. So,

for ( initialize ;   ; change )
  loopBody ;

is the same as:

for ( initialize ;  true  ; change )
  loopBody ;

This is done for compatibility with the language "C" and should not be done in newly written programs. In truth, it should never have been done in "C".

QUESTION 14:

Could all three parts be omitted from a for?