Introduction
to Programming
Java
Control Flow
REPEAT instruction and you
had your first exception - instructions surrounded by square
brackets would be executed some number of times before execution
continued left-to-right, top-to-bottom. A few lessons later
I introduced the IF instruction, another exception.
In this case, instructions surrounded by square brackets
may or may-not get executed. And, then came recursion...
It's time to look at Java's control flow.
for Statement
REPEAT was introduced,
there was an example of
how to draw a circle. Example 21.1 shows this code
and Example 21.2 is the same program written in Java.
Compare the two.
to pointOut100
setpensize 30
penup forward 85
pendown forward 30
penup back 115
end
to main
clean
repeat 18 [ pointOut100 right 20 ]
end
main
|
| Example 21.1 |
class PointsCircle extends TurtleGraphicsWindow
{
void pointOut100()
{
setpensize( 30 );
penup();
forward( 85 );
pendown();
forward( 30 );
penup();
back( 115 );
} // end pointOut100()
void drawCircle()
{
for ( int count=18; count > 0; count-- )
{
pointOut100();
right(20);
}
} // end drawCircle()
public static void main(String[] args)
{
PointsCircle me = new PointsCircle();
me.clean();
me.drawCircle();
}
} // end class PointsCircle
|
| Example 21.2 |
Here are the nitty-gritty details of one way of repeating something in Java. The syntax of the Java for statement is:
for |
( | <ForInit> | ; | <Expression> | ; | <ForUpdate> | ) | <Statement> |
for" The syntax of a <ForInit> in general is much more complex than what I want to explain here; in this particular example you have a local variable declaration consisting of:
Although the syntax for <Expression> is also very
general, in this particular example I compare a local variable
to a int literal. It is very common for the
expression to be a comparison of the local variable declared in
<ForInit> with a value. I use the
greater-than operator.
Other relational operators, as well as equality and logical
operators are described here.
And finally there is the <ForUpdate> piece. Like <ForInit>, this can be more complex than I care to explain, but it is most often a statement that changes the value of the local variable declared in <ForInit>. I use the <PostDecrement> operator which decreases the value in the variable by 1.
for Sometimes Can Be Used in Place of Recursion
for can be used in its place in
some cases. Do you remember squiral from the
first lesson on recursion?
Here's a version of it in Java using for.
class Squiral extends TurtleGraphicsWindow
{
void drawSquiral()
{
for ( int steps=5; steps <= 200; steps += 5 )
{
forward( steps );
right(90);
}
} // end drawSquiral()
public static void main(String[] args)
{
Squiral me = new Squiral();
me.clean();
me.drawSquiral();
}
} // end class Squiral
|
| Example 21.3 |
class RotatedBoxes extends TurtleGraphicsWindow
{
void box( int side )
{
for ( int i=4; i > 0; i-- )
{
forward(side);
right(90);
}
} // end drawSomething()
void drawSomething()
{
for ( int i=12; i > 0; i-- )
{
box(100);
right(30);
}
right(15);
pu();
for ( int i=12; i > 0; i-- )
{
forward(70);
setpencolor(i);
fill();
back(70);
right(30);
}
} // end drawSomething()
public static void main(String[] args)
{
RotatedBoxes me = new RotatedBoxes();
me.drawSomething();
}
} // end class RotatedBoxes
|
| Example 21.4 |
if Statement
The syntax of the Java
if statement is:
if |
( | <Expression> | ) | <Statement> |
if"