Iterations Notes

4.1 While Loops

  • Repeat code while boolean expression evaluates to true
    while (condition) {
      // code to run
    }
  • While loops are useful to iterate over arrays ``` int[] array = {1, 2, 3, 4} int total = 0; int i = 9;

while(i < array.length) { total += array[i]; i++; }

System.out.println(total);

- Infinite while loops run over and over again, since the condition is always true; useful for user input

while(true) { System.out.println("Choose an option"); } ```

Hacks

Say you have a company that makes a profit of $5,450,000 this year. Every year, the company has a profit increase of 5%. Determine how many years it would take to make a profit of at least $30,000,000 using a while loop.

public class WhileLoops {

    public static void main(String[] args) {
        int profit = 5450000;
        int i = 0;

        while(profit < 30000000) {
            profit *= 1.05;
            i++;
        }
        System.out.println(i);
    }
}

WhileLoops.main(null);
35

4.2 For Loops

Three Parts of a For Loop

  • Initialize Variable
  • Test Condition
  • Change of variable
    for(int x = 1; x<=5; x++) {
      System.out.println(x);
    }
public class ForLoops {
    
    public static void main(String[] args) {
        for(int i = 10; i<= 15; i++) {
            System.out.println(i);
        }

        int[] temperatures = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        for(int i = 0; i<temperatures.length; i++) {
            System.out.println(temperatures[i] + 273);
        }
    }
}

ForLoops.main(null);
10
11
12
13
14
15
274
275
276
277
278
279
280
281
282
283

4.3 Loops and Strings

Strings can be iterated over.

String name = "CodeCodeCode";

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

4.4 Nested Iteration

  • Loop within a loop, typically used for two-dimensional values (ie. iteratue over pixels in an image)

For Each Loops

  • Iterates through elements of arrays and collections (ArrayList)
for(dataType item : array) {
    // code here
}
public class ForEachLoops {
    public static void main(String[] args) {
        int[] data = {2, 10, 5, 12};

        for(int number: data) {
            System.out.println(number);
        }
    } 
}

ForEachLoops.main(null);
2
10
5
12

Hacks

int numbers[] = {2, 5, 7, 12};
int total = 0;

for(int number: numbers) {
    total += number;
}

System.out.println(total);
26
public class CaesarCipher {
    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 cipher(String message) {
        String encodedMessage = "";
        for(int i = 0; i<= message.length; i++) {
            int newLetter = ArrayUntils.indexOf(letters, message[i]);
        }
    }

    public static void main(String[] args) {

        String message1 = "Kfzb gly!";
        String message2 = "zlab zlab zlab";
        String message3 = "prmbozxifcoxdfifpqfzbumfxifalzflrp";

    }
}

CaesarCipher.main(null);