Skip to main content

Controlling Execution Flow

In order to control execution flow, there must be a way to:

  • Conditionally run or bypass certain sections of code.

  • Cause certain sections of code to run repeatedly.

  • Trap runtime errors, and run error handling code.

As you've already learned, the If construct is the traditional means of evaluating conditions. Next, you'll learn about alternatives to the If construct, several ways to repeat code, and how to handle runtime errors.

You use If to run or bypass code blocks containing multiple commands. There is a simpler alternative if you want to apply a condition to a single command: post-conditional commands. All of the commands you've learned so far (except for If) may be followed by a colon and a condition, which may be simple or as complex as you require. The command only runs if the condition is true.

In order to allow spaces within the post-condition, enclose it in parentheses.

You can replace these If statements:

    if (name = "") {quit}
    if (x = 1) { set y = 2 }  

with these post-conditionals:

    quit:(name = "")
    set:(x = 1) y = 2                         

In the If below, there are 2 commands that follow the If:

    if (name = "") {write "...Exiting" quit}

For multiple commands, always use If instead of the post-conditional version below, to avoid evaluating the same condition mulitple times:

    write:(name = "") "...Exiting" quit:(name = "")    
FeedbackOpens in a new tab