while loops loops until the boolean inside is false, which stops the while loop from continuing

int x = 5;

while (x > 0) {
    System.out.println(x);
    x--;
}
5
4
3
2
1

the loop keeps looping if the condition inside of is true

for(int i=0;i<5;i++) {
    System.out.println(i);
}
0
1
2
3
4

can use for loops on strings to iterate through

String name = "abcdef";

for (int i = 0; i < name.length(); i+=2) {
    System.out.println(name.substring(i,i+2));
}
ab
cd
ef

while loop versus do while loop

  • while loops are loos that keep continuing until a boolean isn't met anymore
  • do while loops are the same as while loops except for the loop checking for the conditions after checking a statement

for loop, 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
public class Main {

    public static void main(String[] args) {

            // create an array
        int[] data = {2, 10, 5, 12};
    
            // for each loop 
        for (int number: data) {
        System.out.println(number);

    }
}
}
Main.main(null);
2
10
5
12
  • a nested loop is a loop inside of another loop
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

HW

public class CaesarCipher {


    public static void main(String[] args) {

        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"};
        String message1 = "Kfzb gly!";
        String message2 = "zlab zlab zlab";
        String message3 = "prmbozxifcoxdfifpqfzbumfxifalzflrp";
        ArrayList<String> caesar = new ArrayList<String>();
        for (int i = 0; i < letters.length; i++) {
            int d = 0;
            if (i < 23) { 
                caesar.add(letters[i+3]);
            }
            else {
                caesar.add(letters[i+3-26]);
            }

        }
        String message11 = " ";
        for (char c : message1.toCharArray()) {
                message11 += Character.toString((char) (((c - 'a' + 3) % 26) + 'a'));
        }
        String message22 = " ";
        for (char c : message2.toCharArray()) {
                message22 += Character.toString((char) (((c - 'a' + 3) % 26) + 'a'));
        }
        String message33 = " ";
        for (char c : message3.toCharArray()) {
                message33 += Character.toString((char) (((c - 'a' + 3) % 26) + 'a'));
        }
        System.out.println(caesar);
        System.out.println(message11);
        System.out.println(message22);
        System.out.println(message33);
        
        


    }
}
CaesarCipher.main(null);
[d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, a, b, c]
 NiceWjobX
 codeWcodeWcode
 supercalifragilisticexpialidocious