Switch expression

Lets make a menu for an editor program. The switch expression is often used to create menus. You will need:
  • a while loop to keep asking for choices, and
  • a switch-expression to branch execution on the choices. One choice will end the program.

Go to http://haxe.org/ref/syntax and look under the Switch heading. Its syntax is:

switch( variable ) {

case constant1:

case constant2, constant3:


Lets write our menu for the editor in Haxe.

class Menu
  public static function main()
    // menu choice entered by user
    var choice : String = "";
    while( 1 == 1 )
      Sys.print( "Editor Menu\n\n" );
      Sys.print( " n - Create New Document\n" );
      Sys.print( " o - Open Existing Document\n" );
      Sys.print( " s - Save Current Document\n" );
      Sys.print( " p - Print Current Document\n" );
      Sys.print( " e - Exit Editor\n\n" );
      Sys.print( "Enter your choice: " );
      choice = Sys.stdin().readLine();
      switch( choice )
        case "n":
          // function to create new document
          printmsg( "create new document.\n\n");
        case "o":
          // function to open existing document
          printmsg( "open an existing document.\n\n" );
        case "s":
          // function to save current document
          printmsg( "save current document.\n\n" );
        case "p":
          // function to print current document
          printmsg( "print current document.\n\n" );
        case "e":
          // end program
          printmsg( "to end the program.\n\n" );
          Sys.print( "Not a valid choice. Please type one of: n o s p e.\n\n" );
      } // switch
    } // while
  } // main
  static function printmsg( msg : String ) : Void
    Sys.print( "You chose " + msg );
    Sys.sleep( 2 );
} // class

The output of the program is (after choosing "e"):
Editor Menu
 n - Create New Document
 o - Open Existing Document
 s - Save Current Document
 p - Print Current Document
 e - Exit Editor
Enter your choice: e

What Happened?

  1. We initialise a string variable named choice to the empty string in line 4.
  2. We set up the while-loop in line 6 and the expr-cond (1 == 1) makes it an infinite loop.
  3. We print the menu text on screen in lines 7 to 13. Note that we use Sys.print() and newlines (\n) so we can control the line spacing. Note there is no newline (\n) at the end of the text in line 13 - we want the user's choice to appear on the same line as the menu prompt.
  4. We get the user's choice in line 14. Go to http://haxe.org/doc/start/neko to look at Reading User Input.
  5. We set up the switch-expression in lines 16 to 34. We use the value of the choice variable to control the case selection.
  6. If the user enters "n", then the first case is selected in line 17. Our cases could allow for the user typing in either lowercase or uppercase with code like case "n", "N":. We would execute a "new document" function in a real editor. For now, we will just call a function (printmsg()) to print a message on screen to say which menu option was chosen.
  7. In a similar fashion, we handle the cases of "o", "s" and "p" in lines 20 to 28.
  8. The "e" case ends the while-loop with a break after the function call in line 32. Since there is no more code in main() after the loop ends in line 36, the program also ends.
  9. The default case starting in line 33 handles all cases where the user does not enter one of the acceptable choices. It shows them the valid choices and re-displays the menu. By convention, the default case in a switch expression is shown last.
  10. The comments after the braces in lines 35 to 37 are sometimes useful for debugging if the program code is longer than one screenful.
  11. The printmsg function is defined in lines 39 to 42. See Functions for further details. Its argument (msg) is a String and it does not return any value (Void). It prints a line of text made by adding "You chose " before its argument, waits 2 seconds so the user can type their new document, etc and then returns.

Extra Notes

Switches are a way to express multiple if... else if... else if tests on the same value.

Switches in Haxe are different from traditional switches: all cases are separate expressions so after one case expression is executed, the switch block is automatically exited. As a consequence, break cannot be used in a switch and the position of the default case is not important.

A case expression like "case 65, 90:" will match either of the two (or several) values, listed as delimited by commas.

Where To From Here?

This section of the wiki on Flow of Control is finished. We have covered the use of the following keywords:
  • if
  • while and do-while
  • for - in
  • switch - case - default