340 likes | 512 Views
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
E N D
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 • directly in front/behind, 1 empty seat to each side • Multiple choice - focus on tracing/explaining Java • Review Questions posted on Piazza
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"); }
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
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 ? }
Strings and memory class S { public static void main( String []args) { String name; new String(“dog”); name = new String(“happy”); } }
Where is memory allocated? public static void main( String []args) { String name; name = new String("spot"); new String("fido"); }
Loops https://www.wikihow.com/Race-Your-Car
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
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)
Value of i, j after this executes? int i = 5; int j = i++;
Value of m after this executes? int i = 5; int k = 6; int m = i++ + ++k;
Potential Confusion: Difference between if and while boolean doIt = true; if ( doIt ) { System.out.println( doIt ); } while ( doIt ) { System.out.println( doIt ); }
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 }
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"); }
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 {
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);
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); } }
How many "hello"? for ( int a = 0; a <= 5; ++a) { System.out.print("hello "); }
What does this do? for ( char ch = 'A'; ch < 'Z'; ch += 3) { System.out.print( ch); }
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); }
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?
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(); }
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);
Changing Behavior of Loop continue - skip rest of body, check condition break - leave loop immediately
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);
How many times does this loop execute? for ( int i = 9; i >= 0; i--) { System.out.println( "i=" + ++i); }
Today Exam Today Time Spent