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
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
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment