The purpose of loop statements is to repeat
something many times. There are three kinds of loop statements in Java: while
,
for
, and do..while
Pseudocode:
Mom says you have to eat all your peas before you get dessert.
//while loop while (peas are still on the plate) eat peas;
//do..while loop do eat peas;
while (peas are still on the plate);
//for loop, 3 more big bites! for(3 bites) eat peas; //////////////////////////////////
do { loop body }while (loop condition); |
for (initialization block; loop
condition; update block) { loop body } |
while (loop condition) { loop body } |
![]() |
![]() |
![]() |
int x=0;//counter variable while (x!=10)//conditional expression { x=x+2; //increment x System.out.println(x); }//compound statement |
//counter is the "for loop control variable" //and starts at 1 //counter<=3 is the condition under which the //loop continues //counter++ is done at the end of each iteration for (int counter=1;counter<=3;counter++) System.out.println("JAVA totally rules!"); |
int x=0; //counter variable do { x=x+2; //increment x System.out.println(x); } while (x<10); |
The "while" loop, used when you want to
check a condition before the commands are executed. |
The "for" loop, used when you know how many times you need to repeat commands. |
The "do..while" loop, used when you want to repeat commands until a condition is met. |
The while loop is a pre-test loop. This loop
will execute as long as the express being tested evaluates to true. A while loop might not
execute if the controlling expression was false the first time. The variable(s) controlling the condition must be initialized before the while statement. . Be sure to include a statement within the body of the while that will make the condition become false. If you fail to do this you will have an infinite loop. |
COUNTING LOOP A counting loop repeats instructions a certain number of times.
Requirements for a Counting Loop 1. Must contain the name of a control variable; any legal variable name. 2. Must contain the initial value of the control variable. 3. The control variable must be incremented or decremented through each iteration of the loop. This eliminates infinite loops. 4. The control variable must test for the final value in order to terminate the control structure |
The do while loop is a post-test loop used by
Java. The biggest difference between the do while loop and the while loop is that the do
while loop always executes at least once because the controlling condition is not tested
until the end of the loop, after the action statements execute. The do while structure may
be a single or compound statement structure. If the condition evaluates to be true, the statements will be executed again. If the condition evaluates to be false, the do/while structure will be exited. |
class Lab10p1 { public static void main(String[] args) { for (int count = 1; count <= 5; count++) { System.out.println("Counting ... " + count); } System.out.println("Done"); } }
class Lab10p2 { public static void main(String[] args) { int repeatTimes =0;
TextIO.put(" how many times ");
repeatTimes= TextIO.getlnInt(); for (int count = 1; count <= repeatTimes; count++) { System.out.println("Counting ... " + count); } System.out.println("Done"); } }
import Keyboard; class Lab10p3 { public static void main(String[] args) { int count = 0; char countAgain = Keyboard.readChar("Start counting? (y/n): ", "yn"); while (countAgain == 'y' || countAgain == 'Y') { count++; System.out.println("Counting ... " + count); countAgain = Keyboard.readChar("Count again? (y/n): ", "yn"); } System.out.println("Done"); } }
import Keyboard; class Lab10p4 { public static void main(String[] args) throws java.io.IOException { int startAt = Keyboard.readInt("Start counting at: "); for (int count = startAt; count <= 5; count++) { System.out.println("Counting ... " + count); } System.out.println("Done"); } }
class Lab10p5 { public static void main(String[] args) { for (int count = 5; count >= 1; ???) { System.out.println("Counting ... " + count); } System.out.println("Done"); } }
class Lab10p6 { public static void main(String[] args) throws java.io.IOException { for (int count = 0; count <= 12; count = count + 3) { System.out.println("Counting ... " + count); } System.out.println("Done"); } }
class Lab10p7 { public static void main(String[] args) throws java.io.IOException { for (double count = ???; count <= ???; count = count + ???) { System.out.println("Counting ... " + count); } System.out.println("Done"); } }
Counting ... 1.0 Counting ... 1.1 Counting ... 1.2 Counting ... 1.3 Counting ... 1.4 Counting ... 1.5 Counting ... 1.6 Counting ... 1.7 Counting ... 1.8 Counting ... 1.9 Counting ... 2.0 Done
- The user may enter a specific value from the keyboard.
- The user may click a mouse button.
- The computer's internal clock may reach a specific time.
- The computer is microphone may detect a sound.
- The user may insert (or remove) a CD-ROM in the CD-ROM tray.
- The user may insert (or remove) a diskette or tape into the computer.
- The computer may detect a network transmission.
- A sensor connected to the computer may send a signal to the computer. (For example, the computer may be connected to a scale in a factory. When a hopper pours enough material onto the scale, the computer may sense this and turn off the hopper.)
-... and many other possibilities.
while (condition) { statements; }
class Lab10p8 { public static void main(String[] args) throws java.io.IOException { char sayAgain = Keyboard.readChar("Say hello? (y/n): ", "yn"); while (sayAgain == 'y' || sayAgain == 'Y') { System.out.println("H E L L O !!"); sayAgain = Keyboard.readChar("Say hello again? (y/n): ", "yn"); } System.out.println("Bye!"); } }
class Lab10p9 { public static void main(String[] args) throws java.io.IOException { int product = 1, number; number = Keyboard.readInt("Enter first number (0 to stop): "); while (number != 0) { product = product * number; number = Keyboard.readInt("Enter next number (0 to stop): "); } System.out.println("The product is " + product); } }
class Lab10p10 // WRONG!! { public static void main(String[] args) throws java.io.IOException { int product = 1, number; while (number != 0) { number = Keyboard.readInt("Enter next number (0 to stop): "); product = product * number; } System.out.println("The product is " + product); } }
- We had to give the variable number an initial value, even though the program never uses that value. This is certainly not intuitive.
- We had to make sure that the initial value of the variable number was not the same as the sentinel value.
- We had to add an if statement inside the while loop.
initialize the variable used in the condition while (the variable is not equal to sentinel value) { do whatever needs to be done inside the loop get the next value of the variable used in the condition }
class Lab10p11 { public static void main(String[] args) throws java.io.IOException { int x = 0; while (x < 10) { System.out.print(x); } System.out.println("Done!"); } }
class Lab10p12 { public static void main(String[] args) throws java.io.IOException { int response; do { response = Keyboard.readInt("What's the sum of 3 and 4? "); } while (response != 7); System.out.println("That's right!"); } }
class Lab10p13 { public static void main(String[] args) throws java.io.IOException { int number; System.out.println("Begin while statement..."); number = 100; while (number < 10) { System.out.println(number); number++; } System.out.println("... End while statement"); System.out.println("Begin do-while statement..."); number = 100; do { System.out.println(number); number++; } while (number < 10); System.out.println("... End do-while statement"); } }