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

35 comments:

Anonymous said...

alprazolam drug withdrawal symptoms of xanax - generic for xanax

Anonymous said...

tramadol 50mg tramadol withdrawal night sweats - tramadol withdrawal cold turkey

Anonymous said...

generic xanax xanax 1 mg - effects mixing xanax klonopin

Anonymous said...

generic xanax xanax used opiate withdrawal - xanax generic or brand

Anonymous said...

buy tramadol online where to buy tramadol online usa - dangers tramadol addiction

Anonymous said...

buy tramadol online tramadol overdose how much does it take - order tramadol online safely

Anonymous said...

buy tramadol online buy tramadol order cheap tramadol online - tramadol y sus efectos secundarios

Anonymous said...

generic xanax 2mg xanax vs 2mg klonopin - xanax withdrawal brain zaps

Anonymous said...

generic xanax xanax drug test half life - zoloft and xanax for anxiety

Anonymous said...

xanax online xanax the side effects - overdose on xanax death

Anonymous said...

buy tramadol cod fedex buy tramadol usa - tramadol 100mg buy online

Anonymous said...

carisoprodol 350 mg medicamento carisoprodol 350 mg - carisoprodol 350 mg drug interactions

Anonymous said...

buy tramadol online buy tramadol online nz - tramadol 50 mg kapsler

Anonymous said...

generic xanax xanax withdrawal rage - has bought xanax online

Anonymous said...

order alprazolam no prescription xanax and alcohol bac - generic xanax pictures mg

Anonymous said...

buy carisoprodol muscle relaxers soma carisoprodol - carisoprodol 350 mg overdose

Anonymous said...

where to buy cialis online no prescription generic cialis 20 - cialis daily for bph

Anonymous said...

cialis online viagra cialis online usa - buy cheap cialis today

Anonymous said...

buy generic cialis cialis 5mg tablets price - buy cialis blog

Anonymous said...

buy cialis online with paypal cialis interactions - cialis online 2.5mg

Anonymous said...

buy tramadol buy tramadol - tramadol 50mg side effects for dogs

Anonymous said...

learn how to buy tramdadol tramadol for dogs maximum dosage - tramadol 50 mg reviews

Anonymous said...

http://buytramadolonlinecool.com/#50897 tramadol overdose quantity - buy tramadol personal check

Anonymous said...

Burberry Handbags 542213 Louis Vuitton Handbags outlet 686458 Cheap Gucci Bags 179811 http://hermesbagsoutletsale.cabanova.com/

Anonymous said...

Chanel Outlet 892013 Louis Vuitton Bags sale 977342 Gucci Bags online 939145 http://hermesbagsoutletsale.cabanova.com/

Anonymous said...

http://ranchodelastortugas.com/#51462 xanax online no prescription australia - lorazepam or xanax for anxiety

Anonymous said...

Hermes Bags 205083 Louis Vuitton Handbags 177445 Gucci Outlet 127052 http://guccihandbagsforcheap.cabanova.com/

Anonymous said...

Chanel Handbags on sale 84212 christian louboutin outlet 167773 Hermes Outlet 481496 http://cheapburberrybagsonline.cabanova.com/

Anonymous said...

christian louboutin outlet 893496 Cheap Chanel Handbags 968960 Gucci Outlet 773156 http://hermesbagsoutletsale.cabanova.com/

Anonymous said...

buy tramadol online tramadol 50 mg bijsluiter - tramadol dose for kids

Anonymous said...

Louis Vuitton Bags 798960 Burberry Bags 847591 christian louboutin outlet 733627 http://pinterest.com/louboutioutlet/

Anonymous said...

Gucci Outlet 630739 Cheap Louis Vuitton Bags 470 Gucci Handbags online 448736 http://discountlouisvuittonbags.cabanova.com/

Anonymous said...

cheap christian louboutin 298144 Gucci Bags 44614 hermes birkin handbags 691279 http://hermesbagsoutletsale.cabanova.com/

Anonymous said...

Chanel Handbags 253094 Gucci Outlet 352346 Hermes Bags on sale 751861 http://cheapburberrybagsoutlet.cabanova.com/

Anonymous said...

buy tramadol online signs tramadol overdose dogs - tramadol withdrawal st john's wort