Flow of Control

A computer program is a set of instructions for the computer to execute. The program starts at the entry point (in Haxe, the main() function) and continues until a final instruction is executed.

Flow of control allows two things to happen in a computer program.
  • The program can "make a decision" based on the value of one or more variables and branch to execute a block of instructions. This value can be set by a program variable, from user input or from the computer hardware.
  • The program can repeat a block of instructions.

Flow of control changes the computer from being an oversized calculator into a "smart" machine.

Flow of Control Keywords

The list of Haxe keywords can be found at http://haxe.org/ref/keywords. The keywords used for flow of control are:
  • if - else
  • while
  • do - while
  • for - in
  • switch - case - default
  • break
  • continue

If-expression

In real life, we might say "If I have a given number, tell me is it even or odd".

How would a Haxe program do this?
We can use the modulo operator to find even numbers. Its syntax is at http://haxe.org/ref/syntax under Operations. When we divide a number by 2, it is even if its remainder is 0 and it is odd if its remainder is 1.

Go to http://haxe.org/ref/syntax and read the notes on If. Its syntax is:

if( expr-cond ) expr-1 [else expr-2]

So, how do we use it in a program?
class Example
{
  public static function main()
  {
    var number = 4;
    // use % (modulo) to find even numbers
    if( number % 2 == 0 )
      trace( "number is even" );
    else
      trace( "number is odd" );
 
    trace( "end of program" );
  }
 
}

The output of the program is:
Example.hx:5: number is even
 
 
Example.hx:7: end of program

What Happened? What Next?

  1. The variable number is assigned an even value (4) in line 3.
  2. number % 2 == 0 evaluates to true, so execution branches to expr-1 (which happens to be the next expression anyway) and "number is even" is output.
  3. Program execution continues after the if-expression, so line 6 is skipped and "end of program" is output.
  4. Comment out the "else expr-2" line, run the program again and see what happens. No difference?
  5. Uncomment line 6 again, then change line 3 to "var number = 5". Run the program again. Is that what you expected?
  6. To do: change the expr-cond to (number % 2 == 1) and rewrite the program so you get the original output.

Multiple If-expressions

It is possible to have a chain of if-expressions. An example from user input would look like this:
if (event.keyCode == 37)
  trace( "left arrow pressed" );
 
else if (event.keyCode == 38)
  trace( "up arrow pressed" );
 
else if (event.keyCode == 39)
  trace( "right arrow pressed" );
 
else if (event.keyCode == 40)
  trace( "down arrow pressed" );
In this block of code, only one of the four outputs will be printed when an arrow key is pressed.

The if-expression allows the computer to execute different branches of instructions depending on the value of expr-cond. It is one of the most widely used keywords in computer programs.

Where To From Here?

Remember the other thing that flow control does? The program can repeat a block of instructions. How to do this using loops is coming up next.