Skip to main content

Controlling Routine Flow

In order to control the flow of a routine, there must be a way to conditionally run or bypass certain sections of code. There must be a way to cause certain sections of code to run repeatedly. There must be a way to 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.

In the hands-on exercise you just completed, there was an If that caused just one thing to happen if its condition was true; namely, the end of the routine via a Quit. However, it's better to 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 post-conditionals, enclose the condition in parentheses.

postcond ; postconditionals versus If construct
    if name="" { quit  }                       ; you should replace this
    quit:(name = "")                           ; with this postconditional

    if name="" { write "...Exiting" quit  }    ; 2 commands after the if,
                                               ; so do this
    write:name="" "...Exiting" quit:name=""    ; instead of this
FeedbackOpens in a new tab