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. 11 - decision making

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.

........................................................................
decision making
1 of 9




an intro to puppybasic

chapter 11: decision making







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

........................................................................
decision making
2 of 9

hey, now we make your programs smarter. right now your code does this:

[do this.]

but we can make it so smart, it'll know whether to or not!

[do this... but not unless i want you to.]

you just have to be really specific about what you want :D

here's an example of IF...THEN, using the very naughty GOTO statement :O


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

........................................................................
decision making
3 of 9

x=0
alabel:
x=x+1 ______________________
IF x > 5 THEN ? x ' > means "more than" __6___________________
IF x < 10 THEN GOTO alabel ' < means "less than" __7___________________
__8___________________
which will PRINT this, on the screen -> __9___________________
__10__________________
unless x is more than 5, it won't PRINT, and ______________________
when x is 10, it won't go back to alabel.



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

........................................................................
decision making
4 of 9

i'm still using GOTO in these examples, because GOTO is easy to teach.
sometimes, when it's easier to teach, it's easier to learn... but
regardless, you will soon learn a couple statements that help to
eliminate the use of GOTO, and are perfect for the example i just showed
you. in fact, for what i just showed you, there are TWO perfectly
acceptable command pairs that can be used just as easily.

for now, here's another example:





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

........................................................................
decision making
5 of 9

PRINT "what's your name? - " ' *i'm not a chauvinist!
LINE INPUT q$: q$=LTRIM$(RTRIM$(q$)) 'trim spaces
'standard reply
jim$ = "I am James T. Kirk... (Captain... of) "
jim$ = jim$ + "the Starship... Enterprise..."

?:? "are you a female alien? (y/n)"
IF LCASE$(q$)=jim$ AND LCASE$(INPUT$(1))="y" THEN GOTO swoonfornoreason

you see? it doesn't take much to make a program that thinks just like a
female alien, especially one from the 1960's! (I Said "ALIEN!")*


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

........................................................................
decision making
6 of 9

that example uses AND, let's try one that uses OR, and throw in ELSE...
PRINT "what's your name? - "
LINE INPUT q$: q$=LTRIM$(RTRIM$(q$)) 'trim spaces
o$=LCASE$(q$)

IF INSTR(o$,"bones")>0 OR INSTR(o$,"mccoy")>0 THEN
? "i need mr. sulu in sickbay, he thinks we";
? " really need a pilot to fly this thing!"
ELSE
? "damnit, " + q$ +", you're not a doctor!"
END IF


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

........................................................................
decision making
7 of 9

that's an IF... THEN block. if you really want, it works as one long
line that won't fit the screen:

IF INSTR(o$,"bones")>0 OR INSTR(o$,"mccoy")>0 THEN ? "i need mr. sulu
in sickbay, he thinks we";: ? " actually need a pilot to fly this
thing!" ELSE ? "damnit, " + q$ +", you're not a doctor!"

but for that to work you have to actually put it all on the same line.
who would want to? hence, an IF... THEN block is used.

here's a way to check many conditions, using SELECT... CASE:


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

........................................................................
decision making
8 of 9

? "input a number "; : LINE INPUT q$
SELECT CASE VAL(q$)
CASE 1 TO 5: ? "your number is one at least, and five at most."
CASE 6 TO 10
? "your number is six at least, and ten at most."
CASE IS = 15
? "fifteen? what's that got to do with anything?"
CASE IS > 20
? "more than 20? i quit!" ' no particular reason...
CASE ELSE: ? "i don't even understand this answer."
END SELECT


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

........................................................................
decision making
9 of 9

"CASE ELSE" of course, runs the code following that line if none of the
other "CASE" conditions are met. CASE is also really handy for an if
that uses numerous values:

SELECT CASE LTRIM$(RTRIM$(LCASE$(q$)))
CASE "susie", "janie", "jamie", "josie", "connie", "carrie", "mary"
? "didn't we used to date?"
CASE ELSE
? "so what are you doing tuesday?" '* AM NOT! SEE PAGE 5!
END SELECT
-= end of chapter 11 =-


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


click here to go back to the contents page

puppybasic intro ch. 10 - branching within the program

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.

........................................................................
branching within the program
1 of 14




an intro to puppybasic

chapter 10: branching within the program







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

........................................................................
branching within the program
2 of 14

GOTO is easy enough to use, first you'll want a line number:

90

or instead of that, a linelabel:

hey:

