For example, let's say that I ask you to draw the following figure:
![]() |
A good place to start would be to draw the horizontal lines, like this:
![]() |
One way to draw the lines would be to create a sequential block of code:
import java.awt.Graphics;
public class FirstApplet extends java.applet.Applet
{
public void paint(Graphics g)
{
int y;
y = 10;
g.drawLine(10, y, 210, y);
y = y + 25;
g.drawLine(10, y, 210, y);
y = y + 25;
g.drawLine(10, y, 210, y);
y = y + 25;
g.drawLine(10, y, 210, y);
y = y + 25;
g.drawLine(10, y, 210, y);
y = y + 25;
g.drawLine(10, y, 210, y);
y = y + 25;
g.drawLine(10, y, 210, y);
y = y + 25;
g.drawLine(10, y, 210, y);
y = y + 25;
g.drawLine(10, y, 210, y);
}
}
(For some new programmers, the statement "y = y + 25;" looks odd the first time they see it. What it means is, "Take the value currently in the variable y, add 25 to it and place the result back into the variable y." So if y contains 10 before the line is executed, it will contain 35 immediately after the line is executed.)
Most people who look at this code immediately notice that it contains the same two lines repeated over and over. In this particular case the repetition is not so bad, but you can imagine that if you wanted to create a grid with thousands of rows and columns, this approach would make program-writing very tiring. The solution to this problem is a loop, as shown below:
import java.awt.Graphics;
public class FirstApplet extends java.applet.Applet
{
public void paint(Graphics g)
{
int y;
y = 10;
while (y <= 210)
{
g.drawLine(10, y, 210, y);
y = y + 25;
}
}
}
When you run this program, you will see that it draws nine horizontal lines 200 pixels long.
The while statement is a looping statement in Java. The statement tells Java to behave in the following way: At the while statement, Java looks at the expression in the parentheses and asks, "Is y less than or equal to 210?"
We can complete our grid by adding a second loop to the program, like this:
import java.awt.Graphics;
public class FirstApplet extends java.applet.Applet
{
public void paint(Graphics g)
{
int x, y;
y = 10;
while (y <= 210)
{
g.drawLine(10, y, 210, y);
y = y + 25;
}
x = 10;
while (x <= 210)
{
g.drawLine(x, 10, x, 210);
x = x + 25;
}
}
}
You can see that a while statement has three parts:
Java supports another way of doing the same thing that is a little more compact than a while statement. It is called a for statement. If you have a while statement that looks like this:
y = 10;
while (y <= 210)
{
g.drawLine(10, y, 210, y);
y = y + 25;
}
then the equivalent for statement looks like this:
for (y = 10; y <= 210; y = y + 25)
{
g.drawLine(10, y, 210, y);
}
You can see that all the for statement does is condense the initialization, evaluation and incrementing lines into a short, single line. It simply shortens the programs you write, nothing more.
While we are here, two quick points about loops:
To get some practice with looping, try writing programs to draw the following figures:
![]() |
For lots more information on Java and other computer programming languages, check out the links on the next page.
More Options: