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

Saturday, February 3, 2007

howto change the desktop icons

to change an icon right click and "set icon." to set the icon, drag the desired image from the icons folder (most are in /usr/local/lib/X11/pixmaps or /usr/local/lib/X11/mini-icons) right onto the box in the middle if the set icon window- typing is optional!

to create an icon, drag the script or binary for it (this may be the only needed step...) from /usr/local/bin (or any other folder) right onto the desktop.

to edit an icon image, goto the icons folder from mtpaint and open it, or goto the icons folder in rox and right click... "open with" and open it it mtpaint. (the second method may not always work.)

you can zoom in on your image by pressing "7" on your keyboard, or other numbers, nothing that "4" will go back to regular size.

most icons are xpm or png, you can use the "transparent" color by selecting from the background with [CTRL]+Paint tool, or you can set a new background color by selecting "save as..." from the mtpaint menu, checking "set transparancy index" and selecting the number next to the new color in the palette. you may need to "convert to indexed" from the "image" menu, but you probably won't.

after saving the image to the same file you loaded it from, the icon on the desktop will refresh after you hover the mouse cursor over it, if not sooner.

ktwi: puppy 2.11, *should* work also in 1.07 and most (or all) other versions...

thanks to: amish

Friday, February 2, 2007

puppybasic intro ch. 17 - other useful statements

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.

........................................................................
other useful statements
1 of 7




an intro to puppybasic

chapter 17: other useful statements







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

........................................................................
other useful statements
2 of 7

SHELL calls a dos command. dos of course, is not only capable of
managing operations with files and folders, it can call other pieces
of software. running large programs from shell may result in an "out
of memory" error from dos. (usually not an issue.)

here's a nice example for SHELL:
SHELL "dir *.* /a /b > dirfile.txt"

this creates a file called "dirfile.txt" containing a list of all
files in the current folder. then you can load the list with the OPEN
command.


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

........................................................................
other useful statements
3 of 7

mode co40 is another useful dos command, but doesn't always (if ever)
work in windows xp. (for some reason, every time windows makes a new
dos emulator it does even less of the stuff that dos or the previous
dos emulator did.)

it is better still to use mode con cols=40, which works in xp and can
be used in puppybasic like this:

SHELL "mode con cols=40"




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

........................................................................
other useful statements
4 of 7

this is better than width 40, as it's more likely to work. i knew of
one project that wouldn't stay in WIDTH 40 unless you first shelled to
run this dos command, then used WIDTH 40 as well.










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

........................................................................
other useful statements
5 of 7

COMMAND$

quickbasic (compiler) only. reads the arguments typed next to
a standalone exe's name, from dos or windows, so if your compiled
program was called appli01.exe and you were to type:

c:\winnt\>appli01 New.txt /fs
at the dos prompt (for instance) and you put this at the beginning of
your program:

q$=COMMAND$ 'then the string q$ would contain: "New.txt /fs"


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

........................................................................
other useful statements
6 of 7

SYSTEM

use this instead of END, this is only useful when using the
interpreter. it ends the program, but if the
interpreter is running, exits the interpreter also.
this only works if you've run a program using /run
[file].bas or /run file... etc. (from a script for instance.)

an example:




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

........................................................................
other useful statements
7 of 7

SYSTEM
END 'you don't need END here, but it hurts nothing and is easy to spot
'when other people are reading your code.


-= end of chapter 17 =-







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

click here to go back to the contents page

puppybasic intro ch. 16 - arrays

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.

........................................................................
arrays
1 of 7




an intro to puppybasic

chapter 16: arrays







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

........................................................................
arrays
2 of 7

arrays make variables more useful and offer a powerful way to handle
data. for instance: instead of the following code:

READ a
READ b
READ c
READ d
READ e: PRINT a;b;c;d;e;

DATA 52,21,19,45,73



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

........................................................................
arrays
3 of 7

you could replace it with this:
FOR q=1 TO 5
READ ab(q)
NEXT q

FOR q=1 TO 5
? ab(q);
NEXT q

...which seems unspectacular until you have a bit more than five
variables to name.


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

........................................................................
arrays
4 of 7

another advantage of arrays is that you don't need to know exactly how
many items you will be storing. for instance:

DIM q$(1 to 100)

OPEN "file" FOR INPUT AS #1
DO: c=c+1
LINE INPUT #1, q$(c)
LOOP UNTIL EOF(1) or c => 99

