+ All Categories
Home > Documents > Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5...

Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5...

Date post: 25-Dec-2015
Category:
Upload: reynold-harmon
View: 228 times
Download: 1 times
Share this document with a friend
Popular Tags:
30
BIT115: Introduction to Programming Lecture 10 Instructor: Craig Duckett
Transcript
Page 1: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

BIT115: Introduction to ProgrammingLecture 10

Instructor: Craig Duckett

Page 2: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

Assignment 2 RevisionDUE TONIGHT Wednesday, August 5th

Assignment 3DUE NEXT Monday, August 10th

Assignment 3 RevisionDUE Monday, August 17th

DUE Monday, August 24th (NO REVISION)

Assignment 4

Page 3: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

Assignment 1 GRADED

Assignment 2 GRADED

Assignment 1 Revision GRADED

Assignment 2 Revision DUE Lecture 10, Wednesday, August 5th, by midnight

Assignment 3 DUE Lecture 11, Monday, August 10th, by midnight

Assignment 3 Revision DUE Lecture 13, Monday, August 17th, by midnight

Assignment 4 DUE Lecture 15, Monday, August 24th, by midnightNO REVISION AVAILABLE

Extra Credit 01 DUE Lecture 15, Wednesday, August 26th, by midnight

3

Assignment Announcements

Page 4: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

Lecture 9b/10 Topics

• for statement (loop)• do-while loops• Nested loops• cascading-ifs• switch statements

Page 5: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

The for statement (loop)The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:

for (initiating statement; conditional statement; next statement) // usually incremental{ body statement(s);}

“For as long as this condit ion is true . . . do something.”

Page 6: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

The for statement (loop)

There are three clauses in the for statement.

• The init-stmt statement is done before the loop is started, usually to initialize an iteration variable (“counter”). After initialization this part of the loop is no longer touched.

• The condition expression is tested before each time the loop is done. The loop isn't executed if the Boolean expression is false (the same as the while loop).

• The next-stmt statement is done after the body is executed. It typically increments an iteration variable (“adds 1 to the counter”).

for(int count = 1; count < 11; count++) { System.out.println("Count is: " + count); }

Page 7: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

The for statement (loop)for (initial statement; conditional; next statement // usually incremental{ statement(s);}

class ForLoopDemo { public static void main(String[] args) { for(int count = 1; count < 11; count++) { System.out.println("Count is: " + count); } }}

The output of this program is:

Count is: 1Count is: 2Count is: 3Count is: 4Count is: 5Count is: 6Count is: 7Count is: 8Count is: 9Count is: 10

“For as long as this is true . . . do something.”

Page 8: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

initialization; while(condition) { statement; }

for(initialization; condition; increment) { statement;}

The for loop is shorter, and combining the intialization, test, and increment in one statement makes it easier to read and verify that it's doing what you expect.

The for loop is better when you are counting something.

If you are doing something an indefinite number of times, the while loop may be the better choice.

Page 9: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

while loop

class WhileDemo { public static void main(String[] args) { int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } }}

for loop

class ForLoopDemo { public static void main(String[] args) { for(int count = 1; count < 11; count++) { System.out.println("Count is: " + count); } }}

Page 10: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

while loopclass WhileDemo { public static void main(String[] args) { int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } }}

for loopclass ForLoopDemo { public static void main(String[] args) { for(int count = 1; count < 11; count++) { System.out.println("Count is: " + count); } }} SEE: for_while_test.java

Page 11: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

do-while loopsThe Java programming language also provides a do-while statement, which can be expressed as follows:

do { statement(s)} while (expression);

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once, as shown in the following DoWhileDemo program:

class DoWhileDemo { public static void main(String[] args) { int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11); }}

Page 12: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Page 13: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

while loop EVALUATES AT THE TOP

class WhileDemo { public static void main(String[] args) { int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } }}

for do-while loop EVALUATES AT THE BOTTOM

class DoWhileDemo { public static void main(String[] args) { int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11); }}

SEE: for_while_do_while _test.java

Page 14: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

Nested LoopsINSTRUCTOR NOTE: Show Demos (Demo Files located from both Lecture 9 and Lecture 10)

• NestedFor.java• NestedFors2.java• NestWhileTest.java• NestForWhileTest.java• NestedForsClock.java

Page 15: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

