330 likes | 423 Views
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?
E N D
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? • What does it mean for a program to be robust? while (condition) statement;
Today • do loop • for loop
The do loop • Much like the while loop, but the statement is always executed at least once. do statement; while (condition);
The do loop • Much like the while loop, but the statement is always executed at least once. do statement; while (condition); Java Reserved Words
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
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
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!
do loop example final int LIMIT = 5; int count = 5; do { count = count + 1; System.out.println(count); } while (count < LIMIT);
condition evaluated true statement false do loop control flow do statement; while (condition);
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;
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.
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.
The for loop for (initialization; condition; increment) statement;
The for loop Java reserved word for (initialization; condition; increment) statement;
The for loop Java reserved word This statement is executed before the loop starts for (initialization; condition; increment) statement;
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;
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.
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.
The for loop for (initialization; condition; increment) statement; Don't forget to indent!
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");
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");
condition evaluated initialization increment statement true false for control flow for (initialization; condition; increment) statement;
For can be written as while for (initialization; condition; increment) statement; initialization; while (condition) { statement; increment; } turns into
More operators • The most common of these additional operators are the increment and decrement: • increment is ++ • decrement is --
Increment/decrement • These can be put either before or after the variable name: int i; i++; //postfix form ++i; //prefix form
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
Postfix vs. Prefix int i = 7; System.out.println(++i); // prints 8 i = 7; System.out.println(i++); // prints 7
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
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
Homework • Read 3.3, 3.5, 3.6-3.9