Download presentation
Presentation is loading. Please wait.
1
Lecture 6: for Loops Yoni Fridman 7/6/01 7/6/01
2
OutlineOutline The for statement The for statement’s format The for statement’s flow Comparing while, do, and for ä Loop examples ä Debugging control structures The for statement The for statement’s format The for statement’s flow Comparing while, do, and for ä Loop examples ä Debugging control structures
3
The for Statement Remember this while loop? ä int i = 1; while (i <= 5) { System.out.println(i); System.out.println(i); i++; i++;} ä It can be described as follows: ä initialization while (test) { statement statement update update} The for loop condenses this: ä for (initialization; test; update) statement statement Remember this while loop? ä int i = 1; while (i <= 5) { System.out.println(i); System.out.println(i); i++; i++;} ä It can be described as follows: ä initialization while (test) { statement statement update update} The for loop condenses this: ä for (initialization; test; update) statement statement
4
The for Statement’s Format for (initialization; test; update) statement initialization is performed once before the loop starts. It initial- izes the counter. statement is any Java statement. It’s executed until test becomes false. test is the condition being tested. It’s result must be boolean. update is performed at the end of each iteration, to update the counter. Intialization, test, and update must be separated by semicolons.
5
for (initialization; test; update) statement The for Statement’s Flow Start test true End test false
6
The for Statement (Side Notes) The for Statement (Side Notes) ä The initialization, test, and update need not all operate on the counter variable. ä The initialization and update need not have one statement. ä They can be empty (have no statements), in which case they must be performed elsewhere. The semicolons must still be included. ä They can have multiple statements, separated by commas. ä The test can be empty – it defaults to true. In this case, the loop must have a break, or it will iterate forever. ä The initialization need not declare the counter. IMPORTANT: Variables CAN be redeclared within for statements. ä The initialization, test, and update need not all operate on the counter variable. ä The initialization and update need not have one statement. ä They can be empty (have no statements), in which case they must be performed elsewhere. The semicolons must still be included. ä They can have multiple statements, separated by commas. ä The test can be empty – it defaults to true. In this case, the loop must have a break, or it will iterate forever. ä The initialization need not declare the counter. IMPORTANT: Variables CAN be redeclared within for statements.
7
Comparing while, do, and for Why use for instead of while ? Same old reasons: ä It’s shorter. ä It makes the program more readable – all the vital information (initialization, test, and update) can easily be found in one place. ä So when do you use which loop? for is used in loops with counters. while is used in loops without counters. do is used in loops without counters, when you must guarantee at least one iteration. Why use for instead of while ? Same old reasons: ä It’s shorter. ä It makes the program more readable – all the vital information (initialization, test, and update) can easily be found in one place. ä So when do you use which loop? for is used in loops with counters. while is used in loops without counters. do is used in loops without counters, when you must guarantee at least one iteration.
8
Loop Examples Powers of 2 ( while loop) ä ä Factorials ( for loop) ä ä Fibonacci numbers ( for loop) ä ä Powers of 2 ( while loop) ä ä Factorials ( for loop) ä ä Fibonacci numbers ( for loop) ä ä
9
Debugging Control Structures ä Debugging becomes more complicated once control structures have been introduces. ä When testing your program, test the program on various input data. ä Make sure each statement of your code is executed at least once. ä Put println() statements within control structures. ä Common errors to watch out for: “Off-by-one” errors, for example, using i < n instead of i <= n. ä Infinite loops. Never-executed loops or if statements. ä Using the debugger: An example. ä Debugging becomes more complicated once control structures have been introduces. ä When testing your program, test the program on various input data. ä Make sure each statement of your code is executed at least once. ä Put println() statements within control structures. ä Common errors to watch out for: “Off-by-one” errors, for example, using i < n instead of i <= n. ä Infinite loops. Never-executed loops or if statements. ä Using the debugger: An example.
10
HomeworkHomework ä Read: 5.1, 5.2 (bottom of p. 193 to bottom of p. 194), 5.3, 5.4. ä HW3 out today, due Wednesday. ä Black Jack. ä Read: 5.1, 5.2 (bottom of p. 193 to bottom of p. 194), 5.3, 5.4. ä HW3 out today, due Wednesday. ä Black Jack.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.