name: inverse layout: true class: taylor msoe --- class: center, middle .title[ # Computation and Decisions Review ## Assignment, Math, Decisions ] ??? 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. --- # Assignment * The `=` operator is known as the **assignment** operator -- * It assigns the value on the right to the value on the left -- * `int i = 3;` causes `i` to be assigned the value `3` -- * A variable identifier must always appear by itself to the left of `=` -- * Illegal: `i + 3 = i;`, `5.2 = x;` --- # Arithmetic * The following are standard arithmetic operators: + `+` - addition + `-` - subtraction + `/` - division + `*` - multiplication + `%` - modulus/remainder -- * Unary operators take one argument -- * Binary operators take two arguments --- # Operator Precedence * Unary operators take precedence: + `-x` + `(int)x` -- * Binary multiplication and division next: + `x * y` + `x / y` + `x % y` -- * Binary addition and subtraction next: + `x + y` + `x - y` -- * Use parentheses to override + `3 * (5 + 8)` results in `39` (`3 * 13`) --- # Typecasting * If an `int` is available but a `double` is needed, it will automatically be converted -- * If a `double` is available but an `int` is needed, an compiler error is generated -- * To explicitly convert from one primitive type to another, use typecasting: + `(int)x` converts `x` to an `int` (truncates digits after the decimal place) + `(char)i` converts `i` to a `char` --- # Compound Assignment Operators * The following compound assignment operators take the value on the right and apply it to the variable on the left: `*=`, `/=`, `+=`, `-=`, and `%=` -- * For example, + `i += 3;` is equivalent to `i = i + 3;` + `x *= 2;` is equivalent to `x = x * 2;` + `y /= 8;` is equivalent to `y = y / 8;` --- # Console Input * `System.in` is a source of console keyboard input -- * `Scanner in = new Scanner(System.in)` creates a new `Scanner` object that uses console keyboard input as its data source -- * `in.next()` Reads one token (whitespace to whitespace) and returns it as a String -- ``` String word = in.next() // Returns token as a String int i = in.nextInt() // Reads token and converts it to an int double x = in.nextDouble() // Reads token and converts it to a double ``` -- * Can use `Integer.parseInt(value)` to convert a `String` to an `int` -- * Can use `Double.parseDouble(value)` to convert a `String` to a `double` --- # Output * `System.out` is an output stream connected to the console -- * `System.out.println(stuff);` will send the characters in `stuff` to the console and advance to the next line -- * `System.out.print(stuff);` will send the characters in `stuff` to the console without advancing to the next line --- # Algorithms * An algorithm is a set of steps/instructions to solve a problem -- * Algorithms can be expressed in English, pseudocode, flowcharts, or a language like Java -- * Pseudocode — simple, clear statements that describe the steps to solve a problem -- * Flowchart — a graphical description of an algorithm --- # Flowchart Symbols * Flow begins and ends with a circle:
-- * A rectangle describes a statement (unit of work):
--- # Flowchart Symbols * A diamond represents a decision point in program flow:
--- # Pseudocode Example — Do you have a long name? ``` bash print "Please enter your name" input name set length to length of name if length>10: print "You have a long name." else: print "You don't have a long name." ``` --- # Flowchart Example — Do you have a long name?
--- # Java `if`/`else` Statement ``` if(CONDITIONAL) { // statements to run if CONDITIONAL is true } else { // statements to run if CONDITIONAL is false } ``` --- # Java `if` Statement Example — Do you have a long name? ``` bash System.out.println("Please enter your name"); Scanner in = new Scanner(System.in); String name = in.next(); int length = name.length(); if(length>10) { System.out.println("You have a long name."); } else { System.out.println("You don't have a long name."); } ``` --- # Java `while` Loop * You can think of a `while` loop as a forgetful `if` statement -- ``` while(CONDITIONAL) { // statements to run if CONDITIONAL is true } ``` * Repeatedly runs statements until `CONDITIONAL` is no longer true --- # Java `while` Loop Example ``` int i=5; while(i>0) { System.out.println(i); i -= 1; } ``` -- ``` 5 4 3 2 1 ``` --- # Java `for` Loop * The `for` loop is like a `while` loop with two additions: + INITIALIZATION + UPDATE -- ``` for(INITIALIZATION; CONDITIONAL; UPDATE) { // statements to run if CONDITIONAL is true } ``` * `INITIALIZATION` runs once and then checks the `CONDITIONAL` * Statements run if `CONDITIONAL` is true * `UPDATE` runs after the statements * Repeats as long as `CONDITIONAL` is true --- # Java `for` Loop Example ``` for(int i=0; i<5; i+=1) { System.out.println(i); } ``` -- ``` 0 1 2 3 4 ```