q$(c+1) = CHR$(26): c=0: close


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

........................................................................
arrays
5 of 7

will open "file" and store each line in q$(c), c standing for which line
is stored there. this example limits the possible number of lines to 99,
and sets the last line to ascii 26 to mark it as the last.

note the use of DIM(1 to 100) which is required for arrays more than 10
units in size. it is also necessary to use dim for multidimensional
arrays:






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

........................................................................
arrays
6 of 7

DIM twod(1 to 5, 1 to 5) AS INTEGER 'integers let you use wider arrays

will allocate an array of 5 units, and each unit will also have 5
subunits:

twod(1,1) twod(2,1) twod(3,1) twod(4,1) twod(5,1)
twod(1,2) twod(2,2) twod(3,2) twod(4,2) twod(5,2)
twod(1,3) twod(2,3) twod(3,3) twod(4,3) twod(5,3)
twod(1,4) twod(2,4) twod(3,4) twod(4,4) twod(5,4)
twod(1,5) twod(2,5) twod(3,5) twod(4,5) twod(5,5)



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

........................................................................
arrays
7 of 7

you may find that although 3-dimensional arrays are possible, but that
the limitations won't allow a 3d array large enough to be useful all
that often. (i do know someone who used one for a card game, and you may
be able to use one for a program to solve rubik's cube... although such
a program already exists.)

incidently, puppybasic tends to demand that most DIM statements come at
the beginning of a program, but you can change the limits of some arrays
using REDIM.

-= end of chapter 16 =-


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

click here to go back to the contents page

puppybasic intro ch. 15 - timing

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.

........................................................................
timing
1 of 7




an intro to puppybasic

chapter 15: timing







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

........................................................................
timing
2 of 7

vital to graphics and games, and useful for many other things you'll
want to do, the following are common functions for accessing the
timer inside the computer and its clock functions:

TIMER
(mentioned earlier for use with RANDOMIZE) is a numeric function that
returns the number of seconds that have elapsed since midnight.

you can code a delay using TIMER this way...

