Download presentation
Presentation is loading. Please wait.
Published byRoland Ford Modified over 8 years ago
1
Flow of Control (2) : Loops Clark Savage Turner, J.D., Ph.D. csturner@csc.calpoly.edu756-6133 Some lecture slides have been adapted from those developed by John Lewis and William Loftus to accompany by John Lewis and William Loftus to accompany D Java Software Solutions: Foundations of Program Design, Second Edition and by Mark Hutchenreuther for CSC-101 at Cal Poly, SLO. by Mark Hutchenreuther for CSC-101 at Cal Poly, SLO. D
2
CSC-101 Repetition Statements: Loops D Repetition statements allow us to execute a statement multiple times repetitively D They are often simply referred to as loops D Like conditional statements, they are controlled by boolean expressions D Java has three kinds of repetition statements: the while loop, the do loop, and the for loop D The programmer must choose the right kind of loop for the situation
3
CSC-101 Logic of a loop Logic of a while loop statementtrue condition evaluated false
4
CSC-101 The while Statement D The while statement has the following syntax: while ( condition ) statement; while is a reserved word If the condition is true, the statement is executed. Then the condition is evaluated again. The statement is executed repetitively until the condition becomes false.
5
CSC-101 The while Statement If the condition of a while statement is false initially, the statement is never executed D Thus, the body of a while loop will execute zero or more times D See Counter.java Counter.java D See Average.java Average.java D See WinPercentage.java WinPercentage.java
6
CSC-101 Logic of a loop Logic of a do loop statementtrue condition evaluated false
7
CSC-101 The do Statement D The do statement has the following syntax: do { statement; } while ( condition ) Uses both the do and whilereservedwords The statement is executed once initially, then the condition is evaluated The statement is repetitively executed until the condition becomes false
8
CSC-101 The do Statement D A do loop is similar to a while loop, except that here the condition is evaluated after the body of the loop is executed D Therefore the body of a do loop will execute at least one time D See Counter2.java Counter2.java D See ReverseNumber.java ReverseNumber.java
9
CSC-101 Comparing the and loops Comparing the while and do loops while loop do loop statementtrue condition evaluated false false statementtrue condition evaluated
10
CSC-101 Infinite Loops The body of a while loop must eventually make the condition false D If not, it is an infinite loop, which will execute until the user interrupts the program D These are a very common form of logical error You should double check to ensure that your loops will terminate normally You should double check to ensure that your loops will terminate normally You should not rely on a break; or continue; to exit from a loop: when tempted, find another way out You should not rely on a break; or continue; to exit from a loop: when tempted, find another way out See Forever.java for an example of this error. See Forever.java for an example of this error.Forever.java
11
CSC-101 Nested Loops D Similar to nested if statements, loops can be nested as well D That is, the body of a loop could contain another loop D Each time through the outer loop, the inner loop will go through its entire set of iterations D See PalindromeTester.java PalindromeTester.java
12
CSC-101 Logic of a loop Logic of a for loop statementtrue false increment initialization condition evaluated
13
CSC-101 The for Statement D The for statement has the following syntax: for ( initialization ; condition ; increment ) statement;Reservedword The initialization portion is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration
14
CSC-101 Comparing the and loops Comparing the while and for loops while loop for loop statementtrue condition evaluated false statementtrue false increment initialization condition evaluated
15
CSC-101 The for Statement D A for loop is equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; }
16
CSC-101 The for Statement D Like a while loop, the condition of a for statement is tested prior to executing the loop body D Therefore, the body of a for loop will execute zero or more times D It is well suited for executing a specific number of times a specific number of times that can be determined in advance that can be determined in advance D See Counter3.java Counter3.java D See Multiples.java Multiples.java D See Stars.java Stars.java
17
CSC-101 The for Statement D Each expression in the header of a for loop is optional If the initialization is left out, no initialization is performed If the initialization is left out, no initialization is performed If the condition is left out, it is always considered to be true, and therefore creates an infinite loop If the condition is left out, it is always considered to be true, and therefore creates an infinite loop If the increment is left out, no increment operation is performed If the increment is left out, no increment operation is performed D Both semi-colons are always required in the for loop header
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.