prev | TOC | next

Flow of Execution

files: FlowDemo.java, FlowExercise.java (help)


Programs run by sequentially executing a series of instructions.   When we talk about a program's "flow of execution," we are discussing:

  • How it starts
  • The order in which it executes its instructions
  • How it terminates

Download the FlowDemo turtle by dragging the link at the top of the page to the Class List.

1. import com.otherwise.jurtle.*;
2.
3. public class FlowDemo extends Turtle
4. {
5.
6.   
7.     public void runTurtle()
8.     {
9.         Console.println("Here I am");
10.        goThere();
11.        Console.println("Now I'm here again");
12.    }
13.  
14.  
15.    private void goThere()
16.    {
17.        Console.println("Now I'm there");
18.      
19.    }
20.
21.
22. }

If  you haven't done so, run this program by selecting the Turtle in the list on the left and click the green Run button.  Here's the program's output.

Execution started...

Here I am
Now I'm there
Now I'm here again

Execution finished.

How it starts

With any program there's an entry point.  This is the piece of code in your program that is first executed.  When you are creating Turtles to run within Jurtle, the entry point is generally the runTurtle() method.  In the above program this is at line 7.  Every Turtle you create must have a runTurtle() method so it has a place to start.

Order of execution

Programs are constructed out of one or more statements.  Statements are the "sentences" of the Java language and are basically commands to do something.  Within a method, statements are generally executed in the order in which they appear, unless a statement tells the program to jump to somewhere else.  One such statement is a call (or jump) to another method.

When the FlowDemo program executes, the runTurtle() method is called by Jurtle.  The first statement in the method is line 9
   
Console.println("Here I am");

This statement prints the string of characters "Here I am" on the Console.  Looking at the program output this is also the first thing printed after the "Execution started..." message.  The next command is

goThere();

This is a method call that causes the program to jump to the goThere() method which is defined right under the runTurtle() method.  The first statement in this method is line 17

Console.println("Now I'm there");

which causes the string of characters "Now I'm there" to be printed out.

After the line 17 executes, there are no more statements in the goThere() method so the program execution jumps back to runTurtle() at line 11, which is the statement following the call to goThere().  This line:

Console.println("Now I'm here again");

causes its message to be written to the Console panel.

How it terminates

After line 11, there are no more statements in the runTurtle() method.  This causes the program to return to whatever called it, which in this case is some code within the Jurtle application itself.  This code is responsible for printing

Execution finished.

on the console.  The Turtle is then done.

Summary of flow of execution

The diagram below indicates the flow of execution through the FlowDemo program starting with the Jurtle system.

             

Exercises

Download the FlowExercise turtle using the link at the top of this lesson.  Before running the turtle, see if you can write down what it will print on the Console.  Now run the turtle and see if you are correct.  If not, go back and figure out why it printed in the order it did.