Presentation is loading. Please wait.

Presentation is loading. Please wait.

[ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

Similar presentations


Presentation on theme: "[ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)"— Presentation transcript:

1 [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday) [ ] Examples [ ] Class exercise

2 Lab 6 Put loop logic in seuController main to exit only when user enters 9. analyzeSentence - how many words in sentence? consoleMethods class What about $ ? It blows up! We’ll catch that in a couple weeks.

3 Method Block Diagram seuController.java systemMethods.java seuCalculator.java consoleMethods.java

4 ‘ask permission’

5 ‘ask forgiveness’

6 while( ) { } Pre-condition loop user-controlled what goes in the blank spaces? What is the while-body?

7 // illustration of a ‘primer event’ with while Scanner cscan = new Scanner(System.in); int num1 = 0; System.out.println(“Please enter > 0: ”); num1 = cscan.nextInt(); while( !(num1 > 0 )) { System.out.println(“Invalid entry.”); System.out.println(“Please enter > 0: ”); num1 = cscan.nextInt(); } System.out.println(“Good work!”);

8 // illustration of a ‘loop controller’ (usually a boolean) Scanner cscan = new Scanner(System.in); int num1 = 0; boolean iAmNotHappy = true. while( iAmNotHappy) { System.out.println(“Please enter > 0: ”); num1 = cscan.nextInt(); if (num1 > 0) iAmNotHappy = false; else System.out.println(“Invalid entry.”); } System.out.println(“Good work!”);

9 do { } while( ); Post-condition loop user-controlled what goes in the blank spaces? What is the do-body?

10 do { System.out.println(“here we go ….”); } while( 1 == 1 ); Post-condition loop user-controlled What is the do-body?

11 int num = 0; num++ ++num num = num + 1 All these accomplish the same thing in the end. Specifically, they all increment num by 1. BUT – context is the conundrum int num2 = 5; num2++; int num3 = -3; num3 = --num2; int num4 = 5; num4 = num2++ + --num3

12 int num4 = 5; System.out.println(num4++); System.out.println(“Really, num4 is “ + num4); int num5 = 5; System.out.println(“Hello “ + --num5); System.out.println(“Really, num5 is “ + num5); int num6 = 3; int num7 = -6; int num8 = 0; num8 = --num6 + num7++; System.out.println(“num8 is “ + num8);

13 for ( ; ; ) { } Pre-condition loop loop-controlled What is the ‘for-body’ Is the index automatically incremented? When? What goes in all the blank areas?

14 for ( starting index ;logical expression; index mover) { for body index++ } The ‘index mover’ is implicit – automatically done on way up. Note: DO NOT MUCK WITH AN INDEX

15 for ( int index = 0; index < 5; index++) { System.out.println(“hello!”); } Hand trace Output


Download ppt "[ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)"

Similar presentations


Ads by Google