Java Reference
In-Depth Information
for (int i = 1; i <= 10; i++) . . .
for (int i = 0; i < str.length(); i++) . . .
The next two need to be implemented as while loopsȌyou don't know how
many times the loop body is going to be repeated.
250
251
Step 3 With a while loop, find out where you can determine that the loop is
finished.
There are three possibilities:
ȗ
Before entering the loop
ȗ
In the middle of the loop
ȗ
At the end of the loop
For example, if you execute a loop while the balance is less than the target
balance, you can check for that condition at the beginning of the loop. If the
balance is less than the target balance, you enter the loop. If not, you are done. In
such a case, your loop has the form
while (condition)
{
Do work
}
However, checking for input requires that you first read the input. That means,
you'll need to enter the loop, read the input, and then decide whether you want to
go any further. Then your loop has the form
boolean done = false;
while (!done)
{
Do the work needed to check the condition
if (condition)
done = true;
else
{
Do more work
}
}
Search WWH ::




Custom Search