Chapter 3 - Program Control
You need commands and functions to define how the program runs. These commands and
functions are in a group called program control. They determine what line of code is
to be executed next.
Labels/Goto
- Lbl
- Lbl X - X can be any one or two character combination (from A to Z, 0 to 9, and Theta) A label is a spot in the program that program execution can jump to with the use of the Goto command.
- Goto
- Goto X - where X is the same character(s) in Lbl X. Goto jumps program execution to a label defined elsewhere in the program.
Example of Lbl and Goto:
Disp "HELLO."
Goto 1
Disp "ERROR!"
Lbl 1
Disp "GOODBYE."
You shouldn't be outputting the line "ERROR!" because the Goto 1 command jumps program execution to Lbl 1.
If/Then/Else
- If
- If conditional - if the conditional is not equal to 0, program execution continues to the next line of code. If the conditional returns 0, program execution continues after the next line of code. Conditional statements are explained later in this tutorial. You can also replace the conditional statement with a mathematical function. If the function returns anything other than 0, it is treated as a TRUE statement and program execution continues after the If statement.
- If-Then
- If conditional , Then ... End - Then is used immediately after an If statement. Program execution continues in the lines after the then statement until its corresponding End statement is met. Instead of just a regular If statement, where only one line after the If statement is either executed or not executed, the Then statement allows for a block (more than one line) of code to be executed. If the conditional statement returns 0 (False), then program execution continues after the End statement corresponding to that Then statement.
- If-Then-Else
- If conditional, Then ... Else ... End - The If-Then-Else works like an If-Then statement, but instead of program flow continuing after the End statement if the conditional returns FALSE, program flow continues after the Else statement until the End statement. The code in between the Else and End statement is not executed if the conditional returns TRUE.
Conditional statements
Above, you read about the use of the "If" series of statements. You may be wondering what conditionals are, then;
Conditional statements are statements that return either TRUE (1) or FALSE (0). Keep in mind that mathematical functions that return anything other than 0 is considered TRUE. You use conditional statements for If, While, and Repeat statements; more advance usage of conditional statements are explained in a later tutorial.
Operators
Operators include the signs Equals[=], Not_equals[!=], Greater_than(or equal to)[>], and Less_than(or equal to)[<].
They are part of the conditional statement (most of the time).
You use these operators just like in math, except that the left side is being compared to the right side.
If the statement is TRUE, the statement returns a 1, if FALSE it returns a 0.
Conditional statements are important not only because they help determine program flow, but
also because they can be used for error checking. Error checking are lines of code that determine
if something is wrong. Usually, you'll check for a number that is out of the range it is suppose to be in.
For example, say you had 100 dollars to bet in a poker game. If you were to bet over 100 dollars, you
would need to implement code that would compare the amount of cash you had with your bet amount and ask
again for a legal bet.
For/Repeat/While
- For
- For(Variable, start_value, end_value, increment) ... End - The For statement is a loop. The command sets the Variable to a starting value (start_value) and increments it by increment until it reaches end_value. Program flow continues until it reaches the corresponding End statement. The variable is then incremented. There is a check to see if the variable has gone pass the end_value. If it has (after incrementing) than program flow continues right after the End statement. If the end_value is not passed yet, then program flow jumps back to the line right after the For statement and continues again until it reaches the End.
- Repeat
- Repeat Conditional ... End - Repeat is similar For statement. When Repeat is executed, it skips the conditional and program flow continues until it reaches the End statement. A check is made to see if the conditional statement is true. If it is, program flow continues after the End statement. If the conditional is false, program control jumps the line after the Repeat statement and continues looping until the Conditional statement is true.
- While
- While Conditional ... End - A While statement works very much like a Repeat statement, except instead of repeating the code inside the loop until the conditional is true, the code inside the loop is executed only if the conditional is true. The check to see if the conditional is true is not skipped the first time the statement is executed, also.
Warning: When using Repeat and While loops, make sure to include coding that will break out of the loop if need be. If there is no code that ends the loop (by setting variables), then you would have an endless loop. Make sure, also, that you do not override the variable being incremented in a For loop (unless you want to set it to or past the end_value to break out of the loop). Doing so may also result in an endless loop if you are unsure of what you are doing.
Other program controls
- Pause
- Pause [string]: this statement pauses the program. The user must press enter to resume the program. You can add a string or variable after the Pause statement to make display information. You can only have one string or variable, though, unlike a Disp statement. However, if you have a string that is more than 16 characters in length, the user will be able to scroll to see the rest of the string. If you use Pause to display a matrix or list that is to large to fit the screen, the user will be able to scroll around to see the contents of the list or matrix.
- Menu
- Menu(TITLE,MENU_TEXT1,LBL1,MENUTEXT2,LBL2[, ... MENUTEXT7,LBL7]) - Title is a string to be displayed as a title on the top of the menu. MENU_TEXTX is a string (14 characters max string length) to be displayed by number X (from 1 to 7) on the left side of the menu. When a user hits enter on that menu string, or presses the its corresponding number (X), program flow jumps to label X, defined somewhere else in the program.
- Stop
- Stop : This statement exits the program! Note: you don't need to a Stop in the last line of code in a program.
|