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

0 comments: