This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
cubloc:array_and_constants:index [2020/02/03 10:49] COMFILE Technology |
cubloc:array_and_constants:index [2023/07/06 05:29] (current) COMFILE Technology [Arrays] |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Arrays ====== | ||
+ | Cubloc BASIC supports arrays of up to 2 dimensions. Each dimension can contain up to 999 items. String Array not supported. | ||
+ | |||
+ | <code xbasic> | ||
+ | Dim A(20) As Byte ' Declare an array of 20 Bytes | ||
+ | Dim B(200) As Integer ' Declare an array of 200 Integers | ||
+ | Dim C(200) As Long ' Declare an array of 200 Longs | ||
+ | Dim D(20,10) As Single ' 2-dimensional Single array (20 x 10) | ||
+ | </code> | ||
+ | |||
+ | {{ :cubloc:array_and_constants:array.png?nolink |}} | ||
+ | |||
+ | Be sure to make note of how much memory is used when using multidimensional arrays. | ||
+ | |||
+ | <code xbasic> | ||
+ | Dim D(20,10) As Single ' 4 * 10 * 20 = 800 Bytes of Data Memory | ||
+ | </code> | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | |||
+ | ====== Constants ====== | ||
+ | |||
+ | Constants can be used to declare a constant value (a value that cannot be | ||
+ | changed) within the program. This essentially allows a number to be | ||
+ | assigned a name, often improving readability and debugging of the source code. | ||
+ | |||
+ | The CONST statement can be used to declare constants in Cubloc BASIC: | ||
+ | |||
+ | <code xbasic> | ||
+ | Const PI As Single = 3.14159 | ||
+ | Const WRTTIME As Byte = 10 | ||
+ | Const MSG1 As String = "ACCESS PORT" | ||
+ | </code> | ||
+ | |||
+ | When the constant is not given a type, the compiler will find an appropriate | ||
+ | type for it as shown below: | ||
+ | |||
+ | <code xbasic> | ||
+ | Const PI = 3.14159 ' Declare as SINGLE | ||
+ | Const WRTTIME = 10 ' Declare as Byte | ||
+ | Const MYROOM = 310 ' Declare as Integer since it’s over 255. | ||
+ | Const MSG1 = "ACCESS PORT" ' Declare as String | ||
+ | </code> | ||
+ | |||
+ | |||
+ | ====== Constant Arrays ====== | ||
+ | |||
+ | In constant arrays, the user is able to store a list of values before the | ||
+ | program begins. A program requiring a large number of constant values | ||
+ | can be simplified as shown below: | ||
+ | |||
+ | <code xbasic> | ||
+ | Const Byte DATA1 = (31, 25, 102, 34, 1, 0, 0, 0, 0, 0, 65, 64, 34) | ||
+ | I = 0 | ||
+ | A = DATA1(I) ' Store 31 in A. | ||
+ | I = I + 1 | ||
+ | A = DATA1(I) ' Store 25 in A. | ||
+ | Const Byte DATA1 = ("CUBLOC SYSTEMS") | ||
+ | </code> | ||
+ | |||
+ | String data can be stored in Byte constant arrays. Each Byte contains the | ||
+ | ASCII code of each character in the String. In the example above, If | ||
+ | DATA1(0) is read, the ASCII code of 'C’ is returned. Likewise if DATA1(1) is | ||
+ | read, ASCII code of 'U’ is returned. | ||
+ | Integer and floating point (Single) numbers can also be stored in arrays | ||
+ | as shown below: | ||
+ | |||
+ | <code xbasic> | ||
+ | Const Integer DATA1 = (6000, 3000, 65500, 0, 3200) | ||
+ | Const Long DATA2 = (12345678, 356789, 165500, 0, 0) | ||
+ | Const Single DATA3 = (3.14, 0.12345, 1.5443, 0.0, 32.0) | ||
+ | </code> | ||
+ | |||
+ | For multiple-line constant arrays, end each line with a comma, or an | ||
+ | underscore character as shown : | ||
+ | |||
+ | 1) | ||
+ | <code xbasic> | ||
+ | Const Byte DATA1 = (31, 25, 102, 34, 1, 0, 0, 0, 0, 0, 65, 64, 34, | ||
+ | 12, 123, 94, 200, 0, 123, 44, 39, 120, 239, | ||
+ | 132, 13, 34, 20, 101, 123, 44, 39, 12, 39) | ||
+ | </code> | ||
+ | |||
+ | 2) | ||
+ | <code xbasic> | ||
+ | Const Byte DATA2 = (31, 25, 102, 34, 1, 0, 65, 64, 34_ | ||
+ | , 101, 123, 44, 39, 12, 39) | ||
+ | </code> | ||
+ | |||
+ | String constant arrays are deprecated. | ||
+ | |||
+ | Please make note of the following differences between arrays and constant arrays. | ||
+ | |||
+ | ^ ^Array ^Constant Array^ | ||
+ | |Storage Location |Data Memory (SRAM) |Program Memory (FLASH)| | ||
+ | |When Allocated |During Program run |During Download| | ||
+ | |Can be Changed |Yes |No| | ||
+ | |Purpose |Store changing values |Store constant values| | ||
+ | |When Powered Off|Lost |Ratained | | ||
+ | |||
+ | {{ :cubloc:array_and_constants:array_demo.png?nolink |}} | ||
+ | |||
+ | [[cubloc:index:|Go CUBLOC home]] |