Table of Contents

[ Intro | Begin | if/else | Loops | Arrays | Graphics | Animation | Mouse | Game | Real | Methods | Class | Class 2 | Applet | MouseClick | Thread | Button ]


Java For Kids tutorial 4

Java LOOPs

WHILE loops

Try this:

    void main() {
        // 1. declare and initialize variables
        int counter = 1;
 
        // 2. now do the while loop
        while (counter < 5)
        {
                printLine("counter  is equal to " + counter);
                // 3. add 1 to the value of counter
                counter = counter + 1;
        }
 
        // 4. the while loop has finished, so program continues on to here
        printLine("Goodbye");
    }

What was printed?

Counter is equal to 1
Counter is equal to 2
Counter is equal to 3
Counter is equal to 4
Goodbye

Notes on Above code:

Loops are repeating sets of statements and in the case of the while loop, here is the formula:

    while (this statement is true)
    {
        // execute the statements or lines between these brackets
        // add 1 to the counter (usually)
    }
    // and continue the program with the following lines

E.g.

    while (x <5)
    {
        printLine(" x = " + x);
        x = x + 1;
    }

What will the above code output?

Java Exercises:

    void main() {
        while (x < 5)
        {
            printLine(" x = " + x);
            // x = x + 1;
        }
    }
    void main() {
        count = 0;
        while (count < 8)
        {
            Printline("Count is equal to " count);
    }

Java do ... while LOOPs

do while loops are similar to while loops except that the condition is tested after the code to be repeated or looped. The general form of a do … while loop is as follows:

    do {
        // execute code between these brackets
    }
    while (true or false (boolean) statement)

Note that the repeated code is always executed once, because the condition is tested after the loop ends. It may be better to use a plain old while loop in most cases, because it tests the condition before the loop starts.

E.g.

    int y = 10;
    printLine("Counting Down...");
    do {
	    printLine(".. " + y);
	    y = y - 1;
    }
    while (y > 1);
    printLine("Blast Off");

Try it! What did You get?

Java Exericises

    void main() {
        int counter = 10;
 
        while (counter > 1) {
            printLine(" " + counter +
                " green bottles hanging on the wall");
            printLine(" " + counter +
                " green bottles hanging on the wall");
            printLine("And if one green bottle should accidently fall, ");
            printLine("There'd be " + counter-1 +
                " green bottles hanging on the wall");
            counter = counter - 1;
        }
}

Java for LOOPs

Java for loops are a short hand way of;

All in one statement.

Before we carry on we have have to learn about the:

Java INCREMENT operator ( ++ )

    x++;

is the same as

    x = x + 1;

X++ increments (or increases) the value of X by 1. Here is an example that we could use in a while loop:

    int x = 0;         // declare and initialize counter x
    while (x < 5)
    {
        printLine(" x = " + x);
        // add 1 to the value of x
        x++;        // same as (x = x + 1)
    }

Similarly, the:

DECREMENT operator is ( -- )

y– decrements (or decreases) y by 1.

    y--;

is the same as

    y = y - 1;

Try this:

    int y = 10;
    while (y > 0)
    {
        printLine(" and " + y);
        y--;
    }

So, back to the for Loop.

A for Loop sets all the conditions of a loop inside the parantheses after the for statement.

Example A.

    for (int x = 0; x < 10; x++)
    {
        printLine("x = " + x);
    }

The form of the for loop is:

    for (INITIALIZE counter; boolean CONDITION; ITERATION)
    {
        // repeated code to be executed.
    }

Iteration means repetition, or repeating. In actual fact it is either usually increased by 1 (x++) or decreased by 1 ( y–).

In example A above, the Initialisation statement is “int x = 0;”.

This statement declares x as an int and initializes x to be equal to a value of 0 in one go.

The boolean condition is “x < 10;”. This statement tests x and if it is true, then the loop is allowed to execute, If it is false (x=10 or x > 10), then the loop is bypassed and the program execution continues after the { curly brackets } following the for statement.

Example B. Count down from 10 to 1:

    for (int x = 10; x>0; x--)
    {
        printLine(x);
    }