BFOIT - Introduction to Computer Programming

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>   

  1. the statement keyword: "for"
  2. a "(",
  3. initialization code,
  4. a ";",
  5. a boolean expression,
  6. a ";",
  7. update code,
  8. a ")", and
  9. 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:

  1. the variable's type,
  2. the variable's identifier,
  3. an assignment operator "=", and
  4. 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>   

  1. the statement keyword: "if"
  2. a "(",
  3. a boolean expression,
  4. a ")", and
  5. a statement or a block of statements.

Summary


Back to Java TurtleGraphics
Go to the Table of Contents
On to Java User Interface Events

Public Domain Mark
This work (BFOIT: Introduction to Computer Programming, by Guy M. Haas),
identified by Berkeley Foundation for Opportunities in IT (BFOIT),
is free of known copyright restrictions.