package Loops; // loops, sometimes with keywords that make me sadface import java.util.Scanner; public class LoopEx { // loop until you hit high, or the number to avoid public static void breakFn(int high, int avoid) { System.out.println("WITH BREAK"); for (int i = 0; i < high; i++) { // I can't figure out how to write loop conditions! for (int j = 0; j < high; j++) { // if we hit the bad number, kill the inner loop if (i + j == avoid) { System.out.println("PANIC"); break; } System.out.println(i + " " + j); } } System.out.println("WITH LOGIC"); // I'm you but smarter for (int i = 0; i < high; i++) { for (int j = 0; j < high && i + j != avoid; j++) { System.out.println(i + " " + j); } } } // print out all combos, but avoid the forbidden number itself public static void continueFn(int high, int avoid) { System.out.println("WITH CONTINUE"); for (int i = 0; i < high; i++) { for (int j = 0; j < high; j++) { System.out.print(i + " " + j); // don't print the forbidden number if (i + j == avoid) { System.out.println(); continue; } System.out.println(" " + (i + j)); } } System.out.println("WITH ...ELSE"); for (int i = 0; i < high; i++) { for (int j = 0; j < high; j++) { System.out.print(i + " " + j); // don't print the forbidden number if (i + j == avoid) { System.out.println(); // oh look, all it took was an else. } else { System.out.println(" " + (i + j)); } } } } // loop until you hit high, but avoid the forbidden number public static void labeled(int high, int avoid) { // label for labeled break, allows breaking out of this loop from anywhere in loop outerLabel: for (int i = 1; i < high; i++) { // label for labeled break, allows breaking out of just inner loop // in this case same as plain break innerLabel: for (int j = 1; j < high; j++) { if (j == avoid) { System.out.println("outer break: j is avoid"); break outerLabel;// end whole loop when j is bad } if (i + j == avoid) { System.out.println("inner break: combo is avoid"); break innerLabel; // end inner loop when combo is bad } System.out.println(i + " " + j); } } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); // ask user for max until we get something in range System.out.println("Enter max between 1 and 100"); int max = scan.nextInt(); // while checks condition and keeps going until condition is false while (max < 1 || max > 100) { System.out.println("Enter max between 1 and 100"); max = scan.nextInt(); } // ask user for number to avoid until we get something in range int avoid; // do while does its code at least once before checking condition // continues until condition is false do { System.out.println("Enter number to avoid between 1 and 100"); avoid = scan.nextInt(); } while (avoid < 1 || avoid > 100); System.out.println("BREAK----------------------------------"); breakFn(max, avoid); System.out.println("CONTINUE----------------------------------"); continueFn(max, avoid); System.out.println("LABELED BREAK----------------------------------"); labeled(max, avoid); } }