go to previous page   go to home page   hear noise   go to next page

Answer:

Some Machines that Use Cycles

Perhaps the ultimate example of the usefulness of cycles is the ultimate machine — the wheel.

The while statement

while loop

 

Here is a program with a loop:


// 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 flowchart shows how the program works. First, count is set to one. Then it is tested to see if it is less than or equal to three.

If the test returns true the statements in the block following the while are executed. The statement count = count + 1 increases the value stored in the variable count by adding one to it. Then execution goes back to the while statement and the process is repeated.

If the test returns false, execution goes to the "Done with loop" statment. Then the program ends.

Copy this program to a file and run it. Then hack upon it. See if changing a few things does what you expect.

QUESTION 2:

What does this statement do:

count = count + 1;