CSC297.Java, Class 5: Control Structures Admin * What else did you decide to do with Points? * Note that java.lang.Math provides lots of useful operations * Note that java.math also provides some useful classes * Note that when you can't find something, you should email me * How are things going? * Assignment for Wednesday: Simple calculator. * Read a number, read an operation, read another number * Print the result * Do it all again * When do you stop? * If you have extra time: Permit unary operations (like sin, cos, ...) Topic: * Conditional expressions * Regular conditionals * Comparison operations * Loops Control Structures 101 * Imperative languages include a variety of common control structures. * Most other languages include these in some form or another. * Conditional: Choose between multiple options * Loop: Repeat stuff * Function call: Encapsulate control * Recursive function call: Repeat stuff Conditionals * Dozens of different possible designs * Java has only a few * BishopD loves the conditional expression: Not choosing between different commands, but choosing between different values * Form: TEST ? TRUEPART : FALSEPART * Meaning: Evaluate the TEST. If it is true, evaluate and use the value of the TRUEPART. If the TEST is false, evaluate and use the value of the FALSEPART * E.g., absolute value of x: (x >= 0) ? x : -x * E.g., Set y to 5 plus the absolute value of x y = 5 + ((x >= 0) ? x : -x); * The more standard conditional operation is the "if" * It lets you choose between different commands * if (TEST) TRUESTATEMENT * if (TEST) TRUESTATEMENT else FALSESTATEMENT * Meaning: Execute the TEST. If it holds, execute the TRUESTATEMENT. If it fails to hold and there is a FALSESTATEMENT, execute the FALSESTATEMENT if (x >= 0) y = 5 + x; else y = 5 - x; if (y > 100) { println("You pass."); } * You execute these only for the side effects. * You can include compound statements as the TRUESTATEMENT or FALSESTATEMENT if (x >= 0) { out.println("Oh boy, " + x + " is positive."); y = y + 1; } else { out.println("Life sucks."); throw new Exception("..."); } Side note on exceptions (which we'll return to later) * "throws Exception" means "this procedure _might_ crash and burn" * "throw new Exception" means "crash right here, right now" What kinds of tests can you have? * Simple arithmetic comparison * ==, !=, <, >, <=, >= * Procedures that return type boolean if (pt.inQuadrantOne()) pt.translate(1,1); * object.equals(other-object) if (name.equals("Sam")) if (operator.equals("+")) * Combine tests with or (||) and and (&&) if ((x < y) || (y < z)) { ... } * Not: Negate a test with ! if (!pt.inQuadrantOne()) { pt.translate(100,100); } * Exclusive or (sam forgets syntax) holds when exactly one of its two arguments hold Loops * Most common: while * Form: while (TEST) BODY * Meaning: Execute TEST. If it holds, execute the BODY and try again. Otherwise, you're done with the while loop * E.g., to read a series of numbers and sum them, stopping when you hit 0. (Assume we've written readNum.) sum = 0; while ((newval = readNum()) != 0) sum = sum + newval; * Second most common: for (normally used for counting) * Form: for (INIT; TEST; INCREMENT) BODY * Meaning: INIT; while (TEST) { BODY; INCREMENT } * Used primarily for clarity for (i = 0; i < 10; i = i+1) out.println(i); * Often used with arrays * Typical form: for (VAR = INITIAL_VALUE; VAR <= FINAL_VALUE; VARIABLE = VARIABLE + INCREMENT) { BODY; } * Third most common do { BODY } while (TEST) Side note: What does out.flush() do? * Strange output model: The stuff only gets printed when you tell it to print a newline OR when you tell it "print the damn stuff now!"