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. 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

21 comments:

Anonymous said...

buy xanax xanax drug interactions with lexapro - xanax withdrawal 1mg per day

Anonymous said...

alprazolam xanax overdose facts - what does xanax and alcohol feel like

Anonymous said...

buy tramadol online order tramadol florida - tramadol vs codeine

Anonymous said...

xanax 2mg blue xanax pills generic - xanax dosage yellow pill

Anonymous said...

buy tramadol online tramadol 450 mg - legal buy tramadol usa

Anonymous said...

xanax online xanax mg get high - xanax show up in drug test

Anonymous said...

buy tramadol online tramadol dosage for humans - buy tramadol cod overnight delivery

Anonymous said...

buy cialis professional cheap cialis in australia - buy cialis online greece

Anonymous said...

cialis online pharmacy cheap 2.5 mg cialis - reviews viagra cialis levitra

Anonymous said...

xanax online generic xanax pictures of pills - xanax side effects menstrual cycle

Anonymous said...

xanax online 1mg xanax much klonopin - side effects of xanax .25 mg

Anonymous said...

buy cialis super active cialis online test - cialis online order

Anonymous said...

cialis online generic cialis south africa - purchase cheap cialis soft tabs

Anonymous said...

cialis online cialis price in costco - once daily cialis side effects

Anonymous said...

buy tramadol overnight cod tramadol online no prescription overnight cod - can you buy tramadol over the counter in the us

Anonymous said...

buy tramadol online buy from online texas tramadol - tramadol grapefruit

Anonymous said...

http://blog.dawn.com/dblog/buy/#41639 tramadol headache - tramadol high opiate tolerance

Anonymous said...

buy tramadol tablets tramadol and addiction - 100mg tramadol lot

Anonymous said...

buy ativan online ativan after alcohol - ativan dosage vs valium

Anonymous said...

buy tramadol online buy tramadol online us - tramadol hcl 50 mg and tylenol

Anonymous said...

http://ranchodelastortugas.com/#50698 xanax generic pictures pills - alprazolam 0.5 mg tab