COMP 14: Looping (part 2) May 31, 2000 Nick Vallidis
Announcements zAssignment P2 is due today! zAssignment P3 goes out today and is due next Tuesday (June 6th)
Review zHow does a while loop work? zWhat is a sentinel value? zWhat is an infinite loop? zWhat 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. do statement; while (condition); Java Reserved Words A boolean expression Execute the statement, then test the condition. If it's true then execute the statement again and test the condition...
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);
do loop control flow do statement; while (condition); true condition evaluated statement false
do can be written as while A do loop can be easily re-written as a while loop. do statement; while (condition); statement; while (condition) statement; turns into
do can be written as while A do loop can be easily re-written as a while loop. do statement; while (condition); statement; while (condition) statement; turns into 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 for (initialization; condition; increment) statement; Java reserved word
The for loop for (initialization; condition; increment) statement; Java reserved word This statement is executed before the loop starts
The for loop for (initialization; condition; increment) statement; Java reserved word This statement is executed before the loop starts A boolean expression to indicate when to stop
The for loop for (initialization; condition; increment) statement; Java reserved word This statement is executed before the loop starts A boolean expression to indicate when to stop Statement that you want to execute multiple times.
The for loop for (initialization; condition; increment) statement; Java reserved word This statement is executed before the loop starts A boolean expression to indicate when to stop Statement that you want to execute multiple times. Executed after statement, but before the condition is tested again
The for loop for (initialization; condition; increment) statement; Don't forget to indent!
Using the for loop zRemember 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");
for control flow for (initialization; condition; increment) statement; statementtrue condition evaluated false increment initialization
For can be written as while for (initialization; condition; increment) statement; initialization; while (condition) { statement; increment; } turns into
More operators zThe most common of these additional operators are the increment and decrement: yincrement is ++ ydecrement is --
Increment/decrement zThese can be put either before or after the variable name: int i; i++; //postfix form ++i; //prefix form
Postfix vs. Prefix zThese do different things! zPrefix increments the variable before the rest of the statement is executed zPostfix 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 zThese are just shortcuts for commonly used expressions: zWe 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 zYou 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
Examples
Homework zRead 3.3, 3.5,