Before You Post, Please Note...

-> Please Note: By Posting, you agree to submit the contents of your post to the Public Domain <- SEE: howto post to this blog

Friday, February 2, 2007

puppybasic intro ch. 03 - variables

green text is being edited and should not be considered  
relevant to the tutorial.
for a while, it will make up
the bulk of this section.

black text has been edited to be more accurate with
regards to puppybasic.

........................................................................
variables
1 of 9




an intro to puppybasic

chapter 3: variables







........................................................................

........................................................................
variables
2 of 9

variables are the easiest way to store data in memory; of course, this
memory is cleared when the program ends or the machine is turned off,
but variables are useful when you want data in one part of your program
to be available to another line in the program.

there are two main types of variables: string and numeric.

string variable names start with a letter and are made of letters and
numbers.




........................................................................

........................................................................
variables
3 of 9

here is an example of a string variable at work:

q$="hello, world!" ____________________________________________________
PRINT q$ _hello, world!______________________________________
PRINT q$ _hello, world!______________________________________
PRINT q$ _hello, world!______________________________________
PRINT q$ _hello, world!______________________________________
____________________________________________________

this prints the contents of the string variable q$ 4 times.



........................................................................

........................................................................
variables
4 of 9

strings, like those used by the PRINT statement, start and end with a
double quote "like this." you cannot enter a string containing a double
quote:
"like "this example" does,"

later on you'll learn a way around this using the CHR$ function.

string variables almost always end in a dollar sign, while on the other
hand, numeric variables often end without in any special symbol, even
though there are other suffixes for specific types (which you usually
wont need... but you may want them.)


........................................................................

........................................................................
variables
5 of 9

all variables may be named with one or more letters, must start with a
letter, and may include numbers (q2$ or q2 for example) after that.
also, do not give a variable the same name as a statement or function
(like "CLS") if you want your program to work.

it's worth showing that you can print a mix of strings and numbers, like
in the example on the next page...






........................................................................

........................................................................
variables
6 of 9
______________________________________________
n=50 __7 hello, world! 50 times.___________________
PRINT 7; q$; n; "times." ______________________________________________

...and you can combine two strings into one with the + sign like this:

q$="hello"
q$="oh, "+q$+" there..."

now the string "oh, hello there..." is stored in the variable q$.
combining strings this way, with or without variables, is called
"concatonization" and does not work with numeric variables.


........................................................................

........................................................................
variables
7 of 9

there are a couple more things you might want to know before moving on:
it is often preferable (in the interest of time and effort) to type a
question mark: ? "hello."

instead of typing out PRINT, puppybasic will (along with many other
interpreters and compilers) change it to PRINT for you. it won't stop
you from using a question mark where you actually want one:

? "do you want to quit?"
will change to:
PRINT "do you want to quit?"


........................................................................

........................................................................
variables
8 of 9

also, instead of giving each statement its own line, you may often find
you prefer to combine lines of code using a colon :

CLS: PRINT "to whom it may concern"; : PRINT ":"

will clear the screen and PRINT the following:

________________________________________________________________________
_to whom it may concern:________________________________________________
________________________________________________________________________
________________________________________________________________________


........................................................................

........................................................................
variables
9 of 9

COLOR 10: PRINT "dear abby"; : PRINT ","

will print: ___________________________________________________________
_dear abby,________________________________________________
___________________________________________________________

congratulations! if you have not already, you can now write your first
puppybasic program. you will be able to do much more after reading the
next section.

-= end of chapter 3 =-


........................................................................
an intro to puppybasic (chapter 3)
this content is public domain.
........................................................................


click here to go back to the contents page

0 comments: