Page 491 - Introduction to Programming with Java: A Problem Solving Approach
P. 491

                Figure 11.10 DayTrader program that illustrates use of the break statement
the reader to look inside of the loop for loop termination conditions. And that makes your program harder to understand. Nonetheless, in certain situations, the break statement improves readability rather than hinders it. The DayTrader program’s break statement is an example where the break statement improves readability.
11.12 for Loop Header Details
This section supplements the for loop material you studied in Chapter 4, Section 4.10.
Omitting One or More of the for Loop Header Components
It’s legal, although not all that common, to omit the first and/or third components in the for loop header. For example, to print a countdown from a user-entered number, you could do this:
11.12 for Loop Header Details 457
 /*****************************************************************
*
DayTrader.java
Dean & Dean
This simulates stock market day trading.
*****************************************************************/
*
*
*
public class DayTrader
{
}
// end DayTrader
public static void main(String[] args)
{
}
// end main
double balance = 1000.00; // money that’s retained
double moneyInvested;
double moneyReturned;
int day;
// money that’s invested
// money that’s earned at end of day
// current day, ranges from 1 to 90
for (day=1; day<=90; day++)
{
}
moneyReturAnpeda=gmoneyPInDveFstedEn* h(Matnh.craendrom() * 2.0); balance += moneyReturned;
// end for
if (balance < 1.0 || balance > 5000.0)
{
}
break;
balance = moneyInvested = balance / 2.0;
System.out.printf("final balance on day %d: $%4.2f\n",
(day - 1), balance);
 























































   489   490   491   492   493