Fast Track 1.4

From BR Wiki
Jump to navigation Jump to search

Program Flow

As your program grows, there are several pieces of BR for handling the flow and order of your programs.

00070 GOTO 800 

will go to line 800. A better habit is to use line labels because line numbers may change. For example

00070 GOTO finalprint

00900 finalprint: ! This is the final print page...

Gosub is another way to move through your program. It uses RETURN to get back to the line after it initiated it.

00070 GOSUB printheader

will send you through a subroutine, for example

00900 printheader: print fields using “8,16,cc 50”: “Smart Software Inc”
00910 RETURN

Gosubs must always end in a return, which will send the flow back up to the line after the GOSUB statement.

IF THEN ELSE Statements

If Statements can be used to check if certain conditions are met before your program branches one way or another. If a particular condition is met, then another action will (or won't) happen. For example:

010 IF condition1 THEN
020       branch1 
030 ELSE          
040       branch2 
70 End if 

Or, in this example, if the imp_signal$ variable exists, a line containing it will print, otherwise, the line will print "None."

if len(trim$(Label$(imp_signal))) then
   print #PN: "[MEDIUM][BOLD][title]Warning Label: "Label$(imp_signal)&"[/BOLD][/title][SMALL]" : LC+=1
   print #pn: : LC+=1
else
   print #PN: "[MEDIUM][BOLD][title]Warning Label: None[/BOLD][/title][SMALL]" : LC+=1
   print #pn: : LC+=1
end if

IF statements can also be kept on one line if they are simple:

010 IF condition1 THEN branch1 

For example:

If len(trim$(NewString$)) then let String$=String$&", "&trim$(NewString$)

which is like saying that if newstring$ exists, add it to string$ as we're building a string from new information.

Business Rules also allows ELSE IF statements for building several conditions into an IF statement:

010 IF condition1 THEN 
020       branch1 
030 ELSE IF condition2 THEN         
040       branch2 
050 ELSE IF condition3 THEN 
060       branch3 

branch2 only occurs if condition1 is not met, but condition2 is. Branch 3 only occurs if neither condition1 or condition2 are met, but condition3 is.

Another way to test for multiple conditions is using nested if statements, for example:

if condition1 then
   if condition2 then 
      if condition3 then
         Specific Branch
      end if
   end if
end if

ELSE statements can also be added into any of the nested IF statements.

if condition1 then
   if condition2 then 
      if condition3 then
         Specific Branch
      else
         Specific Branch Alternative
      end if
   end if
end if

Here's a real program example:

if file(RecordRead)=0 then
   if Rec$(rc_recipecode)=CorrectRec$(Cor_recipecode) and Rec(rc_version)=CorrectRec(Cor_version) then
      if Rec(rc_include) then
         let ThisCode=CodeTitle
         let ThisCode=Rec(rc_codetype)
         if ThisCode>CodeTitle then
            let CodeTitle=ThisCode
            print #PN:  : LC+=1
            print #PN: CodeHeadings$(CodeTitle) : LC+=1
         end if
         print #PN: Rec$(rc_code)&"[POS(+0,8)]"&Rec$(rc_CodeDescript) : LC+=1
      end if
   end if
end if 

Furthermore, IF statements can be used together with the keywords AND and OR. And requires both conditions to be met before doing branch1:

IF condition1 and condition2 then
   branch1
else
   branch2
end if

Using OR instead of AND would require that only one of the conditions be met before doing branch1.

DO LOOP

Looping can be done WHILE or UNTIL another condition is met. Do Loops are often used to read or write data.

An internal function FILE returns the status on a data file operation. So looping WHILE FILE(filename)=0 , since 0 is the function's return when a file is read properly, will continue through the loop until the end of the file has been reached.

Loops can be nested, as you see in the following example for reading records into two MATs.

do while file(GHSC)=0
     read #2, using readform$ : mat MaterialRec$, mat MaterialRec eof Ignore
          if file(MaterialRec$)=0 then
             if MaterialRec$(recipecode)=Rec$(recipecode) and MaterialRec(version)=Rec(version) then
                print #PN: MaterialRec$(gc_hcode)&"[POS(+0,8)]"&MaterialRec$(gc_hstmt)
             end if
          end if
loop while MaterialRec$(recipecode)=Rec$(grecipecode) and MaterialRec(version)=Rec(version)