Control Flow

What we’ll cover

Block Scope

public static void main(String[] args)
{
    { // this is the scope within which `someUnreachableValue` is defined
        String someUnreachableValue = "This value cannot be reached outside the scope";
    } //  `someUnreachableValue` is not accessible beyond this line
    System.out.println(someUnreachableValue); // compile-time exception
}

Naming Conflicts

public static void main(String[] args)
{  // this is the scope within which `uniqueVariableName` is defined
    String uniqueVariableName = "We cannot re-declare this variable name";
    { // cannot re-declare variable `uniqueVariableName` in inner scope
        String uniqueVariableName; // compile-time exception
    }
}

Conditional Statements

If Statements; General Syntax

public static void main(String[] args)
{
    Scanner userInputGrabber = new Scanner(System.in);
    String userInputPrompt = "Enter your age";
    String userInput = userInputGrabber.nextLine();
    Integer userAge = Integer.parseInt(userInput);
    Integer requiredAge = 18;
    if(userAge >= requiredAge) {
        System.out.println("You are old enough to use the application");
    }
}

If Statements; Example

public static void main(String[] args) {
    Scanner userInputGrabber = new Scanner(System.in);
    String userInputPrompt = "Enter your age";
    String userInput = userInputGrabber.nextLine();
    Integer userAge = Integer.parseInt(userInput);
    Integer requiredAge = 18;
    if(userAge >= requiredAge) {
        System.out.println("You are old enough to use the application");
    }
}

if/else Statements; General Syntax

public static void main(String[] args) {
    Integer randomValue = java.util.concurrent.ThreadLocalRandom.current().nextInt(0, 2);
    Boolean condition = randomValue == 1;
    if(condition) {
        System.out.println("The value was 1");
    } else {
        System.out.println("The value was 0");
    }
}

if/else if Statements; Example

public static void main(String[] args) {
    Scanner userInputGrabber = new Scanner(System.in);
    String userInputPrompt = "Enter your age";
    String userInput = userInputGrabber.nextLine();
    Integer userAge = Integer.parseInt(userInput);
    Integer requiredAge = 18;
    if(userAge > requiredAge) {
        System.out.println("Your age is greater than the required age");
    } else if (userAge == requiredAge) {
        System.out.println("Your age is equal than the required age");
    }
}

if/else if/else Statements; Example

public static void main(String[] args) {
    Scanner userInputGrabber = new Scanner(System.in);
    String userInputPrompt = "Enter your age";
    String userInput = userInputGrabber.nextLine();
    Integer userAge = Integer.parseInt(userInput);
    Integer requiredAge = 18;
    if(userAge > requiredAge) {
        System.out.println("Your age is greater than the required age");
    } else if (userAge == requiredAge) {
        System.out.println("Your age is equal than the required age");
    } else {
        System.out.println("Your age is less than the required age");
    }
}

Multiple Selections - The switch Statement

public static void main(String[] args) {
    Scanner userInputGrabber = new Scanner(System.in);
    String userInputPrompt = "Enter your age";
    String userInput = userInputGrabber.nextLine();
    Integer userAge = Integer.parseInt(userInput);
    switch(userAge) {
        case 15:
            System.out.println("Teenagers cannot use this application!");
            break;

        case 12:
            System.out.println("Preteens cannot use this application!");
            break;

        case 4:
            System.out.println("Children cannot use this application!");
            break;

        case 0:
            System.out.println("Babies cannot use this application!");
            break;

        default:
            System.out.println("Welcome to my application!");
    }
}

Cascading switch conditions

public static void main(String[] args) {
    Scanner userInputGrabber = new Scanner(System.in);
    String userInputPrompt = "Enter your age";
    String userInput = userInputGrabber.nextLine();
    Integer userAge = Integer.parseInt(userInput);
    switch(userAge) {
        case 15:
            System.out.println("Teenagers cannot use this application!");

        case 12:
            System.out.println("Preteens cannot use this application!");

        case 4:
            System.out.println("Children cannot use this application!");

        case 0:
            System.out.println("Babies cannot use this application!");
            break;

        default:
            System.out.println("Welcome to my application!");
    }
}

Case labels