using similar rules for naming labels that you do for naming variables.




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

........................................................................
branching within the program
3 of 14

you can then tell the program to stop running exactly where it is
(usually the program runs the first line, then the second line, then the
third line... not anymore!) and start running at that line number:

GOTO 90

or that linelabel:

GOTO hey




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

........................................................................
branching within the program
4 of 14

and the easiest way to use it is to make the program do the same thing
over and over:

10 PRINT "not again! ";
GOTO 10

so why are they all multiples of 10? well, no reason these days. about
100 years ago (20 to 30, actually) you didn't use (by default) a text
editor like the one you use with puppybasic. you had a program that
simply waited for you to type lines, and then if it started with a line
number, it added that line to the whole program...


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

........................................................................
branching within the program
5 of 14

you only saw the lines you were typing, and if you typed LIST and hit
enter, it would show you the program listing. the trick was listing
programs that were more than about 20 lines long :)

you could make it easier to insert lines by "naming" them multiples of
10. that way if you wanted a line between 10 and 20 for instance, you
could just name it 15. you could copy a line to another number by
displaying it and writing the new number over the old, but if you had a
50 line program and all the numbers were increments of 1, not 10, the
need to insert a new line left you in a bad place! :)



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

........................................................................
branching within the program
6 of 14

now you have a bit of basic history. you youngsters don't know how
spoiled you are!

something you'll really want to know if you're going to use goto: when
you get stuck in a loop, you can stop the program from running by
holding down CTRL and hitting the C key... failing that-try CTRL + BREAK
(not now! it might stop this program, too!) this is better than the
CTRL + ALT + DEL, that will end puppybasic as well, or restart your
computer.




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

........................................................................
branching within the program
7 of 14

GOSUB should be used to replace GOTO when possible. by modern standards,
the regular use of GOTO is not considered good coding. GOSUB is slightly
more tricky, but much more useful.

the first thing you want to do is make a stopping point between your
main program and your subroutines. for the purpose of introduction, a
subroutine is a part of your program that is branched to using GOSUB
and left by using RETURN. subroutines are like miniature programs
themselves.




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

........................................................................
branching within the program
8 of 14

for now, put:

END

at the end of your program, to make the program stop. AFTER the END
statement, put all your subroutines.

for instance, here is a subroutine to clear the screen, greet the user,
and change the color back to white:




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

........................................................................
branching within the program
9 of 14

starting:
CLS: COLOR 10
? "** program that does nothing but start **"
? " 2005 by mennonite"
COLOR 7
?
RETURN

from inside your main program, that is, before the end statement, use:

GOSUB starting


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

........................................................................
branching within the program
10 of 14

and it will run the code in the subroutine, then come right back to
where it left off-just after the GOSUB command! you get all that? again:

GOSUB starting: END
starting:
CLS: COLOR 10
? "** program that does nothing but start **"
? " 2005 by mennonite"
COLOR 7
?
RETURN


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

........................................................................
branching within the program
11 of 14

this is one of the most important features of programming in languages
like puppybasic. make sure you become comfortable, when you write a
longer program it's almost impossible to do without these subroutines.

so why did we make the program stop before the subroutines were run? we
didn't, actually, the subroutine ran... only if your program gets to the
end and then starts running the subroutines that follow, it will
encounter the RETURN statement without having any GOSUB calling the
routine... the program will think: (and this is how stupid computers
really are...) "RETURN? return to where?!" and have a fit and refuse to
do more. it's not usually dangerous, but it makes you look incompetent.


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

........................................................................
branching within the program
12 of 14

REM this won't do anything

the old way to add comments to your program code! and it still works...
but it's even easier to use an apostrophe.

' this won't do anything either

REM but more importantly, you can put this '

atthe = endof * aline - 1 'instead of
' the beginning of a new one


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

........................................................................
branching within the program
13 of 14

' comments are good to use, because when someone else looks at your
' code, they have a Much easier time figuring out what it all does.

(and sometimes, when you're looking at a program you wrote a long time
ago, you'll wish there were comments there.)

i usually don't do much with comments until version 2, (0.2 or 0.4 or 5
if there are betas ;) because if my program is good enough to show
people, it's good enough to make a version 2 of, and like too many
people, i'm sort of lazy about comments. please consider the merits of



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

........................................................................
branching within the program
14 of 14

