While loops consist of 2 portions: the boolean expression and the brackets which store the looping code inside.

while (condition) {
    ...
}

The boolean expression is checked before the loop is started and every time the loop ends and is about to start anew. Usually, inside the loop something is done that slightly changes the conditions for the boolean expression until it reads false and ends. In the example below, the condition is x > 0, meaning that x has to be changed for the loop to stop. Inside the loop, x is decremented by 1 every time, changing the conditions over and over again until it finally returns false and terminates the while loop.

int x = 5;

// The boolean expression in this case is x > 0
while (x > 0) {
    System.out.println(x);
    x--;
}
5
4
3
2
1

One unique application of while loops lie in infinite while loops, loops that run over and over again permanently. This is usually accomplished by setting the boolean condition to be true at all times. The only way to stop these loops are to use a break command, which ends the loop regardless of the conditions present.

For Loop and Enhanced Loop

  • Enhanced for loops are easier to use and errors will usually happen less, as you don't have to manage steps by themselves, but for loops allows you to control everything about looping
  • For loops you can change by how many steps it iterates and if it goes backwards or forwards
  • Enhanced for loops will just go forward and increment its steps by 1

Nested For Loop

  • A nested loop is a loop inside of another loop ### Example
class Main {
    public static void main(String[] args) {
  
      int weeks = 3;
      int days = 7;
  
      
      for (int i = 1; i <= weeks; ++i) {
        System.out.println("Week: " + i);
  
  
        for (int j = 1; j <= days; ++j) {
          System.out.println("  Day: " + j);
        }
      }
    }
  }
  Main.main(null);
Week: 1
  Day: 1
  Day: 2
  Day: 3
  Day: 4
  Day: 5
  Day: 6
  Day: 7
Week: 2
  Day: 1
  Day: 2
  Day: 3
  Day: 4
  Day: 5
  Day: 6
  Day: 7
Week: 3
  Day: 1
  Day: 2
  Day: 3
  Day: 4
  Day: 5
  Day: 6
  Day: 7

Hacks

public class WhileLoops {
    public static void main(String[] args) {
        int i = 5450000;
        int years = 0;
        while (i < 30000000){
            i *= 1.05;
            years += 1;
        }
        System.out.println(years);
    }
}

WhileLoops.main(null);
35
public class ForLoops {
    public static void main(String[] args) {
        for (int x = 10; x <= 15; x++) {
            System.out.println(x);
        }
    }
}
ForLoops.main(null);
10
11
12
13
14
15
public class ForLoops {
    public static void main(String[] args) {
        for (int x = 10; x <= 25; x++) {
            System.out.println(x * 9/5 + 32);
        }
    }
}
ForLoops.main(null);
50
51
53
55
57
59
60
62
64
66
68
69
71
73
75
77
int numbers[] = {2, 5, 7, 12};
int sum = 0;
for (int num: numbers){
    sum += num;
}
System.out.println(sum);
26
public class CaesarCipher {
    private final String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    private String newMsg;

    public String Decipher(String msg){
        this.newMsg = "";
        for(int i = 0; i < msg.length(); i++){
            if(msg.substring(i, i+1).equals(" ")){
                this.newMsg += " ";
            }
            else{
                for(int k = 0; k < letters.length; k++){
                    if(msg.substring(i, i+1).equals(this.letters[k])){
                        if(k + 3 > 25){ // largest index is 25 because starts from 0
                            this.newMsg += letters[k + 3 - 26]; // subtract 26 because still shift 26 letters
                        }
                        else{
                            this.newMsg += letters[k + 3];
                        }
                    }
                }
            }
        }
        return this.newMsg;
    }

    public static void main(String[] args) {
        // String message1 = "Kfzb gly!";
        String message1 = "kfzb gly!";
        String message2 = "zlab zlab zlab";
        String message3 = "prmbozxifcoxdfifpqfzbumfxifalzflrp";

        CaesarCipher solver = new CaesarCipher();
        System.out.println(solver.Decipher(message1));
        System.out.println(solver.Decipher(message2));
        System.out.println(solver.Decipher(message3));
    }
}

CaesarCipher.main(null);
nice job
code code code
supercalifragilisticexpialidocious