Fundamentals of BASIC: Difference between revisions
Jump to navigation
Jump to search
Line 110: | Line 110: | ||
End If | End If | ||
</pre> | </pre> | ||
''' | '''Loops''' | ||
<pre> | |||
For i=1 to 10 | For i=1 to 10 | ||
Print i | Print i | ||
Line 136: | Line 137: | ||
Counter = Counter + 1 | Counter = Counter + 1 | ||
Wend | Wend | ||
</pre> | |||
'''With''' | |||
<pre> | |||
Rem With sample | Rem With sample | ||
'With statement allows accessing properties on a specified object | 'With statement allows accessing properties on a specified object | ||
Line 148: | Line 151: | ||
Print .title | Print .title | ||
End With | End With | ||
</pre> | |||
'''Select Case''' | |||
<pre> | |||
Rem Select Case Example | Rem Select Case Example | ||
'Select Case performs conditional execution | 'Select Case performs conditional execution | ||
Line 173: | Line 178: | ||
End Select | End Select | ||
End Sub | End Sub | ||
</pre> | |||
'''Error trapping''' | |||
<pre> | |||
Rem Try Example | Rem Try Example | ||
'Try catches run time errors | 'Try catches run time errors | ||
Line 198: | Line 205: | ||
Print "Done" | Print "Done" | ||
End Try | End Try | ||
</pre> | |||
'''Functions and Subroutines''' | |||
<pre> | |||
Sub mySubroutine(a,b) | Sub mySubroutine(a,b) | ||
Print a + b | Print a + b | ||
Line 206: | Line 215: | ||
myFunction = a + b | myFunction = a + b | ||
End Function | End Function | ||
</pre> | |||
= Importing VB code = | = Importing VB code = | ||
= A few interesting statements = | = A few interesting statements = |
Revision as of 10:25, 10 December 2013
Variables
Naming
The following list shows some variable names that are allowed by AppStudio:
text LLAMAS Jupiter W1Spec SouthPark
And some that are not allowed:
1table 'starts with a number X&Ycords 'uses special character & first counter 'has a space %correct 'does not start with a letter print 'Keyword
Variables
- All variables are of type Variant.
- No explicit types
- Variable get their types from the value that is assigned.
Numeric
- Floating point: 5e-324 (negative) to 1.7976931348623157e+308 (positive)
- Integers: reliable to 15 digits.
- Hex: 0x500 = 1280
- Octal: O500 = 320
Booleans
- True or False.
Colors
- From “#000000” to “#FFFFFF” (black to white)
- color = (red*65536) + (green * 256) + blue
Strings
- Maximum length depends on memory, up to 2 billion characters
Arrays
- Dim A(9) declares an array with 10 elements
- Dim B(8,8) creates a two dimensional array
- Elements can be referred to as A(3), B(2,3), etc.
- Dim statement has to be in each Code Module
- The following notation is also OK: a[3], B[2][3}
- Array elements do not exist until they are assigned.
Collections
- Like an Array, but uses string values to index
Dim item1, myCollection(10) item1="Knuth" 'Create our collection myCollection[item1] = "firstKey" Print myCollection[item1] 'displays "firstkey"
Objects
- An object is a collection of named values, called properties
Customer={Name: Lovelace, Age: 195, kids:{Byron: 1, Annabella: 2, Ralph: 3}} Print Customer.name 'displays "LoveLace"
Classes
'Define a class like this Function Person(name, gender) 'Add object properties like this this.name = name this.gender = gender End Function 'Add methods like this. All Person objects will be able to invoke this Function speakName() Print "Howdy, my name is " & this.name End Function Person.prototype.speak = Eval("speakName") 'Instantiate new objects with 'new' bob = new Person("Bob", "M") 'Invoke methods like this bob.speak() 'Prints "Howdy, my name is Bob"
Control structures
If...Then
If a=b Then c=1 If a=b Then c=1 End If If a=b Then C=1 Else C=2 End If
Loops
For i=1 to 10 Print i Next i=0 Do While i<10 Print i i=i+1 Loop i=0 Do Print i i=i+1 Loop Until i=10 Rem While...Wend Example 'While...Wend repeats a group of statements Dim Counter Counter = 1 While Counter < 5 Print "Counter = " & Counter Counter = Counter + 1 Wend
With
Rem With sample 'With statement allows accessing properties on a specified object Dim theURL, theTitle With document theURL = .URL .title = "New Title" theTitle = .title Print .URL Print .title End With
Select Case
Rem Select Case Example 'Select Case performs conditional execution CheckHat("Blue") CheckHat("Orange") Sub CheckHat(Hat) Select Case Hat Case "Blue" Print "Kyle's hat" Case "Green" Print "Stan's hat" Case "Cyan" Print "Eric's hat" Case "Orange", "Hood" Print "Kenny's hat" Case "White" Print "Chef's hat" Case "Striped" Print "Mr. Hat" Case "Christmas", "Santa" Print "Mr. Hankey's hat" Case Else Print "Unknown Hat" End Select End Sub
Error trapping
Rem Try Example 'Try catches run time errors Try Dim CurrLiab, CurrAssets CurAssets=InputBox("Enter assets") If Not IsNumeric(CurAssets) Then Throw 1 If CurAssets < 0 Then Throw "lessthan" CurLiab=InputBox("Enter liabilities") If CurLiab=0 Then Throw "Divide by Zero" If CurLiab < 0 Then Throw "Liabilities must be > 0" End If If Not IsNumeric(CurLiab) Then Throw 1 Print CurAssets, CurLiab Print "Ratio:" & CurAssets/CurLiab Catch err If err=1 Then err="Enter a number" If err="lessthan" Then err="Value must be >= 0" End if Print err Finally 'this is optional Print "Done" End Try
Functions and Subroutines
Sub mySubroutine(a,b) Print a + b End Sub Function myFunction(a,b) myFunction = a + b End Function