Learnings

  • Loop through strings as array of chars.
  • While/for loops: repeats lines of code until a specified condition is met
  • For-each loop iterates through arrays with specified datatypes.
  • Sometimes called enhanced for loops.
  • Nested Iteration is a loop within a loop

For Loop

  • Itereates through an index

Enhanced for Loop

  • Used to iterate fuly through something like an array
// looping through 4
for (int i = 0; i<4; i++) {
    System.out.println(i);
  }

System.out.println(" ");
int[] arr = {1, 2, 3, 4, 5};
  
// looping through array with conventional for loop
for (int i = 0; i<arr.length; i++) {
    System.out.println(arr[i]);
}
System.out.println(" ");

// looping through array with enhanced for loop
for (int i : arr) {
    System.out.println(i);
}
0
1
2
3
 
1
2
3
4
5
 
1
2
3
4
5

While Loop

  • Runs if a condition is true
  • Condition is checked before each iteration of the code is run

Do While Loop

  • Runs while a condition is true
  • Condition is checked after each iteration of the code
int a = 2;
boolean boo = false;

// while loop 
while(a < 5){
    System.out.println(a);
    a ++;
}

// infinite loop here
while(boo){
    System.out.println("Inside while");
}

// if condition is false, loop runs once
do{
    System.out.println("do while");
}
while(boo);
2
3
4
do while

Nested Loops

  • Loop inside of a loop
for(int i = 0; i < 3; i++){
    System.out.println("First for loop run");
    for(int j = 0; j < 5; j++){
        System.out.println("Second for loop run");
    }
}
First for loop run
Second for loop run
Second for loop run
Second for loop run
Second for loop run
Second for loop run
First for loop run
Second for loop run
Second for loop run
Second for loop run
Second for loop run
Second for loop run
First for loop run
Second for loop run
Second for loop run
Second for loop run
Second for loop run
Second for loop run
// HACK 1

public class WhileLoops {
    public double money = 0;
    public double profit = 5450000;
    public double goal = 30000000;
    public double years = 0;

    public void Calc() {
        while (this.money < this.goal) {
            this.money = this.money + this.profit;
            this.profit = this.profit * 1.05;
            this.years = this.years + 1;
        }
        System.out.println(this.years);
    }

    public static void main(String[] args) {
        WhileLoops obj = new WhileLoops();
        obj.Calc();
    }
}

WhileLoops.main(null);
5.0
// HACK 2

public class ForLoops {
    public double temp = 0;
    public void Calc() {
        System.out.println("Numbers 10-15");
        for (int x = 10; x <= 15; x++) {
            System.out.println(x);
        }
        
        System.out.println("Convert temperature");
        for (int x = 0; x<=100; x+=10) {
            temp = 0;
            temp = x + 273.15;
            System.out.println(x + "c -> " + temp + "k");
        }
    }

    public static void main(String[] args) {
        ForLoops obj = new ForLoops();
        obj.Calc();

    }
}

ForLoops.main(null);
Numbers 10-15
10
11
12
13
14
15
Convert temperature
0c -> 273.15k
10c -> 283.15k
20c -> 293.15k
30c -> 303.15k
40c -> 313.15k
50c -> 323.15k
60c -> 333.15k
70c -> 343.15k
80c -> 353.15k
90c -> 363.15k
100c -> 373.15k
// CAESAR CIPHER HW

public class CaesarCipher {


    public 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"};
    public String temp;

    // Method which takes a string and swaps a character
    static char[] swap(String str, int i, char j)
    {
        char ch[] = str.toCharArray();
        ch[i] = j;
        return ch;
    }

    public String Calc(String message) {
        temp = message;
        // Looping through each character in the message
        for (int i=0; i<temp.length(); i++) {  
            // Cast the character to ascii to make substitutions much more efficient
            int ascii = (int) temp.charAt(i);

            // Letters at the end of the alphabet behave differently, so we create two separate conditionals
            if (ascii > 64 && ascii < 88 || (ascii > 96 && ascii < 120)) {
                ascii = ascii + 3;
                String tempSwap = new String(swap(temp, i, (char) ascii));
                temp = tempSwap;
            }
            // This is for the last three letters of the alphabet
            else if (ascii > 87 && ascii < 91 || ascii > 119 && ascii < 123) {
                ascii = ascii - 23;
                String tempSwap = new String(swap(temp, i, (char) ascii));
                temp = tempSwap;
            }
        } 
        return temp;
    }
    public static void main(String[] args) {
        CaesarCipher cipherCalc = new CaesarCipher();
        String message1 = "Kfzb gly!";
        String message2 = "zlab zlab zlab";
        String message3 = "prmbozxifcoxdfifpqfzbumfxifalzflrp";
        System.out.println(cipherCalc.Calc(message1));
        System.out.println(cipherCalc.Calc(message2));
        System.out.println(cipherCalc.Calc(message3));


    }
}
CaesarCipher.main(null);
Nice job!
code code code
supercalifragilisticexpialidocious