Published online by Cambridge University Press: 26 August 2009
OBJECTIVES
In this chapter you will study:
while loop;
do loop;
for loop;
Nested loops;
break statement; and
continue statement.
This chapter shows you how to write Java loops. You will learn how to write loops that mirror the familiar COBOL PERFORM statement including the PERFORM—UNTIL, PERFORM—VARYING—UNTIL and PERFORM—VARYING—UNTIL—AFTER. You will see that Java has three different types of loops: while, do, and for. We will use each of these to duplicate the work done by the COBOL PERFORM statement. In addition, we will review loops that test for the terminating condition at the beginning and at the end of the loop. We will also demonstrate writing nested loops in Java.
The chapter begins with the simple COBOL PERFORM—UNTIL statement and shows you how to accomplish the same thing in Java. Then we work with the PERFORM—VARYING—UNTIL and finally the PERFORM—VARYING—UNTIL—AFTER statement. Working programs are developed to illustrate the Java loop statements in action. At the end of the chapter, we design and develop a small program for the Community Naitonal Bank to compute a loan amortization, using some of the Java looping statements.
This chapter assumes you understand the following:
COBOL:
Perform-until
Inline perform (COBOL-85)
Perform-varying-until
Perform-varying-until-after
With test after (COBOL-85)
Java:
OO concepts (Chapter 2)
Java program structure (Chapter 3)
Defining data (Chapter 4)
Arithmetic (Chapter 5)
Decision making (Chapter 6)
LOOP STRUCTURE
We write program loops to repeat a sequence of instructions.
To save this book to your Kindle, first ensure no-reply@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
Find out more about the Kindle Personal Document Service.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.