This is my web page for the year. We are required to do one per year in Mr.
Young’s Intro to JAVA computer programming class. To visit Mr. Young’s JAVA web page, click the fishy at the bottom
of the page. This page has a lot of info, and cool stuff to do and see on
it. We are using the Computing
Concepts with JAVA Essentials by Cay Horstmann this year. Loops are covered in chapter 6; so if you
would like to download them, click here. I’ll be using
some of them in this web page.
I’m doing it on loops (see big title). Loops are mainly ways to make a JAVA program
repeat many times till you get a certain result. In JAVA, there are while, do, and for loops. You use each one at different times, and
they each have different context. JAVA
loops are used everywhere. ATM machines
are an example of a loop. Then ask the same questions to every one; make sure
they have as much money as they wish to withdraw, and then the wait for the
next person.
Let’s start with while
loops. A while statement (aka loop)
executes a block of code repeatedly. A
termination condition controls how often the loop is executed. Basically it means that it repeats till a
one of the conditions is proven false. The basic while loop is:
while (condition)
statement
And example of a while loop would be:
while (balance
< targetBalance)
{
years++
double
interest = balance * rate / 100;
balance =
balance + interest;
}
This broken down means: if the
balance is less then the target balance, each year add the balance and the
interest, and repeat again, till the balance is greater than the
targetBalance. This is probably the
most common loop used. This loop is
part of a program about investment. Get
the program here and the test here, and put it
in your own complier to see how it works.
The next kind of loop is the do loop. It is like the while loop, only the loop is
tested after it has already been executed.
This way it is executed at least once.
And so after you use it, and it comes back false, then the program would
quit. The basic do loop is:
do
statement
while (condition);
And
an example of this loop would be:
do
{
String input = JOptionPane. show InputDialog(
“Please enter a positive number”);
value = Double.parseDouble(input);
}
while (value <= 0);
All
these little things are is that a box pops up, and asks you to type in a
positive number. You do it once, and if
it is less then or equal to zero, then the program quits. Otherwise it will ask you to enter another
number again. These are a little more
complicated, and they aren’t used very much, because you can turn them into
while loops.
The final kind of loop is the
for loop. It is used when you want to
have something run constantly from a starting value to an ending value with a
constant increase or decrease. This
loop really is rewriting a very common while loop. A for loop in while loop form would be:
i = start;
while (i <= end)
{
. . .
i++;
}
This
is probably the most common form of a while loop, where "i" is a
variable, and the “. . .” is the statement.
Moving this into a for loop is real easy. The basic for loop is:
for (i = start; i <= end; i++)
{
. . .
}
Most
while loops can be written into for loops, which makes them easier to
understand. An example of a for loop
when it is all filled in could be:
for (int i = 1; i <= n;
i++)
{
double interest
= balance * rate / 100;
balance =
balance + interest;
}
Recognize
this loop? It’s the same one from the
while loop, written to make it easier to understand. So you could take the
first program and rewrite it using a for loop instead of a while loop.
Now that you know all the
different types of loops, there is one more thing you can do with them. You can “nest” them. Nested loops are when two loops are
dependent on each other, and a shorter way to not have to write a whole lot of
else/if statements. An example of one
would be if you wanted a program that created this design:
[]
[]
[]
[]
[] []
You
could write a whole lot of nested loops.
The code for this program can be found here, and the test here. You can copy it into your
complier and run it.
Other sites with information on
loops:
JAVA loop 3{pfd format}
And then Mr. Young’s JAVA web
page, just click on the fishy!