Difference between revisions of "Zbex program control"
(created page) |
(added chapter navigator at top of page) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | {{ZbexChap | ||
+ | | before = [[Zbex subscripts|Subscripts]] | ||
+ | | after = [[Zbex relations|Relations]] | ||
+ | }} | ||
+ | <center> | ||
__TOC__ | __TOC__ | ||
+ | </center> | ||
An essential feature of any computer language is the ability to branch based on some condition. Zbex provides two mechanisms for branching: loops and the if statement. | An essential feature of any computer language is the ability to branch based on some condition. Zbex provides two mechanisms for branching: loops and the if statement. | ||
Line 228: | Line 234: | ||
In the second piece of code, the labels A(3) and A(4) don't exist, so the program jumps to the label A(2). The rule is that if a label doesn't exist, jump to the next lowest one that does. | In the second piece of code, the labels A(3) and A(4) don't exist, so the program jumps to the label A(2). The rule is that if a label doesn't exist, jump to the next lowest one that does. | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | {{ZbexChap | ||
+ | | before = [[Zbex subscripts|Subscripts]] | ||
+ | | after = [[Zbex relations|Relations]] | ||
+ | }} |
Latest revision as of 06:14, 15 October 2010
Previous chapter Subscripts |
Zbex Manual |
Next Chapter Relations |
An essential feature of any computer language is the ability to branch based on some condition. Zbex provides two mechanisms for branching: loops and the if statement.
Loop statement
There are three types of loop statements: (1) the simple loop, (2) loop while a relation is true, and (3) loop with a counter. Every loop statement must be balanced with a repeat statement. All lines of code between a loop statement and its associated repeat statement are considered to be inside the loop by the compiler. You may not use a goto statement to jump into the interior of a loop from anywhere outside the loop. Examples:
|
This is a simple loop with no exit condition. The way it is written, program control will never leave the loop. We call this an infinite loop. |
|
This is a loop with a counter. It will put the numbers 1 to 10 on the screen.
This example illustrates an important point about Zbex. When a counting loop statement is encountered at run time, the interpreter evaluates the limits and fixes them. Nothing inside the loop can change the limits. In this example, the loop will execute 5 times, even though the value of j is subsequently changed inside the loop.
|
|
The output from the code inside the loop will be the numbers 1, 4, and 7. The last putc statement will put out the number 10. The repeat statement causes the counter (i) to be incremented and then tested. If the test fails,control passes to the statement beyond the repeat.
|
|
This loop will execute five times. Upon exit, the value of i will be 5
The output from this code will be the numbers 2, 4 and 6. Note that the value of the counter can be changed inside the loop. Also note that the exit condition in this case is provided by the repeat statement, not the loop statement. Had j been set to 4 instead of 10, the output would have been the numbers 2 and 4, and the exit condition would have come from the loop statement. |
If statement
The if statement in Zbex is part of an if-else-end grouping. The else is optional. Zbex uses the if statement to test a simple condition. If the condition is true, control passes to next statement in line. If the condition is false and there is an else statement in the group, control passes to the statement following the else statement; otherwise control passes to the statement following the associated end statement. Examples:
|
The output from this code will be the numbers 0, 0, 0, 4 and 5.
The output from this code will be the numbers 1, 3 and 4. Observe the use of and and or in this example. These are called boolean operators and can occur only in if and while statements. Also observe the use of parentheses to establish the order of the boolean operations. |
Goto statement
The normal way to transfer program control to a different part of a program is with the goto statement. The goto statement takes a label as an argument. You may not use a goto statement to jump into a loop or to jump into an if-else-end group. You may use goto to jump around inside a loop or to jump out of a loop (as long as you are not jumping into another loop). Example:
|
This code does precisely the same thing as the loop code below. Notice that the label AA must begin in column one and must be followed by a colon (:). |
Goto case statement
Zbex does not have case statements (these are a feature of both the Pascel and the C programming languages). But Zbex does provide a means for jumping to different points in a program depending on the value of an integer. To do this, we have implemented a variable type called a label. All label variables must be declared using the label declaration statement. All labels must be one dimensional arrays. To illustrate how label variables work, we show two pieces of code, each doing the same thing.
|
|
The code on the right in the above example is not only more compact, it is much faster because the test which determines the name to put out for each value of i happens only once, whereas in the first piece of code, four tests must be made before the names, Bill and Mary, can be put out.
Goto label ranges
Label variables also have the feature that they can identify ranges of an integer for which certain actions should be taken. The two pieces of code below produce the same effect.
|
|
In the second piece of code, the labels A(3) and A(4) don't exist, so the program jumps to the label A(2). The rule is that if a label doesn't exist, jump to the next lowest one that does.
Previous chapter Subscripts |
Zbex Manual |
Next Chapter Relations |