JRJC - How To Make Loops

Brief note for C and C++ programmers

Loops in Java are almost the same as loops in C and C++. The only difference is when testing to see if the loop is done. In C or C++, the expression had to evaluate to zero or non-zero. In Java, it must evaluate to a boolean true or false.

All about loops for everybody else

Loops are what you use to perform similar processes over and over.

To write the numbers 0 through 9, you could do it like this:


    System.out.println("0");
    System.out.println("1");
    System.out.println("2");
    System.out.println("3");
    System.out.println("4");
    System.out.println("5");
    System.out.println("6");
    System.out.println("7");
    System.out.println("8");
    System.out.println("9");

or

    System.out.println("0\n1\n2\n3\n4\n5\n6\n7\n8\n9");

or you could use a loop and do it like this


    int i = 0 ;
    while( i < 10 )
    {
        System.out.println( i );
        i++ ;
    }

This loop is called a "while loop". This is the simplest loop and the one that I use the most often. Its structure looks like this:

    while( boolean-expression )
    {
        stuff
    }

As long as boolean-expression is true, stuff will be executed.

In the "0 through 9" example above, as long as "i" is less than 10, "i" will be printed and then "i" will be incremented.

The first time a "while loop" is encountered, boolean-expression is evaluated to be true or false. If it is false, the whole statement is skipped. If it is true, stuff is executed and then boolean-expression is evaluated again.

Let's walk through a short loop:


      line 1:    int i = 0 ;
      line 2:    while( i < 2 )
      line 3:    {
      line 4:        System.out.println( i );
      line 5:        i++ ;
      line 6:    }
      line 7:    System.out.println("the end!");

Most of the time that I use the "while loop," I use a variable called "done":



    boolean done = false ;
    while ( ! done )
    {
        if ( userPressedEsc() )
        {
            done = true ;
        }
        else
        {
            System.out.println("not done yet");
        }
    }



Note that "!" means "not" in Java. So the controlling statment is "while not done".

The "while loop" can be used for all of your looping needs. However, it is a standard that if you know in advance how many times you need to go through your loop, you use a "for loop". It is considered a shorthand version of a "while loop", but I think it is pretty messy. Nonetheless, you must know how to use the "for loop"!

Take a look at a simple "while loop":


    int i = 0 ;
    while( i < 10 )
    {
        System.out.println( i );
        i++ ;
    }

The "for loop" shorthand for this is:

    for( int i = 0 ; i < 10 ; i++ )
    {
        System.out.println( i );
    }

The "for loop" has three controlling parts:

    for( initialize ; again? ; iterator )
    {
        // stuff goes here
    }

which the compiler converts to:

    {
        initialize ;
        while( again? )
        {
            // stuff goes here
            iterator ;
        }
    }



When I'm writing code and I know I need a loop, I ask myself which message I want to convey:

I always use a "for loop" when I know, in advance, how many times I want to do a loop. For everything else, I always use a "while loop."

There is one other looping construct available in Java. The "do...while loop". I never use it. It does provide a tiny convenience. Some would argue that it provides a more elegant solution once in a great while. This is fine for people that never share their code. When you have to try to explain to somebody what it is and why you use it, didn't you just negate the benefit?

For more information on loops, read page 177 through 179 in Just Java 1.2., or page 73 through 77 in Just Java 2 (sixth edition). Browse the stuff on "continue" and "break". You should know about them, but never use them yourself (IMO).






For more information, please visit my java site.

And for a change of pace, you can visit my permaculture site.






© 2001 paul wheaton