Overview to Control Structures

  • Programming blocks that can change the path taken to complete a set of instructions
  • There are three kinds of structures: Conditional branches, Loops, and Branching Statements
  • Conditional Branches are used for choosing between two or more paths: if/else/else if and switch case
  • Loops are used to sift through different objects and run loops of specific codes: for, while, and do while
  • Branching Statements are used to change the process of loops: break and continue

If/Else/Else If

  • We used this when we make our own learnings last week about if/else statements and Boolean expressions

  • Chooses between two paths

if (count > 6) {
    System.out.println("Count is higher than 6");
} else {
    System.out.println("Count is lower or equal than 6");
}

Ternary Operator

  • Takes three operands rather than one or two

  • Shorter way to write an if else code; uses a question mark

System.out.println(count > 6 ? "Count is higher than 6" : "Count is lower or equal than 6");

Switch case

  • If-else ladder that checks multiple conditions at once
  • Value inputted is checked with each given case until a match is found
  • Things to remember: two cases can't have the same value, data variable needs to be the same in all cases, and the value needs to be literal or constant not a variable.
import java.util.Scanner;

public class SwitchExample { // sets up class and objects for scanner
    public void go() {
        Scanner scan = new Scanner(System.in);
        int score = scan.nextInt();
 
        switch (score){ // switch statement with condition that goes through different cases to find a match
            case 1: // for the score of less than 60
                    System.out.println("You got an F");
                    break;    
            case 2: // for the score  between 60 and 70
                System.out.println("You got a D");
                break;
            case 3: // for a score between 70 and 80
                System.out.println("You got a C");
                break;
            case 4: // for a score between an 80 and a 90
                System.out.println("You got a B");
                break;
            case 5: // for a score between a 90 and 100
                System.out.println("You got an A");
                break;
            default: // if none of the cases are a match, this is what will be the code automatically executed
                System.out.println("You didn't even get a grade");
        }
    }

    public static void main(String[] args) {
        SwitchExample cond = new SwitchExample();
        cond.go();
    }
}

SwitchExample.main(null);
You didn't even get a grade

Loops

  • Used to repeat a code multiple times

  • We can set the number of times we want the code to be repeated

for (int i = 5; i <= 90; i++) {
    methodToRepeat();
}

int whileCounter = 5;
while (whileCounter <= 90) {
    methodToRepeat();
    whileCounter++;
}

Break and Continue

  • A break is used to leave a loop earlier than expected
  • A continue is used to skip a loop we were already in
List<String> names = getNameList();
String name = "Group 3";
int index = 0;
for ( ; index < names.length; index++) {
    if (names[index].equals(name)) {
        break;
    }
}
// Using a list of groups to find one in specific. When we find Group 3, the code will "break" or stop.
List<String> names = getNameList();
String name = "Group 3";
String list = "";
for (int i = 0; i < names.length; i++) { 
    if (names[i].equals(name)) {
        continue;
    }
    list += names[i];
}
// Here, if Group 3 is identified, this code will stop and the program will move on to the next; it will "continue"