COMP 14 Introduction to Programming Miguel A. Otaduy June 4, 2004.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
-
date post
19-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Loops• Allow us to repeat statements some number of
times
• Must use a loop control variable– controls how many times to loop
• 4 Parts to Every Loop– initialization - set loop control variable before
condition– condition - when to stop– update - change to the loop control variable– body - actions to repeat
The for Loop
• Specialized form of while loop• Simplifies the writing of count-controlled loops
Basic Form:
for (initialization; condition; update){
statement(s);}
The for LoopExecution
1. initial statement executes
2. loop condition evaluated
3. If loop condition evaluates to true, execute for loop statement and execute update statement
4. Go back to step 2 and repeat until loop condition is false
The for LoopSyntax
for ( initialization; condition; update ){ loop body;}
Reservedword
The initializationis executed once
before the loop begins
The loop body isexecuted until the
condition becomes false
The update portion is executed at the end of each iterationThe condition-loop body-update cycle is executed repeatedly
for vs. while
A for loop is functionally equivalent to the following while loop structure:
initialization;while ( condition ){ loop body; update;}
The for LoopExample
final int LIMIT = 3;
int count;
for (count=0; count<LIMIT; count++)
{
System.out.println (count);
}
System.out.println (“All done!”);
booleancondition
loop body
update
initialization
Output:012All done!
Comparing while and for
While Loop
final int LIMIT=3;int i = 0;
while (i < LIMIT){
System.out.println (i);i++;
}
System.out.println (“All done!”);
For Loop
final int LIMIT=3;int i;
for (i=0; i<LIMIT; i++){
System.out.println (i);}
System.out.println("All done!");
Watch out!
• for statement ending in semicolon is empty; does not affect program
• while statement ending in semicolon results in infinite loop
for (count=0; count<LIMIT; count++);
while (count<LIMIT);
Examples
for (i=1; i<=5; i++){
System.out.println ("Hello");System.out.println ("*");
}
for (i=1; i<=5; i++)System.out.println ("Hello");System.out.println ("*");
for (i=1; i<=5; i++);System.out.println ("*");
Hello*Hello*Hello*Hello*Hello*
HelloHelloHelloHelloHello*
*
In-Class Exercisefor vs. while Loopsfinal int MAX = 20;
int i;
for (i = 0; i<MAX; i+=3)
{
System.out.println (i);
}
• Predict the output
Output:0369121518
In-Class Exercisefor vs. while Loopsfinal int MAX = 20;
int i;
for (i = 0; i<MAX; i+=3)
{
System.out.println (i);
}
• Translate this to a while loop.
final int MAX = 20;
int i = 0;
while (i < MAX)
{
System.out.println (i);
i += 3;
}
The do...while LoopSyntax
do{ loop body;}while ( condition );
do andwhile arereserved
words
The loop body is executed once initially,and then the condition is evaluated
The loop body is executed repeatedlyuntil the condition becomes false
The do...while Loop
• Like a while loop, but its condition is at the end of the loop
• Loop body always executes at least once
• Must also be checked for termination (not an infinite loop)
• Anything you can do with a do...while loop, you can do with a while loop
The do...while LoopExamplefinal int LIMIT = 3;
int count = 0;
do {
System.out.println (count);
count++;
} while (count < LIMIT);
System.out.println (“All done!”);
booleanconditio
n
loopbodyupdat
e
initialization
Output:012All done!
Comparing while and do...while
while Loop
final int LIMIT=3;int count = 0;
while (count < LIMIT){
System.out.println (count);count++;
}
System.out.println (“All done!”);
do...while Loop
final int LIMIT=3;int count = 0;
do{
System.out.println (count);count++;
}while (count < LIMIT);
System.out.println("All done!");
while vs. do...while
i = 11;while (i <= 10) {
System.out.print (i);i += 5;
}System.out.println();
i = 11;do {
System.out.print (i);i += 5;
} while (i <= 10);System.out.println();
[blank line]
11
In-Class ExerciseThe do...while Loop
int x = 0, y = 0;do {System.out.println (x*y);if (y < x){
y += 2; }x++;
} while (x < 5);
• Predict the output of the loop
x
y0
0
Output:
1 2
004616
23
4
45
break Statements
• Used to exit early from a loop • Used to skip remainder of switch
structure• Can be placed within if statement
of a loop– If condition is met, loop exited
immediately
break Example
double in;while (true){
in=Double.parseDouble(keyboard.readLine());
if (in == 0.0){
break;}System.out.println (1.0 / in);
}
inOutput
1.0 1.00
2.0 0.504.0
0.250.0
continue Statements• Used in while, for, and do...while
structures• When executed in a loop, the remaining
statements in the loop are skipped; proceeds with the next iteration of the loop
• When executed in a while/do…while structure, expression evaluated immediately after continue statement
• In a for structure, the update statement is executed after the continue statement; then the loop condition executes
continue Example
double in;while (true){
in=Double.parseDouble(keyboard.readLine());
if (in == 0.0){
continue;}System.out.println (1.0 / in);
}
inOutput
1.0 1.00
2.0 0.504.0
0.250.05.0
0.20infinite loop
Nested Control Structures• Provides new power, subtlety, and
complexity• if, if…else, and switch
structures can be placed within while loops
• for loops can be found within other for loops – each time through the outer loop, the
inner loop goes through its full set of iterations
Nested Control Structures Examplefor (int row = 1; row <= 5; row++)
{
for (int star = 1; star <= row; star++)
{
System.out.print(“*”);
}
System.out.println();
}
Output:***************
• Can't use the variable row outside the outer for loop.• Can't use the variable star outside the inner for loop.
ExercisePrinting Patterns
• Use nested for loops
* * *
* * *
* * *
* * *
* * *
* * *
1 2 3
4 5 6
7 8 9