CSS ¡161 ¡ Fundamentals ¡of ¡Compu3ng ¡ Flow ¡control ¡(4) ¡
October ¡17, ¡2012 ¡
CSS 161 Fundamentals of Compu3ng Flow control (4) - - PowerPoint PPT Presentation
CSS 161 Fundamentals of Compu3ng Flow control (4) October 17, 2012 Instructor: Uma Murthy Announcements and reminders Announcements HW 1 grades delayed
October ¡17, ¡2012 ¡
– HW ¡1 ¡grades ¡delayed ¡– ¡will ¡be ¡out ¡today ¡ – Website ¡update ¡ – HW1 ¡solu3on ¡(check ¡homeworks ¡and ¡solu3ons ¡page) ¡ – Lecture ¡7 ¡slides ¡(check ¡updated ¡schedule ¡page) ¡
– All ¡homeworks ¡will ¡be ¡due ¡before ¡class ¡at ¡11am, ¡unless ¡otherwise ¡ specified ¡ – Prac3ce ¡using ¡Prac3ce ¡it, ¡self-‑test ¡exercises, ¡and ¡programming ¡ exercises ¡ – Midterm ¡1 ¡on ¡Wed, ¡Oct ¡24 ¡
CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 2 ¡
CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 3 ¡
CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 4 ¡
CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 5 ¡
CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 6 ¡
String s1 = "tintin"; String s2 = "snowy"; String s3 = "Tintin"; System.out.println(s1.equals(s2)); System.out.println(s1.equals("tintin")); System.out.println(s1.equals(s3)); System.out.println(s1.equalsIgnoreCase(s3)); System.out.println(s1.compareTo(s2)); System.out.println(s2.compareTo(s1)); System.out.println(s2.compareTo("snowy"));
CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 7 ¡
String s1 = "tintin"; String s2 = "snowy"; String s3 = "Tintin"; System.out.println(s1.equals(s2)); System.out.println(s1.equals("tintin")); System.out.println(s1.equals(s3)); System.out.println(s1.equalsIgnoreCase(s3)); System.out.println(s1.compareTo(s2)); System.out.println(s2.compareTo(s1)); System.out.println(s2.compareTo("snowy"));
false ¡ true ¡ false ¡ true ¡ 1 ¡
0 ¡
CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 8 ¡
statement
statement while (boolean_expression)
statement
CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 9 ¡
3-‑10 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
. ¡. ¡. ¡
3-‑11 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
. ¡. ¡. ¡
3-‑12 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
for (Initializing; Boolean_Expression; Update) Body
3-‑14 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
for (ini(aliza(on; test; update) { statement; statement; ... ¡ statement; } – Perform ¡ini(aliza(on ¡once. ¡ – Repeat ¡the ¡following: ¡
body header
3-‑16 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
System.out.println("Homer says:"); System.out.println("I am so smart"); System.out.println("I am so smart"); System.out.println("I am so smart"); System.out.println("I am so smart"); System.out.println("S-M-R-T... I mean S-M-A-R-T");
System.out.println("Homer says:"); for (int i = 1; i <= 4; i++) { // repeat 4 times System.out.println("I am so smart"); } System.out.println("S-M-R-T... I mean S-M-A-R-T");
for (int i = 1; i <= 6; i++) { System.out.println("I am so smart"); }
– Performed ¡once ¡as ¡the ¡loop ¡begins ¡ – The ¡variable ¡is ¡called ¡a ¡loop ¡counter ¡
for (int i = 1; i <= 6; i++) { System.out.println("I am so smart"); }
– Uses ¡comparison ¡operators: ¡ < less ¡than ¡ <= less ¡than ¡or ¡equal ¡to ¡ > greater ¡than ¡ >= greater ¡than ¡or ¡equal ¡to ¡
System.out.println("1 squared = " + 1 * 1); System.out.println("2 squared = " + 2 * 2); System.out.println("3 squared = " + 3 * 3); System.out.println("4 squared = " + 4 * 4); System.out.println("5 squared = " + 5 * 5); System.out.println("6 squared = " + 6 * 6);
– Intui3on: ¡"I ¡want ¡to ¡print ¡a ¡line ¡for ¡each ¡number ¡from ¡1 ¡to ¡6" ¡
for (int i = 1; i <= 6; i++) { System.out.println(i + " squared = " + (i * i)); }
– "For ¡each ¡integer ¡i ¡from ¡1 ¡through ¡6, ¡print ¡..." ¡
for (int i = 1; i <= 4; i++) { System.out.println(i + " squared = " + (i * i)); } System.out.println("Whoo!"); ¡
¡Output: ¡ 1 squared = 1 2 squared = 4 3 squared = 9 4 squared = 16 Whoo! ¡
1 1 2 2 3 3 4 4 5 5
Shorthand ¡Equivalent ¡longer ¡version ¡ variable++; variable = variable + 1; variable--; variable = variable - 1; ¡ int x = 2; x++; // x = x + 1; // x now stores 3 double gpa = 2.5; gpa--; // gpa = gpa - 1; // gpa now stores 1.5 ¡
shortcuts ¡to ¡modify ¡a ¡variable's ¡value ¡ Shorthand ¡Equivalent ¡longer ¡version ¡ variable += value; variable = variable + value; variable -= value; variable = variable - value; variable *= value; variable = variable * value; variable /= value; variable = variable / value; variable %= value; variable = variable % value; x += 3; // x = x + 3; gpa -= 0.5; // gpa = gpa - 0.5; number *= 2; // number = number * 2;
– Use ¡cau3on ¡when ¡combining ¡a ¡declara3on ¡with ¡mul3ple ¡ac3ons ¡
– It ¡is ¡even ¡possible ¡to ¡eliminate ¡the ¡loop ¡body ¡in ¡this ¡way ¡
3-‑24 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
3-‑25 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
– When ¡nested, ¡the ¡inner ¡loop ¡iterates ¡from ¡beginning ¡to ¡end ¡for ¡each ¡ single ¡itera3on ¡of ¡the ¡outer ¡loop ¡ int rowNum, columnNum; for (rowNum = 1; rowNum <=3; rowNum++) { for (columnNum = 1; columnNum <=2; columnNum++) System.out.print(" row " + rowNum + " column " + columnNum); System.out.println(); }
3-‑26 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
3-‑27 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
#================# | <><> | | <>....<> | | <>........<> | |<>............<>| |<>............<>| | <>........<> | | <>....<> | | <><> | #================#
#================# | <><> | | <>....<> | | <>........<> | |<>............<>| |<>............<>| | <>........<> | | <>....<> | | <><> | #================#
¡for ¡(each ¡of ¡5 ¡lines) ¡{ ¡ ¡ ¡ ¡ ¡ ¡print ¡a ¡star. ¡ ¡ ¡ ¡ ¡ ¡print ¡10 ¡spaces. ¡ ¡ ¡ ¡ ¡ ¡print ¡a ¡star. ¡ ¡} ¡ ¡print ¡12 ¡stars. ¡
************ * * * * * * * * * * ************
#================# | <><> | | <>....<> | | <>........<> | |<>............<>| |<>............<>| | <>........<> | | <>....<> | | <><> | #================#
public class Mirror { public static void main(String[] args) { line(); topHalf(); bottomHalf(); line(); } public static void topHalf() { for (int line = 1; line <= 4; line++) { // contents of each line } } public static void bottomHalf() { for (int line = 1; line <= 4; line++) { // contents of each line } } public static void line() { // ... } }
line spaces dots 1 6 2 4 4 3 2 8 4 12 line spaces
line * -2 + 8
dots 4 * line - 4 1 6 6 2 4 4 4 4 3 2 2 8 8 4 12 12
#================# | <><> | | <>....<> | | <>........<> | |<>............<>| |<>............<>| | <>........<> | | <>....<> | | <><> | #================#
#================# | <><> | | <>....<> | | <>........<> | |<>............<>| |<>............<>| | <>........<> | | <>....<> | | <><> | #================#
// Prints the expanding pattern of <> for the top half of the figure. public static void topHalf() { for (int line = 1; line <= 4; line++) { System.out.print("|"); for (int space = 1; space <= (line * -2 + 8); space++) { System.out.print(" "); } System.out.print("<>"); for (int dot = 1; dot <= (line * 4 - 4); dot++) { System.out.print("."); } System.out.print("<>"); for (int space = 1; space <= (line * -2 + 8); space++) { System.out.print(" "); } System.out.println("|"); } }
– When ¡executed, ¡the ¡break ¡statement ¡ends ¡the ¡nearest ¡ enclosing ¡switch ¡or ¡loop ¡statement ¡
– When ¡executed, ¡the ¡continue ¡statement ¡ends ¡the ¡current ¡ loop ¡body ¡itera3on ¡of ¡the ¡nearest ¡enclosing ¡loop ¡statement ¡ – Note ¡that ¡in ¡a ¡for ¡loop, ¡the ¡continue ¡statement ¡transfers ¡ control ¡to ¡the ¡update ¡expression ¡
3-‑36 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
break someIdentifier;
someIdentifier:
3-‑37 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
3-‑38 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
expression ¡
should ¡never ¡be ¡used ¡for ¡floa3ng-‑point ¡
3-‑39 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
System.out.println("n = " + n); // Tracing n – When ¡the ¡error ¡is ¡found ¡and ¡corrected, ¡the ¡trace ¡statements ¡can ¡ simply ¡be ¡commented ¡out ¡
– Common ¡prac3ce ¡in ¡interviews ¡
3-‑40 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
3-‑41 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
3-‑42 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
3-‑43 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
String s = ""; char c = ' '; Scanner keyboard = new Scanner(System.in); do { System.out.println("Enter 'A' for option A or 'B' for option B."); s = keyboard.next(); s.toLowerCase(); c = s.substring(0,1); } while ((c != 'a') || (c != 'b'));
3-‑44 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
Result: Syntax error: c = s.substring(0,1); : incompatible types
found: java.lang.String required: char
3-‑45 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
String s = ""; char c = ' '; Scanner keyboard = new Scanner(System.in); do { System.out.println("Enter 'A' for option A or 'B' for option B."); s = keyboard.next(); s.toLowerCase(); c = s.charAt(0); } while ((c != 'a') || (c != 'b'));
Now the program compiles, but it is stuck in an infinite loop. Employ tracing:
3-‑46 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
do { System.out.println("Enter 'A' for option A or 'B' for option B."); s = keyboard.next(); System.out.println("String s = " + s); s.toLowerCase(); System.out.println("Lowercase s = " + s); c = s.charAt(0); System.out.println("c = " + c); } while ((c != 'a') || (c != 'b'));
Sample output:
Enter 'A' for option A or 'B' for option B. A String s = A Lowercase s = A c = A Enter 'A' for option A or 'B' for option B.
From tracing we can see that the string is never changed to lowercase. Reassign the lowercase string back to s.
3-‑47 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
do { System.out.println("Enter 'A' for option A or 'B' for option B."); s = keyboard.next(); s = s.toLowerCase(); c = s.charAt(0); } while ((c != 'a') || (c != 'b'));
However, it’s still stuck in an infinite loop. What to try next?
3-‑48 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
do { System.out.println("Enter 'A' for option A or 'B' for option B."); s = keyboard.next(); s = s.toLowerCase(); c = s.charAt(0); if ( c == 'a') break; if (c == 'b') break; } while ((c != 'a') || (c != 'b'));
This works, but it is ugly! Considered a coding atrocity, it doesn’t fix the underlying problem. The boolean condition after the while loop has also become meaningless. Try more tracing:
3-‑49 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
do { System.out.println("Enter 'A' for option A or 'B' for option B."); s = keyboard.next(); s = s.toLowerCase(); c = s.charAt(0); System.out.println("c != 'a' is " + (c != 'a')); System.out.println("c != 'b' is " + (c != 'b')); System.out.println("(c != 'a') || (c != 'b')) is " + ((c != 'a') || (c != 'b'))); } while ((c != 'a') || (c != 'b'));
Sample output:
Enter 'A' for option A or 'B' for option B. A c != 'a' is false c != 'b' is true (c != 'a') || (c != 'b')) is true
From the trace we can see that the loop’s boolean expression is true because c cannot be not equal to ‘a’ and not equal to ‘b’ at the same time.
3-‑50 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
do { System.out.println("Enter 'A' for option A or 'B' for option B."); s = keyboard.next(); s = s.toLowerCase(); c = s.charAt(0); } while ((c != 'a') && (c != 'b'));
3-‑51 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
boolean invalidKey; do { System.out.println("Enter 'A' for option A or 'B' for option B."); s = keyboard.next(); s = s.toLowerCase(); c = s.charAt(0); if (c == 'a') invalidKey = false; else if (c == 'b') invalidKey = false; else invalidKey = true; } while (invalidKey);
3-‑52 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
53 ¡ CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡
– An ¡asser3on ¡must ¡be ¡either ¡true ¡or ¡false, ¡and ¡should ¡be ¡true ¡if ¡a ¡ program ¡is ¡working ¡properly ¡ – Asser3ons ¡can ¡be ¡placed ¡in ¡a ¡program ¡as ¡comments ¡
assert Boolean_Expression; – If ¡asser3on ¡checking ¡is ¡turned ¡on ¡and ¡the ¡Boolean_Expression ¡ evaluates ¡to ¡false, ¡the ¡program ¡ends, ¡and ¡outputs ¡an ¡asser%on ¡ failed ¡error ¡message ¡ – Otherwise, ¡the ¡program ¡finishes ¡execu3on ¡normally ¡
3-‑54 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡
3-‑55 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ Compu3ng ¡