Presentation is loading. Please wait.

Presentation is loading. Please wait.

Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days.

Similar presentations


Presentation on theme: "Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days."— Presentation transcript:

1 Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days = 31; } System.out.println ( “Month” + month + “ has” + days + “ days”); A way to eliminate confusing if/else statements Note: You can only switch on Integer type variables: char, byte, short, int, and long

2 Iterations (Three kinds) Body Again ? Pre-test Again? Post-test Initialize Adjust Counter Body Again ? Counter controlled The Java syntax for each kind Is somewhat different

3 Pre Test Loop Sum numbers until a user enters a negative int x = 0, sum = 0; while (x >= 0) { x = IO.readInt (“Enter an integer to sum”); if (x >=0) sum +=x; } System.out.println(“The sum is ” + sum); while ( ) { }

4 Post-test loop (another way) Sum numbers until a user enters a negative int x = 0, sum = 0; do { sum += x; x = IO.readInt (“Enter an integer to sum”); } while (x >= 0); System.out.println(“The sum is ” + sum); do { } while ( );

5 Sum number another way int x = 0, sum = 0; while (true) { x = IO.readInt (“Enter an integer to sum”); if (x <0) break; sum +=x; } System.out.println(“The sum is ” + sum); Note: The break statement jumps out of the loop

6 Counter controlled loop Sum numbers from one to one-hundred int sum; for (int i=1; i<=100; i++) { sum += i; } System.out.println (“The sum is ” + sum); for ( ; ; ) { } Question: How to sum numbers from -100 to +100? Question: How to sum just the even numbers? i = 1 i++ sum+=i i <= 100

7 Pre-test loop: sum 1 through 100 Sum 1 through 100 int sum = 0, i = 1; while (i<=100) { sum += i++; } Question: What happens if the ++ was left off? Sum even numbers 2 through 100 (one way) int sum = 0, i = 1; while (i<=0) { if (++i%2!=0) continue; sum += i; } Sum even numbers 2 through 100 (another way) int sum = 0; i = 2; while (i<=100) { sum += i; i += 2; } Note: The continue statement iterates to the next value

8 Post-test loop to sum numbers Sum 1 through 100 int sum = 0, i = 1; do { sum += i++; } while (i<=100); Question: What happens if the ++ was left off? Sum even numbers 2 through 100 (one way) int sum = 0, i = 1; do { if (++i%2!=0) continue; sum += i; } while (i<=100); Sum even numbers 2 through 100 (another way) int sum = 0; i = 2; do { sum += i; i += 2; } while (i<=100);

9 A more difficult problem int sum = 0; for (int num=1; num < 1000); num++) { for (int i=2; i<num; i++) { if (num%i == 0) { sum += num; break; } Sum all the non-primes up to 1000 Note: A prime is a number that is divisible by only itself and one

10 Primitive verses Object variables Primitive variable types: long, int, short, byte, float, double, boolean Object variable types: String and others that we will be creating. How do we compare to primitive variables for equality? – Answer: We use == – Example: if (x == y) How do we compare object variables for equality? – Answer: We use the object’s.equals method – Example: if (str.equals(“quit”)); Question: Why the difference? Answer: Because == will compare where in memory the object is and not its contents. Note: There is more to this, but for now, this explanation will suffice

11 Structure an input loop Loop until the user enters a negative while ((value = IO.readInt("Enter value: "))>=0) { // Do something with the value variable } Loop until the user types quit do { // Do some kind of processing more = IO.readString(); } while (more!=null && !more.equals(“quit”));

12 Review 1.When would you use a pre-test, post-test, or counter controlled loop? 2.What is an infinite loop? Give an example of when it can happen. 3.What is the difference between continue and break? 4.Which loop allows you to initialize a variable as part of its initialization? 5.What is the loop condition for? What is the loop initialization for? 6.When would you use a switch statement? 7.What is a limitation of using a switch statement? 8.What is a nested loop, condition, or a switch? 9.Why is a loop a good thing to apply to the lab projects we did so far? 10.What is a prime number? 11.Which loop requires a semicolon as part of its syntax? 12.When are the braces needed in connection with the loop body? 13.Define the terms: iteration and body


Download ppt "Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days."

Similar presentations


Ads by Google