= 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!"); } } } Although most loops by convention start at 0 and use something like i < 3, it's the same thing. These loops are the same as C loops more or less. == for-each Java has for-each which is used for arrays, lists, and so on. public class Main { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; for (int num : numbers) { // Iterates over each element in the array System.out.println("Number: " + num); } } } As you can see, this is a great way to iterate through lists. == iterators Sometimes the reason why you want to for-each is to remove bad elements from a list. If you have a lot of memory you can just build a new list and delete the old one. But you can also use iterators, and that will let you remove items in-place. Otherwise they are pretty much equivalent in terms of looping through a list. import java.util.ArrayList; import java.util.Iterator; public class Main { public static void main(String[] args) { ArrayList list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Cherry"); Iterator iterator = list.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } }