Background
jLogo Programming
- Commanding a Turtle
- Pseudocode
- Adding New Commands
- Iteration & Animation
- Hierarchical Structure
- Procedure Inputs
- Operators & Expressions
- Defining Operators
- Words & Sentences
- User Interface Events
- What If? (Predicates)
- Recursion
- Local Variables
- Global Variables
- Word/Sentence Iteration
- Mastermind Project
- Turtles As Actors
- Arrays
- File Input/Output
Java
- A Java Program
- What's a Class?
- Extending Existing Classes
- Types
- Turtle Graphics
- Control Flow
- User Interface Events
Appendices
- Jargon
- What Is TG?
- TG Directives
- jLogo Primitives
- TG Editor
- Java Tables
- Example Programs
- *** New ***:
Installation Notes
Updates
- December 13, 2008
- January 6, 2012
- March 15, 2013
- January 20, 2014
- February 13, 2014
- July 29, 2014
- January 18, 2016
- January 29, 2016
- August 19, 2016
Lastly
Java Control Flow
Introduction
Control flow refers to how execution of a program proceeds when it is executing. Early on in the Logo lessons, I pointed out that instructions were executed left-to-right, top-to-bottom. Then I introduced the REPEAT command 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.
Java's for Statement
In the Logo lesson where REPEAT was introduced, there was an example of how to draw a circle. Example 25.1 shows this code and Example 25.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 25.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 25.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> |
- the statement keyword: "for"
- a "(",
- initialization code,
- a ";",
- a boolean expression,
- a ";",
- update code,
- a ")", and
- a statement or a block of statements.
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:
- the variable's type,
- the variable's identifier,
- an assignment operator "=", and
- the variable's initial value.
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
Although recursion is the best way to approach iteration in some problems, 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 25.3 |
Rotated Boxes
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 25.4 |
Conditional Execution Via Java's if Statement
The syntax of the Java if statement is:
if | ( | <Expression> | ) | <Statement> |
- the statement keyword: "if"
- a "(",
- a boolean expression,
- a ")", and
- a statement or a block of statements.
Summary
Go to the Table of Contents
On to Java User Interface Events