name: inverse layout: true class: taylor msoe --- class: center, middle .title[ # Conditionals and Loops ] ??? * **P** speaker view * **C** clone slideshow * **?** help --- # Controlling Program Flow * We may only want to perform one or more statements when a specific condition is met -- * `if` statement in Java provides this functionality ``` Java if ( /* CONDITIONAL */ ) { // One or more statements } ``` -- ``` Java int number = in.nextInt() if (number < 0) { System.out.println("You entered a negative number"); } ``` --- # `if` / `else` * May wish to do different things depending on whether a condition is met -- * `if` / `else` statement in Java provides this functionality ``` Java if ( /* CONDITIONAL */ ) { // One or more statements (executed when condition met) } else { // One or more statements (executed when condition not met) ``` --- # Nested `if` * Can nest `if` statements inside `if` statements -- ``` Java int number = in.nextInt(); if (number < 0) { System.out.println("You entered a negative number"); } else { if (number > 0) { System.out.println("You entered a positive number"); } else { System.out.println("You entered a number that is neither positive nor negative"); } } ``` --- # `if` / `else if` * We can simplify the syntax for the previous example as follows: -- ``` Java int number = in.nextInt(); if (number < 0) { System.out.println("You entered a negative number"); } else if (number > 0) { System.out.println("You entered a positive number"); } else { System.out.println("You entered a number that is neither positive nor negative"); } ``` --- # Boolean Operators * More complicated boolean expressions can be constructed with these boolean operator: + `&&` — logical **AND** + `||` — logical **OR** + `!` — logical **NOT** -- ``` Java boolean answer = true && true; // true answer = true && false; // false answer = true || false; // true answer = false || false; // false answer = true || false && true; // true answer = true || false && true; // true ``` --- # Identifier Scope * The **scope** of an identifier defines when it is available to be used -- * An identifier is in scope from when it is declared until the closing brace `}` that matches the previous openning brace, `}` -- ``` Java public static void main(String[] args) { int i = 5; if (i < 10) { int j = i; } } ``` --- # While Loops * A `while` loop is similar to an `if` statement ``` Java while ( /* CONDITIONAL */ ) { // One or more statements } ``` -- * It differs in that when the statement block is completed, the conditional is evaluated again ``` Java Scanner in = new Scanner(System.in); int sum = 0; while (in.hasNextInt()) { sum += in.nextInt(); } System.out.println(sum); ``` --- # Do-While Loops * A `do`-`while` loop checks the conditional after running the statement block ``` Java do { // One or more statements } while ( /* CONDITIONAL */ ); ``` -- * Statement block always runs at least once ``` Java Scanner in = new Scanner(System.in); int evenCount = 0; int value; do { value = in.nextInt(); if (value > 0 && value % 2 == 0) { evenCount += 1; } } while (value > 0); System.out.println("You entered " + evenCount + " even integers"); ``` --- # For loops * A `for` loop is useful when you want to repeat a know number of times ``` Java for ( /* INITIALIZATION */; /* CONDITIONAL */; /* UPDATE */ ); // One or more statements } ``` -- * Example ``` Java String phrase = "This is our last loop type for today." int numSpaces = 0; for (int i = 0; i < phrase.length(); i++) { if (phrase.charAt(i) == ' ') { numSpaces++; } } System.out.println("Your phrase has " + numSpaces + " space in it"); ```