Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th."— Presentation transcript:

1 Lecture 10 Instructor: Craig Duckett

2 Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th Assignment 3 Revision DUE Monday, August 17 th DUE Monday, August 24 th (NO REVISION) Assignment 4

3 Assignment 1 Assignment 1 GRADED Assignment 2 Assignment 2 GRADED Assignment 1 Revision Assignment 1 Revision GRADED Assignment 2 Revision Assignment 2 Revision DUE Lecture 10, Wednesday, August 5 th, by midnight Assignment 3 Assignment 3 DUE Lecture 11, Monday, August 10 th, by midnight Assignment 3 Revision Assignment 3 Revision DUE Lecture 13, Monday, August 17 th, by midnight Assignment 4 Assignment 4 DUE Lecture 15, Monday, August 24 th, by midnight NO REVISION AVAILABLE Extra Credit 01 Extra Credit 01 DUE Lecture 15, Wednesday, August 26 th, by midnight 3 Assignment Announcements

4 Lecture 9b/10 Topics for statement (loop) do-while loops Nested loops cascading-ifs switch statements

5 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 condition is true... do something.”... do something.”

6 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); }

7 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: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Count is: 6 Count is: 7 Count is: 8 Count is: 9 Count is: 10 “For as long as this is true... do something.”

8 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.

9 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); }

10 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); } SEE: for_while_test.java

11 do-while loops The 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); }

12

13 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

14 Nested Loops INSTRUCTOR NOTE: Show Demos (Demo Files located from both Lecture 9 and Lecture 10) NestedFor.java NestedFors2.java NestWhileTest.java NestForWhileTest.java NestedForsClock.java

15 REFRESHER: The if-else Statement if(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!"); }

16 The if-else Statement if(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?

17 Several if Statements if(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.

18 Several if Statements int 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!”); }

19 “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 && grade = 70 && grade = 80 && grade = 90 && grade <= 100) { System.out.println("You are at the top of your class!"); } } } } And how about all those squiggles!

20 "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. }

21 "Cascading-if" if…else if…else So, 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 && grade = 70 && 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!"); }

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

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

24

25 switch statement The 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; }

26

27 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.

28 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 STATEMENTS There 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

29

30 30 LECTURE 10: ICE


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

Similar presentations


Ads by Google