Download presentation
Presentation is loading. Please wait.
1
COMP 14: Looping (part 2) May 31, 2000 Nick Vallidis
2
Announcements zAssignment P2 is due today! zAssignment P3 goes out today and is due next Tuesday (June 6th)
3
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;
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. 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...
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
do loop control flow do statement; while (condition); true condition evaluated statement false
12
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
13
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.
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 for (initialization; condition; increment) statement; Java reserved word
17
The for loop for (initialization; condition; increment) statement; Java reserved word This statement is executed before the loop starts
18
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
19
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.
20
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
21
The for loop for (initialization; condition; increment) statement; Don't forget to indent!
22
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");
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
for control flow for (initialization; condition; increment) statement; statementtrue condition evaluated false increment initialization
25
For can be written as while for (initialization; condition; increment) statement; initialization; while (condition) { statement; increment; } turns into
26
More operators zThe most common of these additional operators are the increment and decrement: yincrement is ++ ydecrement is --
27
Increment/decrement zThese can be put either before or after the variable name: int i; i++; //postfix form ++i; //prefix form
28
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
29
Postfix vs. Prefix int i = 7; System.out.println(++i); // prints 8 i = 7; System.out.println(i++); // prints 7
30
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
31
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
32
Examples
33
Homework zRead 3.3, 3.5, 3.6-3.9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.