doing what i say, not what i do :) all the same, know you can get flamed
(that is, people may be uncharitable ;) if rarely, online if you write
code that's hard for other people to understand without enough comments.
don't go nuts though, too many comments can be annoying, also. (not as
many will complain about this.)

of course, a really good rule would be: ALWAYS put a comment at the top
of your program that at least says what your program does. you'll go far
that way.

-= end of chapter 10 =-


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


click here to go back to the contents page

puppybasic intro ch. 09 - working with binary files

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.

........................................................................
working with binary files
1 of 7




an intro to puppybasic

chapter 9: working with binary files







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

........................................................................
working with binary files
2 of 7

it might be fun at this point to learn how to save and load some data in
a file, but this will be much more useful when you've learned about
decision making and loops. in the meantime, here is how to work with
binary files:

first of all, be sure to use this statement at the beginning of your
program:
DIM bt AS STRING * 1

so that you have a one byte string to work with. you can make longer
strings, but this one will work for any purpose.


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

........................................................................
working with binary files
3 of 7

always open the file :)

OPEN "filename.txt" FOR BINARY AS #1

if you are only going to read the file, you dont strictly need the bt
variable, and therefore don't need to DIM at the start of your program.
just INPUT a byte:

b$=INPUT$(1, #1)

and that's all there is to that. yes, it IS similar to keyboard input.


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

........................................................................
working with binary files
4 of 7

do it again to read the next byte. usually, you'll probably want to use
this method instead:

GET #1, q, bt
with q being the byte of the file you want to read... if q is 57, then
it will read the 57th byte of the file.

to write, use:
PUT #1, q, bt
where b is a string that's only one byte (or letter) long. it's pretty
much the same idea. some useful functions for working with files are:


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

........................................................................
working with binary files
5 of 7

q = FREEFILE

sets q to the number of the next filenumber that's free... if you've
opened a file as #1, and a second file, as #2, then FREEFILE will equal
3.

LOC(n)

is the byte you're set to read or write at. if you've used INPUT$(1, #1)

50 times already, LOC(1) will be 51.


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

........................................................................
working with binary files
6 of 7

LOF(n)

is worth remembering, LOF(n) is equal to the length of file #n, if you
open a file as #1 and the LOF(1) is 0, that file is empty.

always close the file when you're done working with it:

CLOSE #1

if you are only working with one file, or if you don't have any other
files that you need to leave open, using close with no file numbers:


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

........................................................................
working with binary files
7 of 7

CLOSE

will close all files.


-= end of chapter 9 =-







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


click here to go back to the contents page

puppybasic intro ch. 08 - strings part ii

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.

........................................................................
strings part ii: searching and manipulation
1 of 8




an intro to puppybasic

chapter 8: strings part ii







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

........................................................................
strings part ii: searching and manipulation
2 of 8

although you could find a better routine to do the same thing, INSTR is
a very fast and simple way to see if and when a string exists as part of
a larger string: for instance:

suppose q$ is storing the string:
"the quick brown fox jumped, and stuff."

and you want to know if the string (let's pretend you don't know what q$
is; maybe it's from a file or the keyboard.) contains the word "fox" so
you code:



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

........................................................................
strings part ii: searching and manipulation
3 of 8

n = INSTR(q$, "fox")

n will be equal to the number of letters, numbers, and punctuation that
you have to read before you've started reading "fox"... in this case,
the "f" in "fox" is the 17th byte in q$, so INSTR will set n to 17. if
the string "fox" is not found in the string that q$ is storing, n will
be set to 0.
1
123456789012345 7 90123456789012345678
"the quick brown fox jumped, and stuff."



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

........................................................................
strings part ii: searching and manipulation
4 of 8

SPACE$

is really, really easy to use. SPACE$(n) is a string of ascii 32 (space)
that is n bytes long. q$=q$+SPACE$(10) will add 10 spaces to the end of
q$.

LEFT$ and RIGHT$

are not as powerful or complete as MID$ but are slightly easier to use.
skipping straight to examples, LEFT$("Seventy",5) is "Seven" and
RIGHT$("elephant",3) is "ant". MID$ is similar:


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

........................................................................
strings part ii: searching and manipulation
5 of 8

the syntax for the MID$ function is:

MID$(q$, whichbytetostartat, howmanybytes)

so n$=MID$("hello", 2, 4) would set n$ to "ello" and
n$=MID$("yeah, or... something", 14, 2) would set n$ to "om".

there is a MID$ statement as well, which works the same way, except
instead of reading a string it will rewrite it:




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

........................................................................
strings part ii: searching and manipulation
6 of 8

y$="yeah, or... something"
n$=SPACE$(2)+"no"

MID$(y$, 13, 4)=n$

will store "yeah, or... nothing" in y$. the 4-byte "some" has been
replaced with 2 spaces and "no", changing "something" to " nothing".
whoosh!





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

........................................................................
strings part ii: searching and manipulation
7 of 8

if you do not like the MID$ statement for rewriting, it is totally
possible to avoid it entirely using only the MID$ "reading" function
and concatonization of strings.

there are times (especially with code like the first menu routine,
earlier) when your variable will have leading or trailing spaces that
you don't want. when you use STR$(n) to convert a numeric variable to a
string for instance, it will contain a leading space (if it is at least
zero) for a plus sign, even though the plus sign will never show. if the
number drops below zero, a negative sign will show, but otherwise, you
may be stuck with a space you don't want, like this: " 55".


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

........................................................................
strings part ii: searching and manipulation
8 of 8

LTRIM$(STR$(n))

will remove the leading spaces from a number, and LTRIM$(RTRIM$(q$))
will remove the spaces from both ends of any string. this is not only
useful when changing numbers to strings, but also when accepting
keyboard input or data from files.


-= end of chapter 8 =-




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


click here to go back to the contents page

puppybasic intro ch. 07 - keyboard and data lines

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.

........................................................................
keyboard and data lines
1 of 15




an intro to puppybasic

chapter 7: keyboard and data lines







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

........................................................................
keyboard and data lines
2 of 15

let's get DATA statements out of the way, since i find them irritating,
although you'll want to know how they work when you see them in someone
else's code.

DATA statements (though as i said, i don't like them) are easy enough to
use.

anywhere in the main module of the program (don't even worry about what
that is) put the statement DATA followed by the strings or numbers you
want read, separated by commas:



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

........................................................................
keyboard and data lines
3 of 15

DATA ugh., data, statements-, that takes, me back.

READ q$: ? q$; ________________________________________________________
READ q$: ? q$; _ugh.datastatements-that takesme back.__________________
READ q$: ? q$; ________________________________________________________
READ q$: ? q$; ________________________________________________________
READ q$: ? q$;
there are better ways, but this works. if you use DATA statements, you
may find that you want to put something unique as the last string or
number so that your routine can "know" when it's done reading, but
you'll need loops and decision making (covered later) to pull it off.


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

........................................................................
keyboard and data lines
4 of 15

if you READ a number of times greater than your number of data entries,
you get an error and your program stops. to avoid this, don't read a
greater number of times than your number of entries. (heh.) along with a
unique last entry, you can reread from the beginning of your data
statements. (puppybasic looks at all of them together, from the
beginning of your program to the end, as one long data statement) by
putting an old fashioned line number (like 20) before your first data
statement, which then allows you to use the command:

RESTORE 20



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

........................................................................
keyboard and data lines
5 of 15

whenever you want to reread. you can also move to different places (in
your single virtually continuous data line) but putting line numbers
between DATA statements:

RESTORE 70: READ b$: PRINT b$ + ",";: READ b$: PRINT b$; "."

50 ________________________________________________
DATA 5,6,7,8,9 _ok,that's enough.______________________________
70 ________________________________________________
DATA ok, that's enough ________________________________________________
________________________________________________


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

........................................................................
keyboard and data lines
6 of 15

i think the most versatile way to get keyboard input is with the INKEY$
function, but it doesn't wait for input: it just says "hey! keyboard,
what's happenin'?" and unless a key has been pressed already the
keyboard just says "..." and INKEY$ returns an empty, 0-length string...
not even a space, just the nothing between two quotes, side by side: "".

there's nothing wrong with this, it's just that INKEY$ only works in a
loop, so i'll demonstrate INKEY$ after showing how loops work. in the
meantime, INPUT$(n) is similar, and not too shabby. i'll show you that
momentarily. in order to appreciate the simple beauty of INPUT$(n) you
have to see the alternatives:


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

........................................................................
keyboard and data lines
7 of 15

INPUT "", q$

you can put stuff in the quotes, and then it's like a PRINT statement,
but i prefer using PRINT to print and INPUT for input. one advantage of
separating the two is that you can put the instructions and the visible
end of the keyboard input on separate lines or different locations on
the screen. i'm really trying to set a good example here by showing
INPUT used with a comma - you could use a semicolon: INPUT ""; q$

but INPUT "please type your name - "; q$ ______________________________
will show up on the screen like this: _please type your name - ?____


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

........................................................................
keyboard and data lines
8 of 15

because as we all know the ONLY way to get information from the user is
in the form of a full sentence followed by a question mark :|

ah well. this is why using a comma is better.

of course, i don't actually recommend using the INPUT statement, ever.
LINE INPUT is used the same way you just learned (see? that wasn't in
vain) but is more reliable. i'll explain:

say for instance, you used this code:



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

........................................................................
keyboard and data lines
9 of 15

INPUT "enter your name - ", n$ _____________________________________
and say for instance, he entered: _enter your name - smith, john q.____
_____________________________________
then he would see this message: _REDO FROM START?____________________
_____________________________________
great! what? _____________________________________

it's because he typed a comma. the INPUT statement is designed so that
you can get more than one piece of data from the same prompt, separated
by commas, like this:



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

........................................................................
keyboard and data lines
10 of 15

INPUT "please enter three numbers, put commas between the first
and second, and also between the second and third, and don't forget
them, and don't put too many, or my input routine will yell at you.
thanks. ", a, b, c

assuming your plucky user gets it exactly right, the clever INPUT
statement will save an extra line of code.


Bad Design!



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

........................................................................
keyboard and data lines
11 of 15

so i never use the INPUT statement. ever. instead i use this when i want
a no fuss, two line method of getting keyboard input (there are better
ways using INKEY$, remember?)

PRINT "please enter your name. as long as your name has less than 250
characters and doesn't contain carriage returns, we shouldn't have any
problems:": ?: line input n$

all this is okay, for typing in a line of data and hitting enter. if you
have a menu on the other hand:



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

........................................................................
keyboard and data lines
12 of 15

1. do something

2. do something else

3. do something entirely different

4. exit

then the following prompt might seem reasonable:

? "please enter your selection - ";: LINE INPUT q$


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

........................................................................
keyboard and data lines
13 of 15

but i think this is better:

1. do something

2. do something else

3. do something entirely different

? "select an option (1-3) or hit [esc] to exit :)": q$=INPUT$(1)




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

........................................................................
keyboard and data lines
14 of 15

now q$ will be exactly one byte long and be a 1, or 2, or 3, or esc
(esc is ascii 27.) it might be something else, but you can check to see
if it's one of the acceptable options, and repeat until it's a key you
are looking for. yeah, i'll get to looping.

you can also change the "1" in INPUT$(1) to another number, 5 for
instance, if you want exactly 5 keys input to the program. this too, is
bad design because the person can't see what they're typing, and can't
backspace if they hit a wrong key.




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

........................................................................
keyboard and data lines
15 of 15

usually, i'll use INKEY$. in a loop, INKEY$ can act just like INPUT$(n)
if you want it to and can detect arrow and function keys as well.


-= end of chapter 7 =-








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


click here to go back to the contents page

puppybasic intro ch. 06 - more with text

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.

........................................................................
more with the text screen
1 of 7




an intro to puppybasic

chapter 6: more with text







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

........................................................................
more with text
2 of 7

with PRINT, COLOR, and CLS already covered, here are some more ways to
work with the text-only screen:

LOCATE y
will tell puppybasic to start printing on row y of the screen.

or

LOCATE y, x
well tell puppybasic to print at row y, column x. x is usually 1 to 80.



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

........................................................................
more with text
3 of 7

though it flies in the face of cartesian graphs, descartes was a bit of
a jerk after all, and it makes enough sense to address text as row,
column. the LOCATE command lets you do this, but you may not be able to
do anything on the bottom row (this tends to be the 25th one) unless
your PRINT command uses a semicolon;

LOCATE 25, 1
PRINT "print with a semicolon on the end to print on the bottom row.";

________________________________________________________________________
_print with a semicolon on the end to print on the bottom row.__________


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

........................................................................
more with text
4 of 7

also, making the cursor fly back and forth can be sort of annoying, so:

LOCATE y, x, 0 ...makes it go away and (when you want it to show where
someone is going to be typing, for instance)

LOCATE y, x, 1
makes it come back.






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

........................................................................
more with text
5 of 7

now that you are thinking in terms of coordinates,

CSRLIN
represents the row the cursor is on, and

POS(0)
represents the column. if you print something to the screen, let
a = CSRLIN and b = POS(0), LOCATE to some point on the screen (say the
top lefthand corner,) PRINT something there and then LOCATE a, b, you
will then pick up where you left off. this sort of thing can be useful
for status bars.


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

........................................................................
more with text
6 of 7

VIEW PRINT

is odd but i find it useful: where the text-only screen usually has 25
rows, you can force smaller ranges. VIEW PRINT 10 to 17 keeps the text
currently displayed on all 25 lines, but when you print within lines 10
to 17 it will only scroll within (and change) lines 10 through 17.

q = SCREEN (y, x)
will set the variable q to the ascii value of whatever character is at
position y, x, so that you can not only print to the screen, but read
from it as well.


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

........................................................................
more with text
7 of 7

WIDTH columns, rows

can be useful, but what you can enter as acceptable values for columns
and rows depends on what screen mode you're using. in text only mode,

WIDTH , 50
gives you twice as much top-down room to work with (try 43 too), but

WIDTH 40 (horizontal) doesn't work reliably. this is covered later with
the SHELL command.
-= end of chapter 6 =-


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


click here to go back to the contents page

puppybasic intro ch. 05 - strings part i

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.

........................................................................
strings part i: the indispensable
1 of 7




an intro to puppybasic

chapter 5: strings part i







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

........................................................................
strings part i: the indispensable
2 of 7

although you need to talk to the computer to write a program, and you
usually need math to talk to the computer, you usually need strings
(which may contain plain english) to talk to the person using the
computer. for this and other reasons, strings are generally as vital to
your program as numbers. here are some statements to work with strings:

UCASE$

q$=UCASE$(q$) will convert q$ to an all uppercase copy of itself. the
lowercase version is:



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

........................................................................
strings part i: the indispensable
3 of 7

LCASE$

q$=LCASE$(q$) when you've printed "press q to quit" can be the
difference between a person being able to exit your program when they
want to and being able to a bit later (when someone points out that
their caps lock light is on.) it is possible to turn off someone's caps
lock for them using program code, but processing strings with LCASE$ is
easier and more considerate.
_________________________________
q$="NAME@PUPPY.Com": PRINT LCASE$(q$) _name@puppy.com__________________
_________________________________


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

........................................................................
strings part i: the indispensable
4 of 7

CHR$ is indispensable: there are 256 different ASCII characters, (for
example, the lowercase letter "a" is ASCII 97, and a space, like from
the spacebar, is ASCII 32.) including letters, numbers, punctuation, and
other fun symbols (ASCII 01 is a smiley face. ☺ for the most part, the
only way to produce some of them is with CHR$. for instance, if you want
to produce a double quote on the screen, or within a string, you can use
the following code: (put it all on the same line, of course...)
? "i have been learning about puppybasic from "+CHR$(34)+"an intro to
puppybasic"+CHR$(34)+" and it's okay..."

...as ASCII 34 is the same double quote you would get from the keyboard.


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

........................................................................
strings part i: the indispensable
5 of 7

+------+---------------------------------------------------------------+
| CHR$ | also lets you "print" shadows and lines using text symbols... |
+------+---------------------------------------------------------------+
the reverse of CHR$, the ASC function, gives you the code number for the
first byte of a string:

? ASC("a") -3 _________________________________________________________
will print: __94_____________________________________________________
_________________________________________________________

as ascii 97, in puppybasic coded as: CHR$(97) is "a", and 97 -3 is 94.


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

........................................................................
strings part i: the indispensable
6 of 7

LEN... n = LEN(q$) will set n to a number equal to the number of bytes
in a string - examples: LEN("hello") is equal to 5, LEN("how are you?")
is equal to twelve. spaces and punctuation count.

lastly, as you can convert a string to a number using VAL, you can
convert a number to a string using STR$ so:

sp = 5
q$ = "you pressed spacebar" + STR$(sp-1) + "times."

will store the string: "you pressed spacebar 4 times." in q$.


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

........................................................................
strings part i: the indispensable
7 of 7

you are well on your way to learning puppybasic! the best thing you can
do with this tutorial is read it slowly, think about each page and what
kind of program you could write with each command, and experiment with
the code in puppybasic (type it in, you learn faster that way, even
though you *could* cut and paste...) for a feel of what coding is really
like.

and never forget to have fun.

-= end of chapter 5 =-



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


click here to go back to the contents page