edit upload recent print
Search

                                          



Main » Loops

The 'While' Loop

Just as with conditionals (if / else) statements, a 'while' loop starts with a boolean (true/false) test that is evaluated to determine whether the code inside is run. If the test is 'true', the code inside is run, otherwise it is skipped. The difference with the "while" loop is that the instructions continue to be run until the test condition becomes 'false'.

  int counter = 0;

  while (counter  < 10) 
  {
    println("counter is less than 10.");
    counter = counter + 1; 
  }

You can see that the "while" loop above runs the code inside the brackets 10 times (with 'counter' starting at 0, and incrementing once per loop through 1,2,3, etc. until it reaches 10 and the test (counter < 10) evaluates to false and the loop exits. To accomplish this, three separate statements involving 'counter' are used:

  • int counter = 0;
  • while (counter < 10)
  • counter = counter + 1;

These can be combined into one statement (a "For" loop) as follows:

The 'For' Loop

  for (int counter=0; counter < 10; counter++) 
  {
     println("counter is less than 10.");
  }

This statement works exactly the same as the one above, printing "counter..." exactly 10 times. The same 3 lines as above are all present in the single line that begins with 'for', separated by semi-colons:

  for (part-a; part-b; part-c) 
  { 
    // some code 
  }

part-a is run once (when the 'for' loop is first reached); part-b is tested each time through the loop, and must be 'true' or the loop is skipped; and part-c is done at the completion of each time through the loop.

This version seems to be more common among programmers so its good to know. The most common form uses the variable 'i', perhaps for index, as follows:

  for (int i=0; i < aVariable; i++)
  { 
    // some code 
  }

We start with i=0, and test it against 'aVariable' before the first time through the loop. If its less (lets say aVariable=10), then we do the loop once. After finishing the code in the loop body, between the '{' and '}', we increment i, so that it equals 1 after one time through. We then test against 'aVariable' again, repeat the code, and so on: i will equal 0,1,2,3... and up until it is equal to 'aVariable' so that (i < aVariable) evaluates to false, and thus the loop body is skipped. [Note: In contrast to the 'while' loop above, i is scoped to the loop itself in this case; that is, it doesn't exist before or after the loop.]

Its quite common to use such a 'for' loop when cycling (or 'iterating') through a list (or array). For example, we might move through an array of Strings as follows:

  String[] myStrings = { "hi", "bye", "done" };

  for (int i=0; i < myStrings.length ; i++) 
  {
    println("String #"+i+" = "+myStrings[i]);
  }

Notice how we test each time to see if we've reached the end of the array, stored 'automatically' in myStrength.length.

[Hint: if you're not sure what this does, try it in Processing]


Instructor: Daniel C. Howe

E-Writing Resources

Page last modified on March 01, 2007, at 06:32 PM EST