Counting by Threes

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:

description start at 1, count upward by 2's, quite when count exceeds 10
sequence 1 3 5 7 9
code for ( count= 1 ; count < 10; count+=2 )

Counting by Threes

Here is almost the same loop as in the previous example, but now the control variable is incremented by THREE.

int count;
for ( count = 0; count <= 12; count += 3 )  
{
  System.out.println( "count is: " + count ); 
}
System.out.println( "/nDone with the loop./nCount is now" + count);

Here is a JavaScript version of this loop. Try to predict the output before you run the program. (Notice that the test is now <= ).



QUESTION 8:

Now show the for statement for each of the following sequences

start at 0, count upward by 1's, end at 7 for ( count = ___; count < ___; count++ )
start at 0, count upward by 2's, end at 14 for ( count = ___; count < ___; count += 2 ) start at 1, count upward by 2's, end at 15 for ( count = ___; count < ___; count += 2 )