Zbex program control

From CCARH Wiki
Jump to navigation Jump to search
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:


            int i,j 
        
            loop 
              i = i + 1
            repeat     
    

    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.
            loop for i = 1 to 10
              putc ~i          
            repeat             
      
            j = 5              
            loop for i = 1 to j
              j = j - 1        
            repeat             
    

    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.


            loop for i = 1 to 9 step 3
              putc ~i 
            repeat 
            putc ~i
    

    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.


            i = 0 
            loop while i < 5
              i = i + 1 
            repeat 
      
            i = 0 
            j = 10 
            loop for i = 1 to j
              i = i + 1        
              putc ~i          
            repeat while i < 5 
    

    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:


            int i 
            loop for i = 1 to 5 
              if i > 3          
                putc ~i 
              else 
                putc 0 
              end 
            repeat 
       
            loop for i = 1 to 5    
              if (i > 2 and i < 5) or i = 1
                putc ~i        
              end              
            repeat             
    

    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:


       int i 
        
       i = 1    
    AA:          
       if i <= 5      
         putc ~i             
         i = i + 1 
         goto AA 
       end  
    
       loop for i = 1 to 5 
         putc ~i 
       repeat 
    

    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.


            int i             
            loop for i = 1 to 5
              if i = 1        
                putc Eleanor  
              else            
                if i = 2      
                  putc Walter 
                else          
                  if i = 3    
                    putc Jim  
                  else        
                    if i = 4  
                      putc Bill
                    else    
                      putc Mary
                    end 
                  end 
                end 
              end 
            repeat 
    

        int i
        label A(5)
        loop for i = 1 to 5
          goto A(i)
    A(1): putc Eleanor
          goto B
    A(2): putc Walter
          goto B
    A(3): putc Jim
          goto B
    A(4): putc Bill
          goto B
    A(5): putc Mary
          goto B
    B:  repeat
    


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.

              int i
              label A(5) 
              loop for i = 1 to 5
                goto A(i)
         A(1):  putc Girl
                goto B   
         A(2):          
         A(3):          
         A(4):  putc Boy
                goto B  
         A(5):  putc Girl
                goto B 
         B:   repeat 
    

            int i
            label A(5)
            loop for i = 1 to 5
              goto A(i)
       A(1):  putc Girl
              goto B
       A(2):  putc Boy
              goto B
       A(5):  putc Girl
              goto B
       B:   repeat
    


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