University of British Columbia Reading News Midterm Marks Distribution CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 ■ Next week is reading week ■ marks will not be scaled ■ This week: Chapter 5 all (5.1-5.4) ■ no lectures or labs or tutorials Tamara Munzner ■ second edition: Chap 6 ■ Midterms returned today ■ Grades, statistics already posted on WebCT Loops I ■ returned end of class, line up by last name (A- ■ Next week: Chapter 6 all (6.1-6.4) Z) ■ second edition: Chap 7 Lecture 17, Fri Feb 12 2010 borrowing from slides by Kurt Eiselt http://www.cs.ubc.ca/~tmm/courses/111-10 1 2 3 4 Midterm Distribution: Detailed Regrading Recap: Comparing Strings Recap: Comparing Strings ■ Reminder: protocol for regrade requests ■ Relational operator == is wrong way to compare ■ name1 == name2 : two different references, false ■ read solution and marking scheme first, carefully ■ no regrade requests accepted until at least 24 hours String name1 = "Bubba"; after material is handed back String name2 = "Bubba"; "Bubba" System.out.println(name1 == name2); // prints false ■ exception: arithmetic errors ■ regrade requests must be in writing (paper or email) name1 ■ equals method is right way to compare Strings ■ assignments: to marker (listed on cover sheet) ■ if still have dispute after discussion with TA, can escalate to instructor name2 String name1 = "Bubba"; ■ exams: to instructor String name2 = "Bubba"; "Bubba" System.out.println(name1.equals(name2)); // prints true ■ name1.equals(name2) : contents same, true ■ why? diagrams will help 5 6 7 8 Recap: Short-Circuting Evaluation Recap: Conditional Syntax Recap: Comparing Floats/Doubles Recap: Comparing Characters ■ Java evaluates complex expressions left to right if ( boolean expression ) statement ■ Relational operator for equality not safe for ■ Safe to compare character types with else if ( boolean expression ) statement ■ short-circuiting: Java stops evaluating once value is floating point comparison relational operators clearly true or false ■ optional: zero, one, or many ■ aka lazy evaluation else statement if (.3 == 1.0/10.0 + 1.0/10.0 + 1.0/10.0)) char c = 'a'; ■ optional char d = 'b'; if ((b > a) && (c == 10)) System.out.println("Beware roundoff error"); if (c == d) System.out.println("when b<=a short-circuit"); ■ if , else are reserved words ■ Check if difference close to 0 instead System.out.println("they match"); if ((b > a) || (c == 10)) System.out.println("when b>a short-circuit"); ■ parentheses mandatory ■ statement can be if (Math.abs(f1 - f2) < TOLERANCE) ■ Corollary: avoid statements with side effects ■ single line System.out.println (“Essentially equal.”); ■ block of several lines enclosed in { } if ((b > a) || (c++)) System.out.println("Danger Will Robinson!"); 9 10 11 12 public class NestTest3 { Recap: Switch Syntax Objectives Repetition, Iteration, Loops public static void main (String[] args) { respondToName("Flocinaucinihilipiliphication"); respondToName("Supercalifragilisticexpialidocious"); respondToName("Ambrose"); switch ( expression ) { ■ Computers good at performing same task respondToName("Kermit"); case value : respondToName("Miss Piggy!!!"); many times respondToName("Spot"); ■ Practice with conditionals statements respondToName("me"); } break; ■ Loops allow repetitive operations in programs ■ Understand basic loops public static void respondToName(String name) { case value : System.out.println("You're named " + name); if (name.length() > 20) { statements ■ aka iteration statements, repetition statements System.out.println("Gosh, long name"); break; System.out.println("Keeping typists busy..."); } else if (name.length() > 30) { ■ Loops handy in real life too default: System.out.println("Over the top"); statements } else if (name.length() < 10) { if (name.charAt(0) == 'A') System.out.println("You're first"); switch, case , break are reserved words else if (name == "Kermit") ■ System.out.println("You're a frog"); expression and value must be int or char ■ System.out.println("I love animals"); } else if (name.equals("Spot")) { ■ value cannot be variable System.out.println("You're spotted"); break important, or else control flow continues to next set } else if (name.length() < 3) { ■ System.out.println("Concise!"); statements can be one line or several lines ■ } default executed if no values match expression } ■ } 13 14 15 16
Climbing Stairs Climbing Stairs Climbing Stairs Climbing Stairs Am I at the top of the stairs? Am I at the top of the stairs? Am I at the top of the stairs? Am I at the top of the stairs? ■ ■ ■ ■ No. No. No. ■ ■ ■ Climb up one step. Climb up one step. Climb up one step. ■ ■ ■ Am I at the top of the stairs? Am I at the top of the stairs? ■ ■ No. ■ Climb up one step. ■ 17 18 19 20 Climbing Stairs Climbing Stairs Washing Hair Washing Hair Am I at the top of the stairs? Am I at the top of the stairs? ■ Lather ■ Lather ■ ■ No. No. ■ ■ ■ Rinse Climb up one step. Climb up one step. ■ ■ Am I at the top of the stairs? Am I at the top of the stairs? ■ ■ No. No. ■ ■ Climb up one step. Climb up one step. ■ ■ Am I at the top of the stairs? Am I at the top of the stairs? ■ ■ No. ■ Climb up one step. ■ Am I at the top of the stairs? ■ No. ■ Climb up one step. ■ ...and so on... ■ 21 22 23 24 Washing Hair Washing Hair While Statement If Versus While Statements how if ■ Lather ■ Lather while (boolean expression) statement body ■ Rinse ■ Rinse works Simplest form of loop in Java boolean ■ Repeat ■ Repeat ■ expression Body of loop can be ■ ■ single statement true false ■ whole block of many statements in curly braces Control flow ■ ■ When do you stop?? ■ body executed if expression is true statement ■ then boolean expression evaluated again ■ if expression still true, body executed again ■ repetition continues until expression false ■ then processing continues with next statement after loop 25 26 27 28 If Versus While Statements If Versus While Statements If Versus While Statements Using while Statements how while how while how while how if how if how if public class WhileDemo statement statement statement { statement statement statement public static void main (String[] args) works works works works works works { boolean boolean boolean boolean boolean boolean int limit = 3; expression expression expression expression expression expression int counter = 1; true true false true true false true true false false false false while (counter <= limit) { System.out.println("The square of " + counter + statement statement statement statement statement statement " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } } ■ How can loop boolean change from false to true? ■ These diagrams called flowcharts ■ while statement 29 30 31 32
Recommend
More recommend