07.05.2015 Views

Download - True BASIC

Download - True BASIC

Download - True BASIC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Constants, Variables, and Expressions<br />

11<br />

of the string in place of b. If a is less than 1, then <strong>True</strong> <strong>BASIC</strong> substitutes a value of 1. If a is larger than the length<br />

of the string, or a is greater than b, the result is an empty string. Thus, any values of a and b are legal. For example,<br />

the program:<br />

PRINT “House”[3:100]<br />

PRINT “House”[-5:20]<br />

PRINT “House”[4:4]<br />

PRINT “House”[5:3]<br />

PRINT “Done”<br />

END<br />

produces the following output:<br />

use<br />

House<br />

s<br />

Done<br />

Note that the substring expression in the fourth line returns the null string because a is larger than b, and thus a<br />

blank line is printed.<br />

You may use parentheses to control the order of evaluation when combining substring expressions with other<br />

string expressions. If you don’t use parentheses, substring extraction occurs before concatenation. Thus,<br />

“Abcde” & “fghijklm”[3:7]<br />

equals “Abcdehijkl”, while<br />

(“Abcde” & “fghijklm”)[3:7]<br />

equals “cdefg”.<br />

Here’s a simple program that uses string constants, variables, and expressions:<br />

LET first_name$ = “Abraham”<br />

LET last_name$ = “Lincoln”<br />

PRINT first_name$ & “ “ & last_name$ & “, 16th President of the US”<br />

PRINT “Subscription Code: “ & last_name$[1:4] & first_name$[1:1]<br />

END<br />

It produces the following output:<br />

Abraham Lincoln, 16th President of the US<br />

Subscription Code: LincA<br />

Assignment Statements<br />

The LET statement is the primary means of assigning values to variables. In assigning values you must assign<br />

numeric values to numeric variables and string values to string variables. (<strong>True</strong> <strong>BASIC</strong> does not perform automatic<br />

type conversions.) Examples of numeric assignments are:<br />

LET e = 2.718282828<br />

LET answer = Sin(pi/2) * Exp(-x^2)<br />

LET length = last - first + 1<br />

LET k = k + 1<br />

LET i, j = 2<br />

In an assignment statement, the expression to the right of the equal sign (=) is evaluated first, and the resulting<br />

value is assigned to the variable to the left of the equal sign. This means that you can use the variable appearing<br />

on the left in the expression on the right, and its old value will be used in the expression. In the next to the last<br />

example above, the present value of k increases by one and becomes the new value of k.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!