CSC152 2006S, Class 11: Conditionals Admin: * Sorry about the readings for today * Homework revised: One-line calculator * Readings for Friday available some time Thursday * Feel free to ask questions today! (As always) Overview: * Form of conditionals * Form of tests in conditionals * Lab Most languages have one or more control structures that let you say "If this condition holds then do this thing, otherwise do this other thing" In Scheme * (cond ((this condition) (this thing)) ((another condition) (another thing)) ... (else (something-to-do-if-none-of-the-conditions-hold))) * (if (this condition) (this thing) (this other thing)) * Java provides only if (and an ugly thing called "switch") if (TEST) { CODE_TO_RUN_IF_TEST_SUCCEEDS; } else { CODE_TO_RUN_IF_TEST_FAILS; } * When you want to do multiple tests, as in a cond if (TEST1) { CODE_TO_RUN_IF_TEST1_SUCCEEDS; } else if (TEST2) { CODE_TO_RUN_IF_TEST1_FAILS_AND_TEST2_SUCCEEDS; } else if (TEST3) { .... } ... else { CODE_TO_RUN_IF_NO_TEST_SUCCEEDS; Form of tests: * Arithmetic comparison x < y x <= y x == y ; Equality uses two equal signs x > y x >= y x != y ; x is not equal to y * Combine tests test1 || test2 ; OR, pronounced "bar bar" or "pipe pipe" or "or or" test1 && test2 ; AND , pronounced "and and" !test1 ; NOT , pronounced "bang" * Example, is i between 1 and 10? if ((i >= 1) && (i <= 10)) { ... } * In dealing with objects, you need to use methods rather than those cool one-or-two-character operations * obj.equals(other) if (textuserentered.equals(password)) { pen.println("Welcome, you have entered the correct password"; } else { toaster.lasergun.zap(user); } * obj.compareTo(other) * Returns a negative number if obj "naturally precedes" other * Returns 0 if obj is equal to other * Returns a positive number of obj "naturally follows" other * compareTo is supposed to be transitive and reflexive * Transitive if a naturally precedes b and b naturally precedes c then a naturally precedes c * Reflexive: if a nabutrally precedes b then b natrually follows a * Misspelled: This eboard