This is an old revision of the document!
Table of Contents
J1-3 Loops
In the study of programming languages, we have the concept of program control flow, which always operates in ONE direction. This means the CPU will pull an instruction, execute it, then increment the IP (instruction pointer) or PIC (program instruction counter), etc. and then pull the next instruction.
10 PRINT "A" 20 PRINT "B" 30 PRINT "C"
The program here prints A, B, C.
10 PRINT "A" 15 GOTO 30 20 PRINT "B" 30 PRINT "C"
Here, because of the GOTO statements, the program only prints A, C.
10 PRINT "A" 20 GOTO 50 30 PRINT "B" 40 GOTO 70 50 PRINT "C" 60 GOTO 30 70 END
Here, the control flow is modified to print A, C, B.
What is a loop?
When you add a conditional to repeat the same amount of code, it is a loop:
10 A = 3 20 PRINT "HELLO" 30 A = A - 1 40 IF A > 0 GOTO 20 50 END
The code above prints HELLO three times. Why? On the first time it prints, A = 3. Then it subtracts one, and goes back to line 20 since A is still above zero. A is 2 on the second print and 3 on the third. After the third print, A becomes zero, the condition fails, and the program ends.
GOTO in Java
Java does not have a Goto but we can simulate Goto using some fancy code:
public class Main { public static void main(String[] args) { int x = 3; start: do { System.out.println("Hello world!"); x = x - 1; if (x > 0) { continue start; } break; } while (true); } }
In the above code, the line “continue start” is essentially a GOTO statement. The “break” statement is essentially a “goto end” statement.
However, this is clunky and useless. In Java, a better way to do a loop is with a WHILE. This WHILE allows us to test a conditional on every loop execution:
while
public class Main { public static void main(String[] args) { int x = 3; while ( x > 0 ) { System.out.println("Hello world!"); x = x - 1; } } }
Here, we see a simple while loop. This is the basic concept of the JMP statement paired with a CMP statement; the JMP (jump) is goto, the CMP (compare) is IF. If you are interested, the x = x -1 is a DEC statement that lowers some register by 1, which is then tested by CMP.
However, in Java it's easy to understand; “while x is above zero, do this”. Inside the loop, you must decrement the counter by 1. This is important.
do-while
do-while is not commonly used because it is exactly equivalent to a while loop except that it always executes once before checking the condition:
public class Main { public static void main(String[] args) { int x = 3; do { System.out.println("Hello world!"); x = x - 1; } while ( x > 0 ); } }
Since it's harder to read as well, few people use it.
for loops
For loops are very nice loops, based on C loops:
public class Main { public static void main(String[] args) { for (;;) { // Infinite loop System.out.println("This will run forever!"); break; // Break out of the loop } } }
If you want the loop to run for a set amount of time, you must have the initialization, the condition, and the step:
public class Main { public static void main(String[] args) { for (int i = 3; i > 0; i--) { System.out.println("This will run three times!"); } } }