... (only joking, it's on the next page.)


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

........................................................................
timing
3 of 7

delay = .5 'delay in seconds, in this case: half of one second
t = TIMER + delay: DO: LOOP UNTIL TIMER > t or TIMER < t - delay * 1.2

it is also possible to create a short delay using FOR q=1 TO d: NEXT q
where d is a large number, but the faster computers get, the more
useless this method becomes. also it is unreliable in that the delay is
longer on slower machines, and that the delay it creates can't be
determined exactly on any machine but the one you try it on.





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

........................................................................
timing
4 of 7

SLEEP

makes a program suddenly sit and do absolutely nothing until a key is
pressed, and if used with a number:

SLEEP 5

works the same way, only gives up waiting for a key after that number of
seconds.




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

........................................................................
timing
5 of 7

if used enough times in a program, (especially in a loop,) the keyboard
buffer will fill, which adds up to annoying clicking noises. you can
clear this buffer using INKEY$, but i think it makes more sense to
substitute q$=INPUT$(1) for SLEEP, or a variation of the timer code
example (throw in an INKEY$ function) in this chapter for SLEEP N, and
i usually only use SLEEP when i'm busy enough doing other things to
avoid typing parenthesis: (example: SLEEP is very useful during
debugging.)





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

........................................................................
timing
6 of 7

TIME$
has the double use of checking and setting the time.

to check the time, let q$=TIME$ which will set q$ to an 8-byte string
comprised of "hh:mm:ss" with hh being a pair of hour digits, mm being
minutes, and ss being seconds.

to set the time, create a string in the same 8-byte "hh:mm:ss" format
and let TIME$=q$

easy!


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

........................................................................
timing
7 of 7

DATE$ works like TIME$ and checks and sets the date with the format
month-day-year as "mm-dd-yyyy"

note: if your computer is outside the united states, it's possible that
your OS and thus puppybasic will use a different date display.


-= end of chapter 15 =-





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


click here to go back to the contents page

puppybasic intro ch. 14 - errors

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.

........................................................................
errors
1 of 10




an intro to puppybasic

chapter 14: errors







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

........................................................................
errors
2 of 10

you could do well to always trap and handle errors, i almost never
bother. if you write your program well, you might manage to avoid it.
this applies less to large programs. of course, sometimes error trapping
is quite useful.

whether you use error handling or not, your program, upon encountering
an error, can tell the user the name of the error. if you do not trap
your errors, the program will end as well. if you do use error handling,
you have the option of telling the user a more friendly interpretation
of the problem:
"I'm sorry, no file by that name exists in this folder."


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

........................................................................
errors
3 of 10

or even fixing it so the program can continue:

"That file does not exist. Create? (Y/N)"

you can even trap errors to detect which screen modes don't work. as
mentioned in the graphics section, there are several screen modes in
puppybasic, and they start with SCREEN 0, or text-only mode and range
from 0 to 13. so, you can tell your program to go to all the possible
screen modes: (none of this is true, this tutorial is being edited.)




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

........................................................................
errors
4 of 10

okscreen$ = SPACE$(14)
ON ERROR GOTO oops
FOR q = 0 TO 13
SCREEN q
NEXT q: nomorec = 1

and then when it tries to SCREEN [not an available mode] it will cause
an error 5, and GOTO the "oops" routine:





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

........................................................................
errors
5 of 10

oops:
IF nomorec = 0 THEN
IF ERR = 5 THEN MID$(okscreen$, q + 1) = "x"
END IF
RESUME NEXT

which adds an "x" under the part of the variable okscreen$ for that
mode.





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

........................................................................
errors
6 of 10

the command RESUME NEXT tells the program "don't worry about that last
error..." and the program then runs the next line, which is the "NEXT q"
of the FOR... NEXT loop.

when you're done trying all the modes, you can go back to SCREEN 0,
which is the default, and should always work:

SCREEN 0: WIDTH 80 'WIDTH 80 should always work, otherwise, remove it.

then print a guide for the numbers 0 to 13:



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

........................................................................
errors
7 of 10

PRINT " 1111"
PRINT "01234567890123"

and under that:

PRINT: PRINT okscreen$
END 'don't forget to put an END statement between
'your program and routines.

now your program will put a little "x" under the modes that don't work
on your computer!


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

........................................................................
errors
8 of 10

if we went over this section too fast: here is a review:

ON ERROR... GOTO linelabel

tells the program what routine to goto when an error occurs. there is no
"ON ERROR... GOSUB."

IF ERR=n THEN

will do something in response to the specific error... (you can also use
"SELECT CASE ERR" for handling this part.)


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

........................................................................
errors
9 of 10

RESUME NEXT

goes back to the part of the program where the error occured, but it
goes to the first line AFTER the problem.

note: error code 5 is "illegal function call" - the error you get when
you try to change to an unavailable mode, although there are other
possible causes.





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

........................................................................
errors
10 of 10

there are at least a couple other commands for error handling, but
unless you want them enough to look them up, you probably won't ever
need them.


-= end of chapter 14 =-







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


click here to go back to the contents page

puppybasic intro ch. 13 - working with text 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 text files
1 of 8




an intro to puppybasic

chapter 13: working with text files







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

........................................................................
working with text files
2 of 8

working with text files is a bit different than working with binary
files... for one, the way you open the file depends on whether you will
be reading or writing. for this reason and a few others i prefer to
handle text files as if they were binaries and open them for binary
file access. you may find this is easier when you're starting out, you
may not. often i am too lazy and instead open text files as text files.
to print the following lines to a file:
this is a line. skip the next one:

okay, this is another line...
this is, too.


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

........................................................................
working with text files
3 of 8

always OPEN the file:

OPEN "filename.txt" FOR OUTPUT AS #1

if you do it this way it will start from scratch... that is,
"filename.txt" will be BLANKED if it wasn't before. for this reason you
will use APPEND for now. APPEND, instead of erasing the file, adds to
the end of it. if you are writing to a log, this is exactly what you
want. if you are only storing the data for a saved game file, this may
be problematic. in that case you may prefer OPEN... FOR OUTPUT.
regardless:


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

........................................................................
working with text files
4 of 8

OPEN "filename.txt" FOR APPEND AS #1

now we'll use the PRINT statement to write our lines to the file. the
file currently open is #1, so we'll use PRINT #1, q$

PRINT #1, "this is a line. skip the next one:"
PRINT #1, ""
PRINT #1, "okay, this is another line..."
q$ = "this is, too."
PRINT #1, q$



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

........................................................................
working with text files
5 of 8

the last line was printed differently to demonstrate again that a string
is, after all, a string, and that you can PRINT #1, q$ as well as PRINT
an expression in quotes. both of these are examples of valid strings.

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:
CLOSE
will close all the files your program has opened.


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

........................................................................
working with text files
6 of 8

now to read the file, we open it again, this time for input:
OPEN "filename.txt" FOR INPUT AS #1

the text file is read one line at a time. one of the most reliable ways
to do this is to use line input from within a loop:








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

........................................................................
working with text files
7 of 8

DO: LINE INPUT #1, q$

PRINT q$ 'print the line to the screen

the program has to know when to stop looping. EOF(1) is a condition that
will be true when the end of the file is reached. it can end the LOOP
this way:
LOOP UNTIL EOF(1)

you may find This LOOP superior for reading text files:
DO WHILE EOF(1) = 0: LINE INPUT #1, q$: LOOP 'it handles 0-length files.


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

........................................................................
working with text files
8 of 8

if you only wanted to read the first line of course, the loop isn't
needed, only line input #1, q$

close the file when you're done with it. no files are open that need to
stay open, so it's alright to just say:

CLOSE


-= end of chapter 13 =-



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


click here to go back to the contents page

puppybasic intro ch. 12 - looping 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.

........................................................................
looping within the program
1 of 9




an intro to puppybasic

chapter 12: looping within the program







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

........................................................................
looping within the program
2 of 9

as promised, here are two ways to remove GOTO from the earlier example:

x=0
alabel:
x=x+1
IF x > 5 THEN ? x ' > means "more than"
IF < 10 THEN GOTO alabel ' < means "less than"

one way is DO... LOOP. it sort of works like this:
alabel:
goto alabel unless condition is met...


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

........................................................................
looping within the program
3 of 9

but it's better:

DO: 'no line label needed!
LOOP UNTIL cndtn=1

and the example:
x=0
DO
x=x+1
IF x > 5 THEN ? x
LOOP UNTIL x >= 10 ' >= means "more than or equal to"


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

........................................................................
looping within the program
4 of 9

if you had a very long version of this:

x = 0
DO:
x = x + 1: IF INKEY$ = CHR$(27) THEN EXIT DO
'stop loop if ESC pressed ^^^^
IF x > 5 THEN PRINT x
LOOP UNTIL x >= 2 ^ 17 ' >= means "more than or equal to"

you could stop in the middle with INKEY$!
(2 ^ 17 of course, is 2 raised to the 17th power)


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

........................................................................
looping within the program
5 of 9

as mentioned before, INKEY$ will not wait for a keypress. if you've been
pressing a key (in this loop, INKEY$ is checked many times each second)
then INKEY$ will detect it (there are some exceptions, like the CTRL
key...) but the program just keeps running whether a key is pressed or
not. it is also possible to pickup the most recent keypress using
INKEY$, because there is a buffer that tracks recently pressed keys. it
can get full, especially if you hold a key down, and you can use INKEY$
to clear it and prevent the computer from beeping at you.

EXIT DO, as used here, stops the DO... LOOP and moves on to whatever
part of the program is after that. another way to do this is with OR:


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

........................................................................
looping within the program
6 of 9

x = 0
DO:
x = x + 1
IF x > 5 THEN PRINT x
LOOP UNTIL x >= 2 ^ 17 OR INKEY$ = CHR$(32)
'the spacebar sends an ascii 32.


FOR... NEXT
is the way to loop with a number range.



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

........................................................................
looping within the program
7 of 9

'this is the shorter loop from before, done with FOR... NEXT

FOR x=1 TO 10
IF x>5 THEN ? x
NEXT x

or even:

FOR x=5 TO 10
? x
NEXT x


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

........................................................................
looping within the program
8 of 9

to go in increments of less than 1, try STEP:
FOR x=5 TO 10 STEP .5
? x ' will print 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10
NEXT x

it adds .5 instead of 1. and you can go backwards, and over 1 at a time:

FOR x=10 TO 1 STEP -2
? x ' will print 10, 8, 6, 4, 2... but not 1, because it will get to 2,
'and STEP -2 again, which is 0...or less than the range 10 to 1, get it?
NEXT x


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

........................................................................
looping within the program
9 of 9

to recreate the functionality of INPUT$(1) using INKEY$, use this line:

DO: q$=INKEY$: LOOP UNTIL q$ <> "" ' <> means "not equal to"


by the way, you might want to know the Other kind of DO... LOOP:

DO WHILE condition = true 'makes sense?
LOOP

-= end of chapter 12 =-


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


click here to go back to the contents page

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