name: inverse layout: true class: taylor msoe --- class: center, middle .title[ # Conditionals and Looping Review ## if, switch/case, while, for, do/while ] ??? Toggle speaker view by pressing P Pressing C clones the slideshow view, which is the view to put on the projector if you're using speaker view. Press ? to toggle keyboard commands help. --- # `if` Statement ``` if(CONDITIONAL) { // STATEMENTS // ... // STATEMENTS } ``` -- * The statements within the curly braces will only be executed if `CONDITIONAL` evaluates to `true` -- * The `CONDITIONAL` can be a boolean literal or an expression that evaluates to a boolean value -- + `if(true) { ... }` — `...` will always run -- + `if(false) { ... }` — `...` will never run -- + `if(3<5) { ... }` — `...` will always run -- + `if(3!=4) { ... }` — `...` will always run --- # Boolean Expressions * The following are relational operators that can be used in boolean expressions: + `<` — Less than + `<=` — Less than or equal + `>` — Greater than + `>=` — Greater than or equal + `==` — Equal + `!=` — Not equal -- * Relational operators can be used to produce a boolean value from two primitives --- # Common Error Warning > Note: `3 < 4 < 5` is not legal! -- * Operators are evaluated from left to right and `3 < 4` evaluates to `true` -- * That results in `true < 5` which produces an error since comparing a boolean with an `int` is illegal --- # Boolean Logic Operators * `!` — NOT operator: takes a single boolean value and produces the opposite -- * `||` — OR operator: takes two boolean values and produces `true` if either value was `true` -- * `&&` — AND operator: takes two boolean values and produces `false` if either value was `false` --- # Boolean Expression Example 1 * `1.1 < 2.2 && 3 > 4` -- * -> `true && 3 > 4` -- * -> `true && false` -- * -> `false` --- # Boolean Expression Example 1 * `"word".length() < 8 || (1.1 < 2.2 && 3 > 4)` -- * -> `4 < 8 || (1.1 < 2.2 && 3 > 4)` -- * -> `true || (1.1 < 2.2 && 3 > 4)` -- * -> `true` -- + Here `(1.1 < 2.2 && 3 > 4)` doesn't get evaluated because `true` OR'd with anything is `true` -- + This is called "short-circuit evaluation" --- # `if`/`else` Statement ``` if(CONDITIONAL) { // A STATEMENTS } else { // B STATEMENTS } ``` -- * Either the `A STATEMENTS` or the `B STATEMENTS` will execute — never both and never neither -- * `A STATEMENTS` will only be executed if `CONDITIONAL` evaluates to `true` -- * `B STATEMENTS` will only be executed if `CONDITIONAL` evaluates to `false` --- # `if`/`else`/`if` Statements ``` if(CONDITIONAL1) { // A STATEMENTS } else if(CONDITIONAL2) { // B STATEMENTS } else if(CONDITIONAL3) { // C STATEMENTS } else if(CONDITIONAL4) { // D STATEMENTS } else { // E STATEMENTS } ``` -- * `A STATEMENTS` will only be executed if `CONDITIONAL1` evaluates to `true` -- * `B STATEMENTS` run if `CONDITIONAL1` evaluates to `false` and `CONDITIONAL2` evaluates to `true` + I.e., `!CONDITIONAL1 && CONDITIONAL2` --- # `if`/`else`/`if` Statements ``` if(CONDITIONAL1) { // A STATEMENTS - CONDITIONAL1 } else if(CONDITIONAL2) { // B STATEMENTS - !CONDITIONAL1 && CONDITIONAL2 } else if(CONDITIONAL3) { // C STATEMENTS - !CONDITIONAL1 && !CONDITIONAL2 && CONDITIONAL3 } else if(CONDITIONAL4) { // D STATEMENTS - !CONDITIONAL1 && !CONDITIONAL2 && !CONDITIONAL3 && CONDITIONAL4 } else { // E STATEMENTS - !CONDITIONAL1 && !CONDITIONAL2 && !CONDITIONAL3 && !CONDITIONAL4 } ``` --- # Conditionals Within Conditionals ``` if(CONDITIONAL1) { // A STATEMENTS if(CONDITIONAL2) { // B STATEMENTS } else { // C STATEMENTS } } else { // D STATEMENTS } ``` -- * `A STATEMENTS` run if `CONDITIONAL1` * `B STATEMENTS` run if `CONDITIONAL1 && CONDITIONAL2` * `C STATEMENTS` run if `CONDITIONAL1 && !CONDITIONAL2` * `D STATEMENTS` run if `!CONDITIONAL1` --- # Comparing Objects * Relational operators (`<`, `<=`, `==`, `!=`, `>=`, and `>`) work with primitives -- * Relational operators don't work with reference types -- * Instead we need to ask the object to do the work: `.equals()` -- ``` String word1 = "Happy"; String word2 = "happy"; String word3 = "Happy"; boolean answer = word1.equals(word2); // false answer = word1.equals(word3); // true answer = word2.equals(word1); // false ``` --- # `while` Loop ``` while(CONDITIONAL) { // STATEMENTS } ``` -- * Works just like an `if` statement except it repeatedly checks the conditional after running `STATEMENTS` --- # `for` Loop ``` for(INITIALIZATION; CONDITIONAL; UPDATE) { // STATEMENTS } ``` -- * Just like a `while` loop with the following additions: -- + `INITIALIZATION` runs before checking `CONDITIONAL` for the first time -- + `UPDATE` runs after `STATEMENTS` and before checking `CONDITIONAL` -- + `UPDATE` will run as many times as `CONDITIONAL` evaluates to `true` --- # `do`/`while` Loop ``` do { // STATEMENTS } while(CONDITIONAL); ``` -- * Just like a `while` loop but `STATEMENTS` runs before checking `CONDITIONAL` -- * As a result, `STATEMENTS` always runs at least once --- # `switch`/`case` Statement ``` switch (VARIABLE) { case VALUE1: // A STATEMENTS case VALUE2: // B STATEMENTS break; case VALUE3: // C STATEMENTS break; default: // D STATEMENTS } ``` -- * `A STATEMENTS` **and** `B STATEMENTS` run if `VARIABLE` has the value `VALUE1` -- * `B STATEMENTS` run if `VARIABLE` matches `VALUE2` -- * `C STATEMENTS` run if `VARIABLE` matches `VALUE3` -- * `D STATEMENTS` run if `VARIABLE` doesn't match `VALUE1`, `VALUE2`, or `VALUE3` -- * `VARIABLE` must be an `String` or integer (`int`, `long`, `short`) type --- # Variable Scope * We say a variable identifier is **declared** when a new identifier is associated with a type: ``` String word; int i; ``` -- * The scope of the variable is limited to the curly braces where it was declared ``` if(i==word.length()) { double x = 3.8; // ... // x is within scope here } // x is outside of scope here ```