CSC161 2010F, Class 18: Control Flow: Conditionals (2) Overview: * If statements, revisited. * Conditional expressions. * Case statements. * lab Admin: * Reading for Tuesday: K&R 3.5-3.6 (loops). * New reading requirement: By 9 p.m. the night before the reading is due, send me a note about what you found most difficult. I will try to cover those issues in class the next day. * I will reserve time at the start of class for questions on the exam. Questions on the Exam * For Problem 3, *When do we stop? * EOF * Or you can choose exit * Is each line like the command line? * No, you still have values on the stack * Do you need a p at the end of each line? * No, Sam just wasn't very creative in writing the example * But you won't get output if you don't have a p. * For the triangles problem, how do I find out if a scalene triangle is obtuse or acute? Do we have to use trig? (Not really.) * Thing about the angle opposite the longest side. * In a right triangle, a^2 + b^2 = c^2 * In an acute triangle, a^2 + b^2 ? c^2 * In an obtuse triangle, a^2 + b^2 ? c^2 * Or use Wolfram alpha * For Problem 5, * Why does 128.6 show up as 128.6000006? * Because of the way that C represents 128.6; it can't represent it exactly. Sam's stupid question of the day * What, exactly, does "Consistency is the hobgoblin of small minds" mean? * We use it when we want to say "Who cares?" to someone who wants us to be more consistent (say, about capitalization after bullet points) Conditional Statements in C if (TEST) CONSEQUENT; else ALTERNATE; If you want multiple consequents or alternates, put 'em in braces if (TEST) { CONSEQUENT1; CONSEQ@UENT2; } else { ALTERNATE1; ALTERNATE2; } You don't need the alternate (e.g., the alternate is "nothing") if (TEST) CONSEQUENT; E.g., if we're only looking at *this* (e.g., if we're only counting carriage returns) if (ch == '\n') ++newlines; You can nest ifs, in which case it tests each condition as it goes, and "stops" when you get the first successful condition if (TEST1) CONS1; else if (TEST2) CONS2; else if (TEST3) CONS3; else ALTERNATE; // Note: We know that !TEST1 and !TEST2 and !TEST3 For testing ranges if (x >= 90) printf ("A-like"); else if ((x >= 80) && (x < 90)) printf ("B-like"); ... vs. if (x >= 90) printf ("A-like"); else if (x >= 80) printf ("B-like"); ... So, how do conditionals in C differ from conditionals in Scheme (other than the new syntax) * In Scheme, we use conditionals in (at least) two ways * To select between operations to do (if (equal? (car object) 'circle) (image-select-ellipse! image 10 10 50 50) (image-select-rectangle! image 10 10 50 50)) * To choose between two values to use in an expression (square (if (> x y) x y)) * In C, these two versions require different syntaxes For the expression form, we use the legendary ?: syntax To compute the square root of the larger of x and y sqrt (x > y ? x : y) Form TEST ? CONSEQUENT : ALTERNATE 3 + (x > y ? x : y) It is usable *anywhere* you can use an expression * There is no alternate-free version of ?: Scheme provides one other form of conditional, the switch statement switch (EXPRESSION) { case VAL1: case VAL2: STATEMENTS; case VAL3: STATEMENTS; break; case VAL4: STATEMENTS; break; default: STATEMENTS; break; } Meaning: 1. Evaluate the expression 2. Find a matching case value (or default if no one matches) 3. Execute all subsequent code until you hit the first break or return