Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 106 - Spring 2008 Today’s Topics Comments and/or Questions? Loops for repeated execution –for loops –do-while loops –break; and continue; in loops –nested loops

3 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 2008

4 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 2008

5 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 - Spring 2008 1 2 3 4 (if 2 was true)‏

6 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 2008

7 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 2008

8 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 2008

9 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 2008

10 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 - Spring 2008

11 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 - Spring 2008

12 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 - Spring 2008 1 2,3 (if 2 was true)‏

13 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 - Spring 2008

14 break; We've seen how break; reacts in switch statements. 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 - Spring 2008

15 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 - Spring 2008

16 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, it basically skips all the code 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 - Spring 2008

17 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 - Spring 2008

18 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 - Spring 2008

19 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 - Spring 2008

20 exercise Write an application that keeps displaying in the command window the powers 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 106 - Spring 2008

21 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 106 - Spring 2008

22 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 106 - Spring 2008

23 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 106 - Spring 2008

24 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 106 - Spring 2008

25 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 106 - Spring 2008

26 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 106 - Spring 2008


Download ppt "CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann."

Similar presentations


Ads by Google