Java Reference
In-Depth Information
Fig. 5.29 | Drawing concentric circles.
5.2 Modify Exercise 5.16 from the end-of-chapter exercises to read input using dialogs and to
display the bar chart using rectangles of varying lengths.
5.12 Wrap-Up
In this chapter, we completed our introduction to control statements, which enable you to
control the flow of execution in methods. Chapter 4 discussed if , if else and while . This
chapter demonstrated for , do while and switch . We showed that any algorithm can be
developed using combinations of the sequence structure, the three types of selection state-
ments— if , if else and switch —and the three types of repetition statements— while ,
do while and for . In this chapter and Chapter 4, we discussed how you can combine these
building blocks to utilize proven program-construction and problem-solving techniques.
You used the break statement to exit a switch statement and to immediately terminate a
loop, and used a continue statement to terminate a loop's current iteration and proceed with
the loop's next iteration. This chapter also introduced Java's logical operators, which enable
you to use more complex conditional expressions in control statements. In Chapter 6, we ex-
amine methods in greater depth.
Summary
Section 5.2 Essentials of Counter-Controlled Repetition
• Counter-controlled repetition (p. 153) requires a control variable, the initial value of the control
variable, the increment by which the control variable is modified each time through the loop (al-
so known as each iteration of the loop) and the loop-continuation condition that determines
whether looping should continue.
• You can declare a variable and initialize it in the same statement.
Section 5.3 for Repetition Statement
•The while statement can be used to implement any counter-controlled loop.
•The for statement (p. 155) specifies all the details of counter-controlled repetition in its header
•When the for statement begins executing, its control variable is declared and initialized. If the
loop-continuation condition is initially true, the body executes. After executing the loop's body,
the increment expression executes. Then the loop-continuation test is performed again to deter-
mine whether the program should continue with the next iteration of the loop.
 
 
Search WWH ::




Custom Search