1 / 34

CS 200 Loops

CS 200 Loops. Jim Williams, PhD. This Week. Exam 1: Thursday P5 ( Eclipse & zyBooks) : Thursday Team Lab: More & Maze Hours Last Week? Lecture: Loops, Review. Midterm Exam 1. What is the location of your exam?. Midterm Exam - Thursday. Bring ID and #2 pencil Exam seating

avak
Download Presentation

CS 200 Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 200 Loops Jim Williams, PhD

  2. This Week • Exam 1: Thursday • P5 (Eclipse & zyBooks): Thursday • Team Lab: More & Maze • Hours Last Week? • Lecture: Loops, Review

  3. Midterm Exam 1 What is the location of your exam?

  4. Midterm Exam - Thursday • Bring ID and #2 pencil • Exam seating • directly in front/behind, 1 empty seat to each side • Multiple choice - focus on tracing/explaining Java • Review Questions posted on Piazza

  5. Method Value • The value of a method is the value returned. • The method may also read from a Scanner, print output or have another side effect. static boolean isJava(String name){ System.out.println(name); return name.endsWith(".java"); }

  6. Memory Areas Stack • frame contains local variables and parameters • allocated on method call, freed on method return Heap • allocated when needed, (e.g., using new) • use references to access • garbage collector frees memory no longer accessible

  7. Primitives and Wrapper classes Draw a picture of memory and describe each. public static void main( String []args) { int k; Integer m; k = 2; //example of ? m = 3; //example of ? k = m; //example of ? }

  8. Strings and memory class S { public static void main( String []args) { String name; new String(“dog”); name = new String(“happy”); } }

  9. Where is memory allocated? public static void main( String []args) { String name; name = new String("spot"); new String("fido"); }

  10. Loops https://www.wikihow.com/Race-Your-Car

  11. Loops • Various ways of repeating statements, over and over and over and over and over… • Different purposes (while, for, do-while) • Everything can be done with a while loop but other loops are better choices for code readability. • Keywords that change execution • Nesting • Diagram

  12. Common operators Increment means add 1, decrement subtract 1 prefix and postfix operators int m = 2; m = m + 1; //add one to m store in m m += 1; //compound operator m++; //increment operator (post) ++m; //increment operator (pre)

  13. Value of i, j after this executes? int i = 5; int j = i++;

  14. Value of m after this executes? int i = 5; int k = 6; int m = i++ + ++k;

  15. Potential Confusion: Difference between if and while boolean doIt = true; if ( doIt ) { System.out.println( doIt ); } while ( doIt ) { System.out.println( doIt ); }

  16. 3 loops - Different Purposes do { //indefinite - body executes at least once } while ( !finished); while ( !finished) { //indefinite - body may never execute } for ( int i=1; i < 5; i++) { //definite }

  17. Which loop is the better choice? int a = 1; while ( a < 5) { System.out.println("hello"); ++a; } for ( int a = 1; a < 5; ++a) { System.out.println("hello"); }

  18. UML Activity Diagram (Flow Chart)

  19. Which is best Java construct for this?

  20. Arrange Code to Match Diagram //declarations... off = true; System.out.println("RING, RING”); System.out.println(“Sleeping”); } while ( !off); System.out.print(“Enter to snooze or ‘off’"); if ( response.equals("off")) { off = false; } do {

  21. How many times will this execute? Scanner input = new Scanner( System.in); int num; int sum = 0; do { num = input.nextInt(); sum += num; } while ( num > 0);

  22. Body of While will execute... boolean finished = false; while ( ! finished) { String line = input.nextLine(); if ( line.equals("done")) { finished = true; } else { System.out.println("echo:" + line); } }

  23. How many "hello"? for ( int a = 0; a <= 5; ++a) { System.out.print("hello "); }

  24. Review Topics

  25. What does this do? for ( char ch = 'A'; ch < 'Z'; ch += 3) { System.out.print( ch); }

  26. What is the value of i after this? int i = 4; int j = 3; while ( i <= 15) { i = 2 * -i + j; System.out.println( "i:" + i + " j:" + j); }

  27. Nested Loops

  28. How many "bye"? for ( int i = 1; i <= 5; i++) { for ( int j = 2; j < 6; j++) { System.out.print("bye "); } } //same output line or different?

  29. What does this print? for ( int i = 1; i <= 5; i++) { for ( int j = 1; j <= 3; j++) { System.out.print(i+","+j+" "); } System.out.println(); }

  30. What is the value of count? int count = 0; for ( int i = 1; i < 4; i++) { for ( int j = 6; j > 3; j--) { count += 1; } } System.out.println("count=" + count);

  31. Changing Behavior of Loop continue - skip rest of body, check condition break - leave loop immediately

  32. What is the value of sum? int sum= 0; boolean done = false; for ( int i = 1; i < 4 && !done; i++) { for ( int j = 6; j > 3; j--) { if ( j > 4) continue; sum += 1; } if ( i == 3) break; } System.out.println("sum=" + sum);

  33. How many times does this loop execute? for ( int i = 9; i >= 0; i--) { System.out.println( "i=" + ++i); }

  34. Today Exam Today Time Spent

More Related