Sunteți pe pagina 1din 8

Getting Started

We will begin by simply saying hello. This is one of the usual begin programs that any new
student will write when they come to a new language. Type in the following code:
Print "Hello"
Here is a screen shot of the result:
The PR!T statement is used to display information on the screen.
nteraction with the user
Well we can display things to the user" how should we get information from the user# This form
of interaction is gathering input" and we use the !P$T statement to accomplish it. The !P$T
statement gets data from the user" but what does it do with the data# %ariables are used to store
data that can change during the program e&ecution ' they are central to most programming
languages. The !P$T statement uses a (ariable as its target to hold the information the user
supplies. Here is an e&ample:
nput )
*efore we integrate !P$T into our program lets ta+e a closer loo+ at (ariable types. %ariables
are created in ,iberty *asic when we assign a (alue to them. The traditional command for
assigning a (alue to a (ariable is ,-T. The command ,-T is understood in an implicit
assignement and the command is not re.uired. /ost programmers simply write the (ariable
name" an e.uals sign and then the (alue to be assigned. Here is an e&ample:
0ount 1 23
Which is the same as:
,et 0ount 1 23
,iberty *asic has two basic (ariable types. !umeric (ariables and String (ariables. !umeric
(ariables hold numbers only. They are useful because we can perform numeric operations on
them 4li+e adding" subtracting" s.uare root and so on5. They can hold any type of number
including whole numbers and fractional numbers. Here are some e&amples of numeric (ariable
types:
0ount 1 23
items 1 67
total 1 26.23
a 1 88
9 1 .::::2
pi 1 ;.27
String (ariables typically hold words. They are howe(er not restricted and can hold any form of
data" letters" numbers and special characters. Strings are (ery (ersatile" howe(er they can not be
used to perform math functions. ) string (ariable is differentiated by the <=< that is appended to
the (ariable name. )dditionally when assigning a (alue to a string the (alue must be enclosed in
.uotation mar+s. Here are some e&amples of strings:
!ame= 1 "*rad"
System= 1 "Windows >P"
Today= 1 "/onday"
Players= 1 "26"
n the e&ample abo(e Players= is a string (alue that holds a number. This is o+" but math
functions can not be performed on the string. There are functions that allow con(ersion of strings
to numeric (ariable types and from numeric (ariable types bac+ to strings. We will discuss those
in a latter installment.
) final word of caution about (ariables: Spelling counts" as does the case used in the (ariable.
?or instance the (ariable "close" is not the same as the (ariable "0lose" or "clos" or "c,oSe".
These are different (ariables.
Getting some answers
So with that foundation ' lets get some information form the user. Specifically the users name.
Here is what the new program will loo+ li+e:
Print "-nter your name"@
nput !ame=
Print "Hello "@!ame=
,ets play a game
,ets e&tend the lesson into a simple game. The game if HighA,ow. The user will try to guess the
number the computer has thought of and the computer will tell the user whether thier guess was
too high" too low or right on.
This pro9ect is going to re.uire a couple new commands we ha(e not yet co(ered. The first is a
control structure. t allows us to ma+e decisions based on the contents of a (ariable. The
command is ?...TH-!. We will be using the bloc+ form of the ?...TH-! which is constructed
as follows:
f e&pression Then
statments...
...
...
-nd f
n this form the statements between the ?...TH-! and the -!B ? are only e&ecuted if the
condition of the e&pression is true. What does that mean. Well lets ta+e an e&ample:
f ) 1 3 Then
Print "The answer was fi(e"
-nd f
Then the print statement 4Print "The answer was fi(e"5 could only be e&ecuted if the (alue )
really was 3.
The other thing we will need to understand is looping. ,ooping is where we e&ecute se(eral
statements of code then we encounter a special statement that redirects the program bac+ up to
e&ecute that same code again. There are se(eral statements and programming structures that can
be used to cause looping" but we will be using the GCTC statement because it is easy to
understand. GCTC simply redirects the program from its current location to another labeled
section of code.
What does it mean to ha(e a label. n ,iberty *asic labels are used to organiDe the code" allow a
place to branch or goto during program e&ecution and pro(ide na(igation within the pro9ect
during design time. We will co(er some of these uses later. Right now we are interested in labels
to aid in program branching. ) label is any word that is contained in brac+ets 4ie. "E" and "F"5.
Here are some e&amples of labels:
EloopF
EmainF
EbeginGhereF
,ooping is done by instructing the program to GCTC a specific labeled area of code. Here is an
e&ample:
EloopF
<some code here
...
...
Goto EloopF
/ost looping is conditional" which is to say that it only e&ecutes a GCTC statement if a certain
condition is true. The condition is e(aluated in an ?...TH-! statement. f the e&pression in the
?..TH-! statement is true then the GCTC statement is e&ecuted and the program e&ecution is
redirected to the labeled area of code. ?or instance:
EloopF
<some code here
...
...
f ) 1 3 then
Goto EloopF
-nd ?
n the e&ample abo(e the program will only loop if ) really is 3. Ctherwise the program will fall
through to the end of the code and terminate. Here is a graphic that demonstrates the program
flow in a basic looping program that uses conditional looping.
Be(eloping the game
So getting on with the game... ,ets go ahead and introduce the game and then choose our
number. We will le(erage the hello code we wrote earlier:
Print "-nter your name"@
nput !ame=
Print "Hello "@!ame=@" welcome to HighA,ow"
!umber 1 3
,ets add to this some breif instructions so the use +nows what is e&pected.
Print ")ll right "@!ame=@" choose a number between 2 and 2:"
Cne of the things you may ha(e noticed is that ha(e embedded a string (ariable into the print
statement. t is outside of the .uotations and preceded and followed by a semicolon. The
semicolons force the elements of the print statement to follow each other on the same line and
not to be on separate lines. The also are re.uired with in the synta& of the statement where we are
mi&ing literals 4the part contained between .uotes5 and (ariables in the same print statement.
We ne&t need a label that we can branch bac+ up to ' because the guessing part of the game
needs to be in a repeating loop until the correct answer is located.
EloopF
!ow lets find out what the users choice is. We will couple a PR!T statement with an !P$T
statement. The !P$T statement always prints a .uestion mar+ 4"#"5 on the screen when it is
waiting for input. li+e to force the .uestion mar+ to print at the end of my PR!T statement. Bo
this by placing a semicolon at the (ery end of the PR!T statement:
Print "What is your choice"@
nput a
!ow using the ?...TH-! statement we need to e(aluate whether the answer is higher" lower or
right on compared to our chosen number.
f a H !umber then
print "Too High"
goto EloopF
-nd if
f a I !umber then
print "Too ,ow"
goto EloopF
-nd if
f a 1 !umber then
print ")ll rightJ Kou got itJ"
-nd if
There it is. The game of HighA,ow. f you ha(e followed along" typing the program in then go
ahead and run it.
)dding Randomness
The game could use a couple embellishments. ?or instance ' it would be nice if the programmer
4who is also the tester5 did not +now what the answer would be. Ha(ing fi(e as the magic
number each time +ind ta+es some of the charm out of the game. What we need is a way to
randomly select a number between 2 and 2:.
,iberty *asic does ha(e a random function called R!B. t returns a number between : and 2 4a
fractional number5 which can then be scaled to fit our needs. Here is how we do that:
! 1 R!B4:5
,ets say for instance that the function abo(e returned the (alue .;28L. !ow we need a number
that is greater than" or e.ual to 2 and less than" or e.ual to 2:. We can get this by mo(ing the
decimal one place to the right of our current number. This is accomplished by multiplying it by
2:. So we would write the following:
! 1 R!B4:5 M 2:
That will gi(e us the (alue of ;.28L. *ut we really wanted a whole number. The !T function
will truncate the decimal portion of the number. We would write this as:
! 1 !T4R!B4:5 M 2:5
Which is great. t will gi(e us the number ; in the e&ample abo(e" but what if R!B4:5 had
actually produced the (alue .:673# Then the result of our e.uation would be :" which is less than
2. We can sol(e this by adding a one to the final e.uation. So the following e.uation would
return a (alue that is between 2 and 2: inclusi(e:
! 1 !T4R!B4:5 M 2:5 N 2
So change the part of the part of the program where we set the (alue the computer has selected.
Right now it is a line that reads li+e this:
!umber 1 3
0hange it to read:
!umber 1 !T4R!B4:5 M 2:5 N 2
!ow run the program. ) little bit more interesting ' huh# Well" not a lot ' but this is simple
games stuff ' we will ha(e to be patient before we can write the ne&t ma9or bloc+buster game.
There is much more to learn.
/a+ing the game repeat
)s you ha(e no doubt noticed ' the game ends as soon as you get the answer correct. f you want
to play again you must close the output window and then run the program again. Wouldn<t be
great if it as+ed you if you wanted to play again# f you chose to play again you would get a new
magic number and try to guess what it is" of not then the program would e&it.
This is not hard to do. t simply re.uires another label and another conditional loop using the
GCTC statement.
Put the label 9ust before the line where we choose the magic number. ha(e inserted into the
portion of the code as show below:
Print "-nter your name"@
nput !ame=
Print "Hello "@!ame=@" welcome to HighA,ow"
EbeginF
!umber 1 !T4R!B4:5 M 2:5 N 2
!ow we need an ?OTH-! statement to handle the conditional looping. Place this code at the
(ery end of your program. We will be as+ing a .uestion again" so we will use an !P$T
statement. This time the target of the !P$T statement will be a string (alue. Here is what to add
to your code:
Print ""
Print "Bo you wish to play again ' 4y 1 yes5"@
nput answer=
f answer= 1 "y" then
0ls
Goto EbeginF
-nd if
ha(e introduced a couple new items here. ?irst is the idea of printing blan+ lines. The statement
4Print ""5 places a line of nothing on the output window creating a blan+ line to help separate and
brea+ up the te&t being printed to the output window. The second new thing is a command 0,S.
This mean 0,ear Screen ' it only wor+s in the default /ainWin which we ha(e been using for
output. ssuing this command simply remo(es all te&t from the output window.
also thin+ the program would be nice if it than+ed us for playing at the end of the run. f you
thin+ so too" add the following to the (ery end of the program:
Print "Than+s for playing ' Good bye"
!ow try out the program. /uch nicer huh#
?inal thoughts
Well the program runs nice" but there are still a couple more things we can do to spruce it up. The
first is to add some comments to the code. 0omments do nothing durring program e&ecution" but
they ma+e the code easier to understand later should we re(isit the program at another time" and
help us to +now what were thin+ing when we wrote specific sections of code. Traditionally a
comment line began with the command R-/ or R-/)RP. R-/ still wor+s in ,iberty *asic"
but most people today simple use an apostrophe to begin a comment. li+e to use liberal
comments in my code so that will understand what was doing when wrote it. Here is an
e&ample of a comment you might put 9ust before you select your magic number.
<Select a random number between 2 and 2: 4inclusi(e5
)lso want to issue a challenge ' something you can do to try out your new s+ills. /ost games
ha(e some form of scoring mechanism ' wouldn<t it be great to +eep trac+ of how many guesses
it ta+es to get the right number and also what the best record 4fewest guesses5 that you obtained
for a gi(en run# What you need to do this is a counter. ) counter is a numeric (ariable that you
initialiDe to Dero" then count up" each iteration through something. Here is an e&ample:
0ount 1 :
O
<some code here
O
0ount 1 0ount N 2
t is not .uite li+e algebra. That e.uation would ne(er be permitted" but in computer math that is
perfectly acceptable. What it means is to ta+e the current (alue of 0ount and add one to it and
assign the result to 0ount. t simply increments the (ariable.
am not going to tell you how to integrate the solution to the challenge. See if you can do that.
Howe(er the complete game is included in a Dip file called beginners2.Dip which accompanied
this article.
)lso" a couple disclaimers before my fellow *asic Programmers eat me ali(e:
25 /ost people a(oid GCTC statements if they can. Somewhere along the way people decided
that GCTC statements were bad because they fostered poor coding practices. That is partially
true" but if you are going to be a poor coder it really does not matter whether you use GCTC
statements or not. find them to be easy to understand and a good starting place for e&plaining
looping and such topics. ) mature programmer will use different looping constructs in a program
li+e this" such as the WH,-OW-!B statements. We will (isit these at a much later date.
65 The program as written has a lot of places that a bad input will crash the program. t has been
designed to be basic and not ha(e a lot of error chec+ing. -rror chec+ing is important and bomb
proofing concepts ' which we will co(er later ' will impro(e the runability of the program. /any
e&perienced programmers am sure will recogniDe the potential errors that ha(e introduced in
getting a numeric (alue using an input statement. What if the user enters a letter# Try it and find
out.

S-ar putea să vă placă și