Friday, May 20, 2011

Beginning Programming with Just BASIC -- Tutorial 3: IF Constructs



Tutorial 3: IF Constructs, Boolean Operators and Branch Labels

Sometimes you only want to run a portion of the code if a certain condition is met. This is another basic part of programming, and can be accomplished using IF-constructs. The most basic IF-statement looks like this:

if condition then
    some code
end if

Also, if you have some other code you wish to run, you can issue the ELSE command. As an example,

print "What is x?"
input x
if x = 1 then
    print "x is 1!"
else
    print "x is not 1!"
end if

This is a rather stupid example, because you can always just print the variable. But it's gets the point across, and that's all that matters.

One other thing about if statements that I forgot to mention in the video: suppose you have more than one condition under consideration. If you only care if one or the other condition is true, then you can use the OR Boolean Operator:

if condition1 or condition2 then ...

However, if you want both conditions to be true to run the code, you can use the AND Boolean Operator:

if condition1 and condition2 then ...

I would also like to take the time to show you how to use the GOTO statement, and issue a word of caution regarding this command. The GOTO statement allows you to jump around to different parts of your program called branches. A branch is defined by a branch label, which goes inside square brackets []. This is a very useful thing in programming if you wish to run certain parts of the code more than once.

But watch out! Don't fall snare to the trap of Spaghetti Programming, a problem where beginning programmers overuse the GOTO statement and wrap their codes and their minds in humongous knots and get exceedingly confused. This doesn't tend to be a problem with [branchLabels] so much, but in older versions of BASIC, every line was numbered and you could GOTO any point in your program. Things got a little hairy. Well, there is a way to combat Spaghetti Programming, and it is called for- and while-loops. But I'll save that for a later tutorial.

Anyway, here is an example program that uses everything we have learned so far:

[Start]
    print "Guess a number from 1 to 100!"
    print "You have 7 guesses."
    x = int(100*rnd(1)) + 1
    guesses = 7
[Loop]
    print "Guess number "; 8 - guesses; ":"
    input guess
    if guess = x then goto [youWin]
    if guess < x then print "Too low."
    if guess > x then print "Too high."
    guesses = guesses - 1
    if guesses = 0 then goto [youLose]
    goto [Loop]

[youWin]
    print "You win!"
    goto [playAgain]
[youLose]
    print "You lose!"
    goto [playAgain]
[playAgain]
    print "Play again? (y or n)"
    input answer$
    if answer$ = "y" then goto [Start]
    end

This program is a game that has the user try to guess a randomly generated number from one to one hundred. They win if they can guess it within seven tries, and they lose if they use up all seven guesses. The computer will notify them whether their guess is too high or too low to help them zero in on the correct guess.

A couple of notes about this code:
  1. x is assigned randomly using a few built in functions. You can find a list of all the built in functions in the help files. The rnd(1) function generates a random decimal from 0 to 1. It is then multiplied by 100 because in the game, x can go all the way up to 100. To make it an integer instead of a decimal, we use the int() function to turn it into an integer. Since the int() function rounds down, I added 1 to x so that its range is 1 to 100. You just have to think about it and scratch your head for awhile, and then you'll figure it out.
  2. If you only have one line of code to run if a condition is true, you can have a one-line if-statement. Simply put the code to run after the word THEN.

No comments:

Post a Comment