Download presentation
Presentation is loading. Please wait.
1
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann
2
Michael Eckmann - Skidmore College - CS 106 - Spring 2007 Today’s Topics questions, comments? loop terminology To repeatedly execute a block of code –for loops –do - while loops
3
while loops While loops are used to repeat lines of code until some condition is met. e.g. while (condition) { // statements to do while condition is true. } condition is evaluated and if true the code within the curly braces executes. Then condition is tested again and if true the code within the curly braces executes. This continues until the condition is false (or a break; statement is hit.) Michael Eckmann - Skidmore College - CS 106 - Fall 2005
4
Terminology The body of a loop is --- the statements within the curly braces (or if it doesn't have a compound statement, the single statement is the body.) A control variable is one that is used in the condition of the loop to determine if the body should execute or if the loop should terminate. An iteration of a loop is one complete cycle through a loop while it is running. This includes checking the condition, changing the control variable, and the main body of the loop. A compound statement is 1 or more statements within curly braces. Michael Eckmann - Skidmore College - CS 330 - Fall 2005
5
for loops The for loop is a counter controlled repetition structure that compactly lets a programmer specify the control variable, its initial value, the condition to be checked each time through the loop and the amount to increment (or decrement) the control variable for ( expression1; expression2; expression3 ) statement or compound statement Michael Eckmann - Skidmore College - CS 106 - Spring 2007
6
for loops for ( expression1; expression2; expression3 ) statement or compound statement where expression1 contains the declaration and initialization of the control variable expression2 contains the continuation condition and the ending value of the control variable expression3 contains the increment or decrement of the control variable (and the amount of this change in value each time through the loop) Michael Eckmann - Skidmore College - CS 106 - Spring 2007
7
for loops for ( expression1; expression2; expression3 ) { statements } Order of operations: 1. expression1 executes first (and only once) 2. expression2 executes and if the result is false, program control goes to the code after the right curly brace 3. if the result is true, then the statements within the curly braces of the for loop execute in order then expression3 executes (which usually alters the value of the loop variable.) Then “goto” 2 above. Michael Eckmann - Skidmore College - CS 106 - Fall 2004 1 2 3 4 (if 2 was true)
8
for loop example for ( int x = 10; x >= 1; x-- ) { do some statements here.... } note that we used x-- to subtract 1 from the counter each time through the loop, we could have used: x = x - 1 or x -= 1 or --x Michael Eckmann - Skidmore College - CS 106 - Spring 2007
9
for loop Each of the three expressions in the for loop structure are optional. don’t need expression1 if control variable declared and initialized before the loop don’t need expression2 if you desire having an infinite loop don’t need expression3 if you change the value of the control variable within the loop Michael Eckmann - Skidmore College - CS 106 - Spring 2007
10
for loop for ( int cntr = 1; cntr <= 20; cntr = cntr + 1 ) { do some statements here.... } this is essentially the same thing as the following while loop int cntr = 1; while ( cntr <= 20 ) { do some statements here.... cntr = cntr + 1; } except, the variable cntr is not available to the program after the for loop, but it is available to be referenced after the while loop Michael Eckmann - Skidmore College - CS 106 - Spring 2007
11
exercise Write an application that calculates the product of the odd integers from 1 to 15 and then displays the results in a message dialog. Let's write it using a while loop Then let's write it using a for loop Michael Eckmann - Skidmore College - CS 106 - Spring 2007
12
do while loops While loops and for loops all have the possibility of having the statements inside them execute zero or more times. Zero being the key word here. When would it be possible for those loops to execute zero times? The big difference between those kinds of loops and do- while loops are that do-while loops execute their statements at least once (that is, one or more times.) Michael Eckmann - Skidmore College - CS 106 - Fall 2005
13
do while loops do { statements to do } while (condition); –In this kind of loop, the condition is tested after the loop executes the first time. If the condition is true it does the statements again, and so on until the condition is false. Michael Eckmann - Skidmore College - CS 106 - Fall 2005
14
do while loops do { statements to do } while (condition); –In this kind of loop, the condition is tested after the loop executes the first time. If the condition is true it does the statements again, and so on until the condition is false. Michael Eckmann - Skidmore College - CS 106 - Fall 2005 1 2,3 (if 2 was true)
15
do while loops int cntr = 1; while ( cntr <= 20 ) { do some statements here.... cntr = cntr + 1; } // the above is basically equivalent to: int cntr = 1; do { do some statements here... cntr = cntr + 1; } while (cntr <= 20); Michael Eckmann - Skidmore College - CS 106 - Fall 2005
16
Nested loops The body of a loop can contain another loop. We say that the inner loop is nested inside (or within) the outer loop. It is important to understand that the inner loop executes fully (all its iterations) within each iteration of the outer loop. For example, if the inner loop of a nested loop always iterates 10 times and the outer loop always iterates 5 times --- the code within the inner loop (its body) will end up executing a total of 50 times. Michael Eckmann - Skidmore College - CS 330 - Fall 2005
17
Nested loop - what prints? public class Printing { public static void main(String args[]) { for ( int i = 1; i <= 10; i++) { for ( int j = 1; j<=5; j++) { System.out.print("@"); } System.out.println(); } Michael Eckmann - Skidmore College - CS 330 - Fall 2005
18
Nested loop - what prints? public class Printing2 { public static void main(String args[]) { int i = 1, j = 1; for ( ; i <= 10; i++) { for ( ; j<=5; j++) { System.out.print("@"); } System.out.println(); } Michael Eckmann - Skidmore College - CS 330 - Fall 2005
19
Nested loop - what prints? public class Printing3 { public static void main(String args[]) { int i = 1, j = 1; while (i <= 10) { while ( j<=5) { System.out.print("@"); j++; } System.out.println(); i++; } Michael Eckmann - Skidmore College - CS 330 - Fall 2005
20
Nested loop - what prints? public class Printing4 { public static void main(String args[]) { int i = 1; while (i <= 10) { int j = 1; while ( j<=5) { System.out.print("@"); j++; } System.out.println(); i++; } Michael Eckmann - Skidmore College - CS 330 - Fall 2005
21
Pay attention to control variable The lesson is, to pay attention to the control variables. Does the inner loop's control variable get reset each time through the outer loop?, etc. Michael Eckmann - Skidmore College - CS 330 - Fall 2005
22
More Nesting example on page 185 Michael Eckmann - Skidmore College - CS 330 - Fall 2005
23
break; We've seen how break; reacts in switch statements. Well break; acts similarly within the curly braces of a while loop, for loop or do-while loop. It terminates the loop and program control continues after the loop as if it ended normally. Michael Eckmann - Skidmore College - CS 106 - Fall 2005
24
break; int x = 0; while (x <= 10) { if (x == 5) break; System.out.println(“x = “ + x); x++; } // what's this code going to do? Michael Eckmann - Skidmore College - CS 106 - Fall 2005
25
continue; The continue; is a way to cause your loop to skip the rest of the current iteration of the loop and continue on to the next iteration. So, what it does is, it basically skips all the code in the body of the loop after it and then goes to the next iteration. It acts slightly differently in for loops vs. while or do-while loops. After the rest of the current iteration is skipped, for a for loop, the next thing that happens is the expression3 then expression2 is evaluated to determine if it should go again. Michael Eckmann - Skidmore College - CS 106 - Fall 2005
26
continue; However, continue; within a while or a do-while loop acts like: After the rest of the current iteration is skipped, the next thing that happens is the condition is tested to determine if it should go again. Michael Eckmann - Skidmore College - CS 106 - Fall 2005
27
continue; for (int x = 0; x <= 10; x++) { if (x == 5) continue; System.out.println(“x = “ + x); } // what's this code going to do? Michael Eckmann - Skidmore College - CS 106 - Fall 2005
28
continue; int x = 0; while (x <= 10) { if (x == 5) continue; System.out.println(“x = “ + x); x++; } // what's this code going to do? Michael Eckmann - Skidmore College - CS 106 - Fall 2005
29
break; and continue; break; is never absolutely necessary continue; is never absolutely necessary labelled version of these are also never absolutely necessary That is, any code you write in Java that contains a break or continue can be rewritten without them. Michael Eckmann - Skidmore College - CS 106 - Fall 2005
30
labeled break & continue statements labeled break statements within nested structures (while, for, do while or switch structure) –A label can be given to a block of code. A block of code is defined as code enclosed in a pair of curly braces. –The labeled break statement can specify the block to “jump out of.” Non-labeled break statements could only “jump out of” the immediately enclosed structure, whereas, labeled break statements can “jump totally out of” a set of nested structures. Michael Eckmann - Skidmore College - CS 330 - Fall 2005
31
labeled break & continue statements labeled continue statements within nested structures (while, for, or do while structure) –A label can be given to a structure. –The labeled continue statement causes execution to skip the code in the enclosing structures and continues with the next iteration of the structure with the specified label. Michael Eckmann - Skidmore College - CS 330 - Fall 2005
32
labeled break & continue statements Let’s look at an example of how to label a block and nest some loops so that we can do a labeled break; or a labeled continue; Michael Eckmann - Skidmore College - CS 330 - Fall 2005
33
exercise Write an application that keeps displaying in the command window the multiples of the integer 2, namely 2, 4, 8, 16, 32,... Your loop should not terminate (i.e. you should create an infinite loop.) discuss: –Condition –Why program stops or doesn’t stop Michael Eckmann - Skidmore College - CS 330 - Fall 2005
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.