CyBasic How to program in CyBasic CyBasic Screen is a field for entering CyBasic operators. The result of the program may be a line, a curve, either open or closed, an image, text, or even a file. There is a virtual rectangular coordinate system with its center in the center of the screen: the X-axis is horizontal, with the positive direction going to the right; the Y-axis is vertical, with the positive direction going up. The screen has size of 160x87 pixels. You can only draw on the screen within the fixed range of coordinates: -80Long>Int>Char). Ex.: A(Long)+B(Long)=C(Long); A(Char)+B(Long)=C(Long); A(Double)+B(Long)=C(Double). 2. Type of the integer-valued constant is Long if it satisfies the conditions for Long Data Type. Otherwise, the type of the integer-valued constant is Double. 3. Type of the real constant is Double. EX.: 1.3; 0.672; 1. 4. Result of division is Double. 5. Returned value of function sin, cos etc. is Double. 6. Type of returned value of the user-defined Function is assigned while the Function is determined. By Value (ByVal) A By Value is a way of passing the value of an argument to a procedure instead of passing the address. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed. By Reference (ByRef) A By Reference is a way of passing the address of an argument to a procedure instead of passing the value. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed. Unless otherwise specified, arguments are passed by value. Procedure A procedure is a named sequence of statements executed as a unit. For example, Function and Sub are types of procedures. A procedure name is always defined at the module level. All executable code must be contained in a procedure. Procedures can't be nested within other procedures. Argument An argument is a constant, variable, or expression passed to a procedure. Statements To create a program in CyBasic, you may use the following operators: Hereinafter, describing the statements, everything contained inside […] can be omitted. Dim Statement Declares variables. Syntax Dim varname[([subscripts])] [As type] The Dim statement syntax has these parts: varname (Required. Name of the variable; follows standard variable naming conventions), subscripts (Optional. Dimensions of an array variable (may be number or expression)), type (Optional. Data type of the variable; may be Char, Int, Long, Double). Remarks Variables declared with Dim outside any procedure are available to all procedures. Variables declared with Dim within the procedure are available only within the procedure. Use the Dim statement to declare the data type of a variable. For example, the following statement declares a variable as an Int: Dim a As Int When variables are initialized, a numeric variable is initialized to 0. Note: When you use the Dim statement in a procedure, you generally put the Dim statement at the beginning of the procedure. This is important! For an array variable, subscripts must be greater than or equal to 2. If you don’t indicate the type of variable, it always is Double (Dim A = Dim A As Double). Examples for Dim Statement Dim A «Variable is Double Data Type» Dim A As Char «Variable is Char Data Type» Dim A [100] As Char «Array variable has 100 elements» How to call array elements: A [10] = 32 A [0] = 100 «First element of array» A [99] = 101 «Last element of array» A [100] – it’s a bug, because the last element of array that contains 100 elements has index 99. If…Then…Else Statement Conditionally executes a group of statements, depending on the value of an expression. Syntax If condition1 Then [statements] [ElseIf condition2 Then [elseifstatements] ... [Else [elsestatements]] End If The If...Then...Else Statement syntax has these parts: condition1 (Required. A numeric expression that evaluates to True or False. If condition is Null, condition is treated as False), statements (Optional. One or more statements separated by colons; executed if condition is True), condition2 (Optional. Same as condition1), elseifstatements (Optional. One or more statements executed if associated condition2 is True), elsestatements (Optional. One or more statements executed if no previous condition1 or condition2 expression is True). Remarks The Else and ElseIf clauses are both optional. You can have as many ElseIf clauses as you want in a block If, but none can appear after an Else clause. Block If statements can be nested; that is, contained within one another. When executing a block If, condition1 is tested. If condition1 is True, the statements following Then are executed. If condition1 is False, each ElseIf condition (if any) is evaluated in turn. When a True condition is found, the statements immediately following the associated Then are executed. If none of the ElseIf conditions are True (or if there are no ElseIf clauses), the statements following Else are executed. After executing the statements following Then or Else, execution continues with the statement following End If. For...Next Statement Repeats a group of statements a specified number of times. Syntax For counter = start To end [Step step] [statements] [Exit For] [statements] Next The For…Next statement syntax has these parts: counter (Required. Numeric Variable used as a loop counter), start (Required. Initial value of counter), end (Required. Final value of counter), step (Optional. Amount counter is changed each time through the loop. If not specified, step defaults to one), statements (Optional. One or more statements between For and Next that are executed the specified number of times). Remarks The step can be either positive or negative. The value of the step determines the loop processing as follows: if step value is positive or 0, then the loop executes if counter <=end. If the step value is negative, then the loop executes if counter >=end. Function Statement Declares the name, arguments, and code that form the body of a Function procedure. Syntax Function name [(arglist)] [As type] [statements] [name = expression] [Exit Function] [statements] [name = expression] End Function The Function statement syntax has these parts: name (Required. Name of the Function; follows standard variable naming conventions), arglist (Optional. A list of variables representing arguments that are passed to the Function procedure when it is called. Multiple variables are separated by commas), type (Optional. Type of the value returned by the Function procedure), statements (Optional. Any group of statements to be executed within the Function procedure), expression (Optional. Return value of the Function). The arglist argument has the following syntax and parts: [ByVal | ByRef]varname[As type] [= defaultvalue] ByVal (Optional. Indicates that the argument is passed by value), ByRef (Optional. Indicates that the argument is passed by reference), varname (Required. Name of the variable representing the argument; follows standard variable naming conventions), type (Optional. Data type of the argument passed to the procedure; may be Char, Int, Long or Double), defaultvalue (Optional. Any numeric expression. It is determine only once, at the moment of Function initialization). Variables used in Function procedures fall into two categories: those that are explicitly declared within the procedure and those that are not. Variables that are explicitly declared in a procedure (using Dim or the equivalent) are always local to the procedure. Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure. You can't define a Function procedure inside another Function or Sub procedure. Like a Sub procedure, a Function procedure is a separate procedure that can take arguments, perform a series of statements, and change the values of its arguments. However, unlike a Sub procedure, you can use a Function procedure on the right side of an expression in the same way you use any intrinsic function, such as Sin, Cos or Sqrt, when you want to use the value returned by the function. The Exit Function statement causes an immediate exit from a Function procedure. Program execution continues with the statement following the statement that called the Function procedure. Any number of Exit Function statements can appear anywhere in a Function procedure. Caution! Function procedures can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow. How to invoke Function: If a function's arguments are defined as follows: If a function's arguments are defined as follows: Function MyFunc1(MyStr As Int, MyArg1 As Int = 5, MyArg2 = 7.5) Dim RetVal Function can be invoked in your program as follows: RetVal = MyFunc1(48, 2, 6.9) "All 3 arguments supplied". RetVal = MyFunc1(1, , 5) "Second argument omitted". If a function's arguments are defined as follows: Function MyFunc2(MyArgum = 15) Dim RetVal The Function can be invoked in your program as follows: RetVal = MyFunc2(2) "Argument supplied". RetVal = MyFunc2() "Argument omitted, however it's necessary to write the brackets". Sub Statement A sub statement declares the name, arguments, and code that form the body of a Sub procedure. Syntax Sub name [(arglist)] [statements] [Exit Sub] [statements] End Sub The Sub statement syntax has these parts: name (Required. Name of the Sub; follows standard variable naming conventions), arglist (Optional. List of variables representing arguments that are passed to the Sub procedure when it is called. Multiple variables are separated by commas), statements (Optional. Any group of statements to be executed within the Sub procedure). The arglist argument has the same syntax and parts as Function arglist. All executable code must be in procedures. You can't define a Sub procedure inside another Sub or Function procedure. The Exit Sub keywords cause an immediate exit from a Sub procedure. Program execution continues with the statement following the statement that called the Sub procedure. Any number of Exit Sub statements can appear anywhere in a Sub procedure. Like a Function procedure, a Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. However, unlike a Function procedure, which returns a value, a Sub procedure can't be used in an expression. Variables used in Sub procedures fall into two categories: those that are explicitly declared within the procedure and those that are not. Variables that are explicitly declared in a procedure (using Dim or the equivalent) are always local to the procedure. Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure. Caution! Sub procedures can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow. You can call a Sub procedure using the Call Statement. Call Statement A call statement transfers control to a Sub procedure. Syntax [Call] name [argumentlist] The Call statement syntax has these parts: Call (Optional. Keyword), name (Required. Name of the procedure to call), argumentlist (Optional. Comma-delimited list of variables or expressions to pass to the procedure. Components of argumentlist may include the keywords ByVal or ByRef to describe how the arguments are treated by the called procedure). Remarks You are not required to use the Call keyword when calling a procedure. Caution! Call Statement is used to invoke only user-defined procedures. Print Statement A Print Statement writes data either on a screen or to a file. Syntax Print [#filenumber,][outputlist] The Print statement syntax has these parts: filenumber (Optional. Any valid file number, numeric expression), outputlist (Optional. List of expressions or string constant divided by comma to print). Remarks String Constant – any sequence of contiguous characters in inverted commas that represent the characters themselves rather than their numeric values. A string constant can include letters, numbers, spaces, and punctuation. # - after this symbol must follow file number that is conformed to the file number in Open Statement. If you don’t write the filenumber, information will be printed on the screen. Basic rule: variable with Char Data Type will be printed as a character with a code that is equal to this variable (see Character Table). Character Table Character Code of the character Space 32 ! 33 " 34 # 35 $ 36 % 37 & 38 ' 39 ( 40 ) 41 * 42 + 43 , 44 - 45 . 46 / 47 0 48 1 49 2 50 3 51 4 52 5 53 6 54 7 55 8 56 9 57 : 58 ; 59 < 60 = 61 > 62 ? 63 @ 64 A 65 B 66 C 67 D 68 E 69 F 70 G 71 H 72 I 73 J 74 K 75 L 76 M 77 N 78 O 79 P 80 Q 81 R 82 S 83 T 84 U 85 V 86 W 87 X 88 Y 89 Z 90 [ 91 \ 92 ] 93 ^ 94 _ 95 ` 96 a 97 b 98 c 99 d 100 e 101 f 102 g 103 h 104 i 105 j 106 k 107 l 108 m 109 n 110 o 111 p 112 q 113 r 114 s 115 t 116 u 117 v 118 w 119 x 120 y 121 z 122 { 123 | 124 } 125 ~ 126 Remember! Some of characters are not represented on the screen. Ex.: a character with code 32 is a space and you will not see it. Print Statement Examples: filenumber "Prints blank line to a file" Print "Prints blank line on the screen" How to print an Array variable: If an array variable is declared as Char Data Type, then: Print A [1] - second element of array will be printed (according to the printing rule of Char Data Type). Print A - all array elements will be printed. Note: If an array element is met that equals zero, then the print will be stopped. If an array variable is declared as any other type, then: Print A [1] - second element of array will be printed. Print A – you’ll see a message about an error. You must indicate all elements of the array if you want to print them. This is important to know! If you print to a file, then, before using the Print Statement, a file must be opened with the Open Statement. Caution! During the process of running the CyBasic application, you can’t write more than 23Kb into the files. When this value is exceeded, an error occurs. Input Statement An Input Statement assigns the data to variables. Syntax Input varname The Input Statement syntax has this part: varname (Required. Name of the variable). If, in the program, an Input Statement is met, the dialog line appears on the screen where you must put the value of the variable. If you press , execution of program will be interrupted. If you press (without entering value of variable), the variable is initialized to 0. Basic rule: a variable with Char Data Type will be entered as the code of the fit character (see Character Table) that you have put. Input Statement Examples: Dim a “Declares variable a as Double Data Type” Input a “Enters value with Double Data Type in variable a” How to input Array variable: If an array variable is declared as Char Data Type then: Input A [1] - second element of array will be entered (according to the basic rule of inputting Char Data Type). Input A - all array elements will be entered. Note: if the amount of characters being entered is more than the array dimension, the excess will be truncated. If the amount of characters being entered is less then the array dimension, all characters will be put in array and the next element will be equal to zero. If an array variable is declared as any other type, then: Input A [1] - second element of array will be entered. Input A – you’ll see a message about an error. You must indicate all elements of an array if you want to input them. Open Statement An Open Statement enables input/output to a file. Syntax Open pathname [For mode] As filenumber The Open statement syntax has these parts: pathname (Required. String expression that specifies a file name), mode (Optional. Keyword specifying the file mode: Append, Read, Write. If unspecified, the file is opened for Read access), filenumber (Required. A valid file number in the range 0 to 65535). File Modes: Read: This mode is for reading only. Data can be read from a file using a Get Statement. If there is no file or if the data is read beyond the bounds of a file, an error occurs. Write: This mode is for writing only. If there is no file, it will be created. If there is a file, it will be deleted and created anew. Note (for Read and Write modes): when a file is opened, the current position is initialized to 0. Append: This mode is for writing only. If there is no file, it will be created. If there is a file, it will be opened. The current position is initialized at the end of the file. Remarks You must open a file before any Input/Output operation can be performed on it. If the file specified by pathname doesn't exist, it is created when a file is opened for Append or Write modes. Otherwise, an error occurs. If the file is already opened by another process and the specified type of access is not allowed, the Open operation fails and an error occurs. Important! In Read mode, you can open a file using a different file number without first closing the file. In Append and Write modes, you must close a file before opening it with a different file number. Caution! During the process of running the CyBasic application, you can’t write more than 23Kb into the files. When this value is exceeded, an error occurs. Open examples: Open “file1.txt” for read as 1 Open “file2.txt” for write as 2 ………………………………… Open “file3.txt” for read as 3 Open “file3.txt” for write as 4 - it’s a bug, you’ll see a message about an error. You must close the file before opening it for Write mode. Close Statement A Close Statement concludes the input/output to a file opened using the Open statement. Syntax Close [filenumber] Filenumber (Optional. Numeric expression that is any valid file number). Remarks If you omit the filenumber, all active files opened by the Open Statement are closed. When the Close statement is executed, the association of a file with its file number ends. Close examples: Open “file1.txt” for read as 1 Open “file2.txt” for read as 2 …………………………………. Close 1 “Concludes the file with number 1” Close “Concludes the files with number 1 and number 2” Put Statement A Put Statement writes data from a variable to a file. Syntax Put filenumber, [recnumber][, varname] The Put Statement syntax has these parts: filenumber (Required. Any valid file number, numeric expression), recnumber (Optional. Record byte number at which writing begins), varname (Optional. Name of variable containing data to be written to a file). Note: filenumber has to conform to the file number in Open Statement. Remarks Data written with Put is usually read from a file with Get. The first record or byte in a file is at position 0. The second record or byte is at position 1, and so on. If you omit recnumber, the records are written in sequence, beginning with current position. After recording, the current position is shifted forward by the size of the variable in bytes. If you exceed the file dimension, it automatically increases. You must include delimiting commas, for example: Put 4,,var_char "Write variable var_char to the current position" Put 4, 100 “Determine the current position is equal 100” Put 4, 100, var_char “Determine the current position is equal 100, write the variable var_char there; after that the current position will be equal 101” Dim var_int as Int “Declare variable var_int as Int Data Type” Put 4,,var_int “Write variable var_int to the current position” Put 4, 100, var_int “Determine the current position is equal 100, write the variable var_int there; after that the current position will be equal 102” This is important to know! Before using the Put Statement, a file must be opened with an Open Statement. Caution! During the process of running the CyBasic application, you can’t write more than 23Kb into the files. When this value is exceeded, an error occurs. Get Statement Reads data from a file into a variable. Syntax Get filenumber, [recnumber][, varname] The Get Statement syntax has these parts: filenumber (Required. Any valid file number, numeric expression), recnumber (Optional. Record byte number at which reading begins), varname (Optional. Name of variable where data will be written). Note: filenumber has to conform to the file number in Open Statement. Remarks Data read with Get is usually written to a file with Put. The first record or byte in a file is at position 0, the second record or byte is at position 1, and so on. If you omit recnumber, the records are read in sequence, beginning with current position. After the records are read, the current position is shifted forward depending on the size of the variable in bytes. If you exceed the file dimension, an error occurs. You must include delimiting commas, for example: Get 4,,var_char “Read data from the current position to a variable var_char” Get 4, 100 “Determine the current position is equal 100” Get 4, 100, var_char “Determine the current position is equal 100, read data from there to a variable var_char; after that the current position will be equal 101” Dim var_int as Int “Declare variable var_int as Int Data Type” ………………………… Put 4,,var_int “Read data from the current position to a variable var_int” Put 4, 100, var_int “Determine the current position is equal 100, read data from there to a Variable var_int; after that the current position will be equal 102” This is important! Before using the Get Statement, a file must be opened with Open Statement. Line Statement A Line Statement draws a line on the screen. Syntax Line X1, Y1, X2, Y2 X1, Y1, X2, Y2 – numeric expressions. There is a virtual rectangular coordinate system in the screen: the X-axis is horizontal, with the positive direction going to the right; the Y-axis is vertical, with the positive direction going up. (X1, Y1) are coordinates of the origin of line, while (X2, Y2) is coordinates of the point of line. Remember that -20000, >=, <, <=, =, <>, +, -, *, /, (, ), 4. You may use the following functions in the programs: sin(x), cos(x), tg(x), sgrt(x), acos(x), acosh(x), actan(x), actanh(x), asin(x), asinh(x), atan(x), atanh(x), cosh(x), exp(x), log(x), log10(x), log2(x), pow(x,y), pow2(x), sinh(x), sqr_pow(x,y), sqrt(x). F.e., I=sin (x+5); Arguments of the functions should be enclosed in brackets. 5. In each operator, you should separate the text part from the parameter by any space. 6. The spacer of a fractional part in a number is a period. 7. You can use only the limited number of variables, subprograms, and opened files. 8. The maximum number of nested cycles is 30. How to insert comments in your program You can insert different comments in your program in order to explain, in detail, your actions. Any notes or symbols after the marks “ ‘ ” are understood as comments and therefore will not be executed. So, comments may be written at the new line and begin with “ ‘ ”. Comment’s Examples: For i=1 to 10 ‘main circle of the program ………………………………………………… ‘the beginning of drawing the cross line –1, 1, 1, -1 line 1, 1, -1, -1 ‘the end of drawing the cross
This info was took from http://www.cybipros.cjb.net/ give them credit for it.