Perhaps the ultimate example of the usefulness of cycles is the ultimate machine — the wheel.
while statementHere is a program with a loop:
import java.io.*;
// Example of a while loop
class loopExample
{
public static void main (String[] args )
{
int count = 1; // start count out at one
while ( count <= 3 ) // loop while count is <= 3
{
System.out.println( "count is:" + count );
count = count + 1; // add one to count
}
System.out.println( "Done with the loop" );
}
}
The statement count = count + 1 increases the
value stored in the variable count by adding one to it.
You would learn a lot if you imported this program into NotePad and ran it
in the DOS window.
Or, you can run a JavaScript version of the program by clicking the button:
If you look at the source for this page using your browser's "View Source" menu item, you will see the JavaScript that is responsible for the above. JavaScript is similar to Java, but has some big differences, so be careful not to get confused.