[ [[tutorial-en-001 | Intro]] | [[tutorial-en-002 | Begin]] | [[tutorial-en-003 | if/else]] | [[tutorial-en-004 | Loops]] | [[tutorial-en-005 | Arrays]] | [[tutorial-en-006 | Graphics]] | [[tutorial-en-007 | Animation]] | [[tutorial-en-008 | Mouse]] | [[tutorial-en-009 | Game]] | [[tutorial-en-010 | Real]] | [[tutorial-en-011 | Methods]] | [[tutorial-en-012 | Class]] | [[tutorial-en-013 | Class 2]] | [[tutorial-en-014 | Applet]] | [[tutorial-en-015 | MouseClick]] | [[tutorial-en-016 | Thread]] | [[tutorial-en-017 | 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: * Initialize the counter to 1. * The while loop (the statements inside the curly brackets after while) will repeat as long as the statement after it is true. If the statement is false (counter = 5 or greater) then the loop stops. * The counter adds one to itself. Each time the loop goes through, one is added to the counter until the condition (x<5) is false. I.e. x = 6. If the condition is false the loop is bypassed. * Goes on to the next line and prints "goodbye", and ends the program. 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? {{java007.gif}} ** Java Exercises: ** * Try the above example with the line x = x + 1; commented out. void main() { while (x < 5) { printLine(" x = " + x); // x = x + 1; } } * Try the other examples of loops and comment out (start the line with a "//" so that line is not executed) the lines that add (increment) or minus (decrement) 1 to the condition variable (x or y). * What happens? Why? * Fix the errors in this code: 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? {{java008.gif}} ** Java Exericises ** * Which loop always executes at least one pass or loop? * What is the output of the following code: 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; } } * Sing what you have just printed out. * In a do ... while loop, when is the boolean statement tested? * Print out the set of even numbers from 20 to 30. * Print out the numbers between 27 and 35. ===== Java for LOOPs ===== Java for loops are a short hand way of; * initializing the counter, * setting the boolean condition, and * incrementing the counter, 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); }