Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 200 Loops Jim Williams, PhD.

Similar presentations


Presentation on theme: "CS 200 Loops Jim Williams, PhD."— 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? 125 Agriculture Hall
6210 Sewell Social Sciences B102 Van Vleck Other (due to conflicts) notified via 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 Heap 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 review 1 passing primitive to method changing parameter in method 2 passing array to method changing contents of array in method 3 passing array to method changing parameter to another array in method 4 passing a string to method calling string method in method public class ClassNameHere { static void method1(int num) { num = 5; } static void method2(int []listA) { listA = new int[3]; listA[1] = 10; static void method3(StringBuffer name) { name //name.toUpperCase(); public static void main(String[] args) { String name = "Helen"; method3( name); System.out.println( name); // int [] listB = {4, 6, 8}; // method2( listB); // System.out.println( listB[1]); // int num = 4; // method1( num); // System.out.println("num=" + num);

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?
name: heap new String("spot"): heap name: stack new String("spot"): stack public static void main( String []args) { String name; name = new String("spot"); new String("fido"); } B is the best answer A, C and D are not true

10 Loops

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++; 5,5 6,5 5,6 other

14 Value of m after this executes?
int i = 5; int k = 6; int m = i k; 11 12 other try it.

15 Potential Confusion: Difference between if and while
boolean doIt = true; if ( doIt ) { System.out.println( doIt ); } while ( 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; } while for other for ( int a = 1; a < 5; ++a) { System.out.println("hell o"); } for since a definite number of iterations.

18 UML Activity Diagram (Flow Chart)

19 Which is best Java construct for this?
If statement for loop while loop do while loop

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 { //declarations are assumed... off = false; do { System.out.println(“Sleeping”); System.out.println("RING, RING”); System.out.print(“Enter to snooze or ‘off’"); response = input.nextLine(); off = response.equals("off"); } while ( !off);

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

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); } until "done" is input never forever other? try it

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

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); } 5 13 -23 49

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? 5 9 20 other?

29 What does this print? for ( int i = 1; i <= 5; i++) {
1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3 4,1 4,2 4,3 5,1 5,2 5,3 1,1 2,1 3,1 1,2 2,2 3,2 1,3 2,3 3,3 1,4 2,4 3,4 1,5 2,5 3,5 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); 3 9 12 other

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

32 What is the value of sum? 3 9 12 other 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); 3 9 12 other

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

34 Today Exam Today Time Spent


Download ppt "CS 200 Loops Jim Williams, PhD."

Similar presentations


Ads by Google