1 / 33

COMP 14: Looping (part 2)

COMP 14: Looping (part 2). May 31, 2000 Nick Vallidis. Announcements. Assignment P2 is due today! Assignment P3 goes out today and is due next Tuesday (June 6th). Review. How does a while loop work? What is a sentinel value? What is an infinite loop?

adina
Download Presentation

COMP 14: Looping (part 2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 14: Looping (part 2) May 31, 2000 Nick Vallidis

  2. Announcements • Assignment P2 is due today! • Assignment P3 goes out today and is due next Tuesday (June 6th)

  3. Review • How does a while loop work? • What is a sentinel value? • What is an infinite loop? • What does it mean for a program to be robust? while (condition) statement;

  4. Today • do loop • for loop

  5. The do loop • Much like the while loop, but the statement is always executed at least once. do statement; while (condition);

  6. The do loop • Much like the while loop, but the statement is always executed at least once. do statement; while (condition); Java Reserved Words

  7. The do loop • Much like the while loop, but the statement is always executed at least once. do statement; while (condition); Java Reserved Words A boolean expression

  8. The do loop • Much like the while loop, but the statement is always executed at least once. Execute the statement, then test the condition. If it's true then execute the statement again and test the condition... do statement; while (condition); Java Reserved Words A boolean expression

  9. The do loop • Much like the while loop, but the statement is always executed at least once. do statement; while (condition); Don't forget to indent!

  10. do loop example final int LIMIT = 5; int count = 5; do { count = count + 1; System.out.println(count); } while (count < LIMIT);

  11. condition evaluated true statement false do loop control flow do statement; while (condition);

  12. do can be written as while • A do loop can be easily re-written as a while loop. do statement; while (condition); turns into statement; while (condition) statement;

  13. do can be written as while • A do loop can be easily re-written as a while loop. do statement; while (condition); turns into statement; while (condition) statement; These are exactly the same statement.

  14. The for loop • do and while loops are good when you don't know how many times the loop will run. • for loops are good when you know the exact number of iterations.

  15. The for loop for (initialization; condition; increment) statement;

  16. The for loop Java reserved word for (initialization; condition; increment) statement;

  17. The for loop Java reserved word This statement is executed before the loop starts for (initialization; condition; increment) statement;

  18. The for loop Java reserved word This statement is executed before the loop starts A boolean expression to indicate when to stop for (initialization; condition; increment) statement;

  19. The for loop Java reserved word This statement is executed before the loop starts A boolean expression to indicate when to stop for (initialization; condition; increment) statement; Statement that you want to execute multiple times.

  20. The for loop Java reserved word This statement is executed before the loop starts A boolean expression to indicate when to stop for (initialization; condition; increment) statement; Executed after statement, but before the condition is tested again Statement that you want to execute multiple times.

  21. The for loop for (initialization; condition; increment) statement; Don't forget to indent!

  22. Using the for loop • Remember this example? final int LIMIT = 5; int count = 1; while (count <= LIMIT) { System.out.println(count); count = count + 1; } System.out.println("Done");

  23. Using the for loop • Same thing as a for loop final int LIMIT = 5; int count; for (count=1; count<=LIMIT; count=count+1) { System.out.println(count); } System.out.println("Done");

  24. condition evaluated initialization increment statement true false for control flow for (initialization; condition; increment) statement;

  25. For can be written as while for (initialization; condition; increment) statement; initialization; while (condition) { statement; increment; } turns into

  26. More operators • The most common of these additional operators are the increment and decrement: • increment is ++ • decrement is --

  27. Increment/decrement • These can be put either before or after the variable name: int i; i++; //postfix form ++i; //prefix form

  28. Postfix vs. Prefix • These do different things! • Prefix increments the variable before the rest of the statement is executed • Postfix increments the variable after the rest of the statement is executed

  29. Postfix vs. Prefix int i = 7; System.out.println(++i); // prints 8 i = 7; System.out.println(i++); // prints 7

  30. Assignment operators • These are just shortcuts for commonly used expressions: • We probably won't be using the rest of these... x += y is the same as x = x + y x *= y is the same as x = x * y x -= y is the same as x = x - y x /= y is the same as x = x / y x %= y is the same as x = x % y

  31. Conditional Operator • You can read about this in the book (on pg. 130) if you want to learn it. • It is just a shortcut for the if-else statement and so you don't need to know it for COMP 14 Looks like this: condition ? expression : expression

  32. Examples

  33. Homework • Read 3.3, 3.5, 3.6-3.9

More Related