. . .

  • Conditional Branches, which we use for choosing between two or more paths.
  • Loops that are used to iterate through and repeatedly run specific code blocks.
  • Branching Statements, which are used to alter the flow of control in loops.

Conditional Branches:

The If/Else Statement

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

The Switch Statement

switch (count) {
    case 0:
        System.out.println("Count is equal to 0");
        break;
    case 1:
        System.out.println("Count is equal to 1");
        break;
    default:
        System.out.println("Count is either negative, or higher than 1");
        break;
    }

Loops!

The For Loop:

for (int i = 1; i <= 50; i++) {
    methodToRepeat();
}

The While Loop:

int whileCounter = 1;
while (whileCounter <= 50) {
    methodToRepeat();
    whileCounter++;
}

Branching Statements:

The Break:

List<String> names = getNameList();
String name = "John Doe";
int index = 0;
for ( ; index < names.length; index++) {
    if (names[index].equals(name)) {
        break;
    }
}

The Continue:

List<String> names = getNameList();
String name = "John Doe";
String list = "";
for (int i = 0; i < names.length; i++) { 
    if (names[i].equals(name)) {
        continue;
    }
    list += names[i];
}