Article
· Jul 10, 2016 4m read

NewBie's Corner Session 12 Do and Goto

NewBie's Corner Session 12 Do and Goto

Welcome to NewBie's Corner, a weekly or biweekly post covering basic Caché Material.

InterSystems Caché provides a GUI (Graphical User Interface) based Integrated Development Environment (IDE) called Caché Studio. Developers can use Studio to create and maintain applications.

Controlling Process Flow

Controlling Process Flow means controlling the execution path of code. The execution of code flows from the top to the bottom in a routine, except for the following:

•             Do and Quit commands

•             Goto command

•             Inline Do command (in a later session)

•             Structured Code (in a later session)

Do and Quit commands

A Do command and some form of Quit command should always be paired. The Do command transfers code execution to a Label. Code execution continues until it encounters a Quit command. The Quit command may be explicit or implied (more about implied Quits in a later session).

Consider this example:

ROUTINEABC

STARTLABEL
               Write !,"At STARTLABEL"
               Do PROC                                            ; Do command
               Write !,"At Start after PROC"
               Quit                                                    ; Quit command
PROC
               Write !,"At Proc label"
               Set X=5
               Quit                                                    ;Quit command

This example demonstrates the Do and Quit commands. Code execution begins at the STARTLABEL. Upon encountering the "Do PROC" line, execution jumps to the PROC Label. When the Quit (under the PROC label) is encountered, execution jumps back to the line immediately after "Do PROC".

Execution continues from that point with the writing of "At Start after Quit" and then it quits.

Enter this routine in Studio and run it from the Terminal.

For every Do command, there must be some form of Quit command. The Quit may be explicit, but also may be implicit or implied.

Orphan Quits (Quits with no associated Do) can cause havoc. Unlike other programming languages, there is no enforcement of Do and Quit pairs in COS. Orphan Quits (Quits with no associated Dos) are a misuse of the Quit command and must be avoided.

Goto command

The Goto command transfers control to a Line Label. Unlike the Do command, the Goto command does not have an associated Quit command.

ROUTINEDEF
STARTLABEL
               Write !,"At STARTLABEL"
               Goto PROC
Write !,"At Start after PROC"
               Quit
PROC
               Write !,"At Proc label"
               Set X=5
               Quit

This example demonstrates the Goto command.  Code execution begins at the STARTLABEL. Upon encountering the "Goto PROC" line, execution jumps to the PROC Label. When the Quit command is encountered under PROC, execution stops. The Quit command under STARTLABEL is never executed.

--Mike Kadow

PaulMikeKadow@gmail.com

If you have a comment, please respond through the InterSystems Developer Community, don't send me private email, unless of course you wish to address me only.

See NewBie Index for an index of all NewBie Corner posts.

Discussion (0)0
Log in or sign up to continue