Fast Track 1: Difference between revisions
No edit summary |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{:Fast Track 1.1} | {{:Fast Track 1.1}} | ||
{:Fast Track 1.2} | {{:Fast Track 1.2}} | ||
{:Fast Track 1.3} | {{:Fast Track 1.3}} | ||
{:Fast Track 1.4} | {{:Fast Track 1.4}} | ||
NEXT: [[Fast Track 2|Handling Data]] | |||
<noinclude> | <noinclude> | ||
[[Category:Fast Track Tutorial]] | [[Category:Fast Track Tutorial]] | ||
</noinclude> | </noinclude> |
Latest revision as of 21:13, 5 August 2014
Basics
BR is an object oriented programming language, with the goal of quickly creating customized business software that is efficient, powerful, and easy to use.
The basic command, print, will print anything back onto your console (hit enter after typing each line).
PRINT 10 +6 PRINT “Here we go!” PRINT variable$ PRINT NumericExpression
LET
LET will set the value of a variable. Non-numeric, or string variables, always end in $, and the assigned data must always have quotes around it.
LET string$=”Print it all.”
PRINT string$
will return
Print it all.
That's a string, or non-numeric variable. A numeric variable NUMVAR
Let numvar=10 Let numvar2=20 Print numvar+numvar2
will return
30
Print environmental variables:
PRINT version$
will return the version of BR that you are using.
Write a classic program:
Note that BR uses line numbers. It is recommended that you use an external editor and BR compiler to get around this once you're comfortable with the basics.
10 print “hello world” RUN
will run your program.
SAVE hello
will save your program, naming it "hello".
List
LIST will list the lines of your program.
LIST can list line numbers, lines containing words specified in quotes, or whole programs.
LIST LIST “print” - for all occurrences of the command PRINT LIST -P -for page by page listing of long programs LIST 20 50 – for lines 20 to 50
List your program:
10 print “hello world”
Change the line number to 30 and press Enter:
30 Print “Hello World”
Add some lines, by typing:
10 print “What is your name?” 20 input name$
INPUT
INPUT asks for input from the user.
Change line 30 to
30 PRINT “HELLO ”&” “&name$&”!”
RUN your program, enter your name, and voila!
Notice the use of the ampersand, you can add two variables together.
To split a string apart, you can use beginning and ending spaces:
let name$= “Johnny Cash” let firstname$=name$(1:6) print firstname$
will return “Johnny.”
To save a program that already exists, use REPLACE. Most commands can be shortened to their smallest unique beginning letters.
REP hello
The BR Editor
BR allows you to edit programs while they are running. The following commands will help you do this:
DEL can be used to delete lines from your program.
Typing over lines or line numbers will change them, but only if you press enter afterwards. Then, using REPLACE will save your changes if the program has been saved once already.
You may have already noticed that the built-in BR editor is a little hard to use.
Convert your programs to source code so that you can edit them in an external editor. Use list (and hit enter after every line).
LIST >hello.brs
External Editors
It is highly recommended that you use an external editor. If you don't already have one, download MyEditBR from Mills Enterprises, and download Lexi, which is available from Sage AX—a tool that allows you to edit without worrying about line numbers (and a few extra features) and install them both.
Open your BR program hello.brs in your editor, and continue with the tutorial.
Use Lexi to strip the line numbers (you can add them again too). When you hit “Compile BR program” it will automatically add the line numbers and re-create your program as a .BR for Business Rules to use, which you can run from the BR console to fully test them. Simple errors will be flushed out for you to handle when you compile it too.
Using an external editor allows you to copy and paste, move around in the text, and have much more control over the editing process than the BR console editor allows.
Errors
Error Beeps
You may have noticed that as you're working with your programs, BR will beep at you if there is a mistake. Enter the line:
10 Print “Print it all
and BR will produce the Error 1003, which appears in the third section on the bottom bar of your BR console. Checking on the wiki is the best way to learn about unknown errors. Search and you'll see that 1003 is a Missing Quote error. Which you can easily fix to run the program.
Print Newpage
You may have noticed that your console is filling with stuff. Type
Print Newpage
(and enter) and it will clear the screen. When writing programs it's important to use this code to clear your user's screen in between sections of your program so that leftovers don't appear on your screens.
Clear
The Clear command will also clear the screen, but will exit any programs you have running, and delete any unsaved portions, and current variable values. When debugging, CLEAR PROC will clear the current activity but will leave variables intact.
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)
NEXT: Handling Data