[ ] Review Exam
[ ] Questions
[ ] Lab 6
[ ] HW6[ ] Method Block Diagram[ ] Methods, methods[ ] Looping constructs
[ ] for loop[ ] ++, -- (Thursday)
[ ] Examples[ ] Class exercise
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.
Method Block Diagram
seuController.java systemMethods.java
seuCalculator.java consoleMethods.java
‘ask permission’
‘ask forgiveness’
while( )
{
}
Pre-condition loopuser-controlledwhat goes in the blank spaces?What is the while-body?
// 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!”);
// 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!”);
do
{
}
while( );
Post-condition loopuser-controlledwhat goes in the blank spaces?What is the do-body?
do
{
System.out.println(“here we go ….”);
}
while( 1 == 1 );
Post-condition loopuser-controlled
What is the do-body?
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
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);
for ( ; ; )
{
}
Pre-condition looploop-controlled
What is the ‘for-body’
Is the index automatically incremented? When?What goes in all the blank areas?
for ( starting index ;logical expression; index mover)
{
for bodyindex++
}
The ‘index mover’ is implicit – automatically done on way up.
Note: DO NOT MUCK WITH AN INDEX
for ( int index = 0; index < 5; index++)
{
System.out.println(“hello!”); }
Hand trace Output