4.11. Chapter Summary

 
[Page 118 ( continued )]

Chapter Summary

  • Program control specifies the order in which statements are executed in a program. There are three types of control statements: sequence, selection, and repetition. The preceding chapters introduced sequence and selection statements. This chapter introduced repetition statements.

  • There are three types of repetition statements: the while loop, the do-while loop, and the for loop. In designing loops , you need to consider both the loop control structure and the loop body.

  • The while loop checks the loop-continuation-condition first. If the condition is true , the loop body is executed; if it is false , the loop terminates. The do-while loop is similar to the while loop, except that the do-while loop executes the loop body first and then checks the loop-continuation-condition to decide whether to continue or to terminate.

  • Since the while loop and the do-while loop contain the loop-continuation-condition , which is dependent on the loop body, the number of repetitions is determined by the loop body. The while loop and the do-while loop are often used when the number of repetitions is unspecified.


    [Page 119]
  • The for loop is generally used to execute a loop body a predictable number of times; this number is not determined by the loop body. The loop control has three parts . The first part is an initial action that often initializes a control variable. The second part, the loop-continuation-condition , determines whether the loop body is to be executed. The third part is executed after each iteration and is often used to adjust the control variable. Usually, the loop control variables are initialized and changed in the control structure.

  • Two keywords, break and continue , can be used in a loop. The keyword break immediately ends the innermost loop, which contains the break. The keyword continue only ends the current iteration.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net