REFRESHER: The if-else Statementif(Boolean_expression){ statement 1 //Executes when true}else{ //<-- No Conditional statement 2 //Executes when false}

public class IfElseTest{ public static void main(String args[]) { int x = 30;

if( x < 20 ){ System.out.print(“The number is less than 20."); }else{ System.out.print(“The number is NOT less than 20!"); } }}

Page 16: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

The if-else Statementif(Boolean_expression){ statement 1 //Executes when true}else{ // <--No Conditional statement 2 //Executes when false}

Now, this works great if you’re only testing two conditions, but what do you do if you have more than two conditions? What if you have three conditions, or four, or five?

Page 17: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

Several if Statementsif(Boolean_expression_01){ statement//Executes when true}

if(Boolean_expression_02){ statement//Executes when true}

if(Boolean_expression_03){ statement//Executes when true}

if(Boolean_expression_04){ statement//Executes when true}

if(Boolean_expression_05){ statement//Executes when true}

You could create a whole bunch of if statements to look for and test a particular condition, and this is perfectly acceptable, although this might get unwieldy if you have several conditions, say ten or more to look through.

Page 18: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

Several if Statementsint grade = 98;

if(grade >=0 && grade < 60){ System.out.println(”Sorry. You did not pass.”);}

if(grade >= 60 && grade < 70){ System.out.println(”You just squeaked by.”);}

if(grade >= 70 && grade < 80){ System.out.println(”You are doing better.”);}

if(grade >= 80 && grade < 90){ System.out.println(”Not too bad a job!”);}

if(grade >= 90 && grade <= 100){ System.out.println(”You are at the top of your class!”);}

Page 19: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

“Nested if-elses"It is common to make a series of tests on a value, where the else part contains only another nested if statement. If you use indentation for the else part, it isn't easy to see that these are really a series of tests which are similar. This is traditional syntax, but there’s actually a cleaner way to do this in Java.

int grade = 68; if(grade >=0 && grade < 60) { System.out.println("Sorry. You did not pass."); } else { if(grade >= 60 && grade < 70) { System.out.println("You just squeaked by."); } else { if(grade >= 70 && grade < 80) { System.out.println("You are doing better."); } else { if(grade >= 80 && grade < 90) { System.out.println("Not too bad a job!"); } else //(grade >= 90 && grade <= 100) { System.out.println("You are at the top of your class!"); } } } }

And how about all those squiggles!

Page 20: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

"Cascading-if" if…else if…else

Text

if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true}else if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true}else { // <-- No Conditional //Executes when none of the above conditions are true.}

if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true}else if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true}else if(Boolean_expression 3){ //Executes when the Boolean expression 3 is true}else { // <-- No Conditional //Executes when none of the above conditions are true.}

Page 21: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

"Cascading-if" if…else if…elseSo, contrary to our typical formatting style of indenting to the braces, it is better to write them at the same indentation level by writing the if on the same line as the else.

int grade = 68; if(grade >=0 && grade < 60) { System.out.println("Sorry. You did not pass."); } else if(grade >= 60 && grade < 70) { System.out.println("You just squeaked by."); } else if(grade >= 70 && grade < 80) { System.out.println("You are doing better."); } else if(grade >= 80 && grade < 90) { System.out.println("Not too bad a job!"); } else // <-- No conditional { System.out.println("You are at the top of your class!"); }

Page 22: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

if (this.frontIsBlocked()){ this.turnAround();} else if (this.canPickThing()){ this.turnRight();} else if (this.leftIsBlocked()){ this.turnLeft();} else{ this.move();}

Page 23: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

The Switch Statement(A Brief Look)We’ll look at this in more detail on Monday, August 10 th

Page 24: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Page 25: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

switch statementThe switch statement is similar to the cascading-if statement in that both are designed to choose one of several alternatives. The switch statement is more restrictive in its use, however, because it uses a single value to choose the alternative to execute. The cascading-if can use any expressions desired. This restriction is also the switch statement’s strength: the coder knows that the decision is based on a single value.

switch ( expression ) { case value1 : statement(s) when expression == value1; break; case value2 : statement(s) when expression == value2; break; case value3 : statement(s) when expression == value3; break; default : statement(s) if no previous match; }

Page 26: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Page 27: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

int score = 8;

switch (score) { case 10: System.out.println ("Excellent."); case 9: System.out.println (“Well above average."); break; case 8: System.out.println (“Above average."); break; case 7: case 6: System.out.print ("Average. You should seek help."); break; default : System.out.println ("Not passing."); }

switch statement

Now, this switch written as is will work provided the user enters the correct data and in the correct range, but it will not properly catch improper data or data that is outside the range.

Page 28: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

Assignment 3 – One Last Hint

public void NavigateMaze() { while(!this.isAtEndSpot() ) {

// Something happens here// Something happens here// Something happens here

// if robot cannot pick up a thing { // if robot still has things in its backpack { // put down a thing } }

// Something happens here } // print everything here }

NESTED IF STATEMENTSThere is an if statement INSIDE an if statement.

You might also do something similar without a nested if by using only one if and ‘AND’ logic with &&

With Pseudo-Code

See MAZE city next slide for demonstration

Page 29: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Page 30: Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.

30

LECTURE 10: ICE


Recommended