Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Learn to program with BASIC

Programming is fun, but it can also be very complex. If you’ve ever even looked at C++ code, you can testify to that.If you even know what C++ is, you
can probably testify to that.

But in today’s online-only column feature, I am going to teach you the basics of programming. This is the very foundation upon which most of today’s
programmers, including me, got their start.

The first thing you should know is that there are many different programming “languages” just as there are many different human languages – C, Java,
and Perl, just to name a few. These langauges are a set of commands, phrases, and syntax that define how a program works.

I will be teaching you the basics of the language called BASIC, the Beginner’s All-Purpose Symbolic Instruction Code.

Two other words you’ll want to know before we get started: compiler and interpreter. A compiler takes your code and turns it into an executable file that
any user can double-click on and run your program. An interpreter simply runs your code. We will use an interpreter because those are readily avaliable
for free: most compilers are commercial.

Let’s jump right into it. I will be assuming you are using Microsoft Windows.

We will be using the Yabasic interpreter, avaliable for free from yabasic.de. Click on download, select Windows Binary, and download the setup
program. Once downloaded, execute the setup program and install Yabasic.
When completed, go to Start > All Programs > Yabasic and click on the Yabasic building blocks icon. You’ll see the MS-DOS style window shown below.
This is Yabasic.

Yabasic in action.

This is where you’ll type in your code: at the flashing icon shown below the text. Once you’ve typed in your code, you will press Return (or Enter) twice
and your code will run. Then Yabasic will close.

As noted in the window, using just the interpreter, you can’t save your code. To save your code, you’ll need to use an external text editor. I will go over
this later, once we learn some more.

One of the many little-known quirks of programers is the fact that almost all of them starting by writing the same program, no matter what language
they learned. Keeping that tradition alive, I present you with “Hello World”, your first BASIC program.

In the Yabasic window, type in what is shown in the graphic below. After typing in both lines, don’t press return just yet.

Now, just what does this mean?


// This is my first program.

Believe it or not, this technique is one of the most useful programming tricks you’ll ever learn. Commenting is something you’ll want to do heavily once
you start writing longer programs. These comments, or everything that follows the “//”, are ignored by the interpreter. They are simply for your benefit
so that when you come back to your code you can remember, for example, exactly why you used the code you did.

print “Hello World!”

This is the beef of our “Hello World” program. The print command tells the computer that “everything after this is stuff I want to appear on the screen
for my user to see, minus the quotes.” The computer will gladly oblige, and you will see the words “Hello World!” on the screen (no quotes, of course).
Remember, the “//” line is simply a comment – the computer ignores it.
Now press return twice. This is what you will see.

Congratulations on your first program!

It worked just like we expected. Now press Return again and Yabasic will close.

You can see why we need to use an external editor now, can’t you? To get anything done programming wise, you’ll need to be able to save your work.

This is where the fun begins. I am going to jump in and teach you how to make a simple game that will teach you much more about programming with
BASIC.

Open up Notepad by going to Start > All Programs > Accessories and clicking on Notepad.

The below code may look daunting, but I will explain it step by step in a moment. Type it into Notepad.

You’ll need to click on this image and type the code in from the image.

Once you are done typing it into Notepad, go to File > Save. Navigate to your desktop and then enter “hilo.yab” (even enter the quotes) and press enter.
Before we execute our program, I’ll break it down into blocks to explain what each line means.

// The Hi-Lo Game, by Matt


2
// Start of each round of the game
clear screen
print “I am thinking of a number between one and ten.”
print “Take a guess and I will tell you if it is higher or lower.”

This part is mostly stuff we’ve done before, but there are a few new things. The first line is just a simple comment proclaiming that it was in fact you
who wrote this program. Remember, the computer will ignore anything that starts with “//”.

Next, the simple number “2″. In Yabasic, this is a simple way to denote a label. Other forms of BASIC use different methods. It says 2 because it is the
second line. By doing this, if later in the program I want to redirect the action back to this point, I can simply use the “goto” command to “go to” the 2
label, or the second line. We’ll look at this later.

The “clear screen” command does just that – it clears the screen of any text. This way, it will just be your program on the screen and nothing else. After
that, we use the “print” command which we mastered earlier to explain our game to the player.

// Ask the user what he thinks the answer is with input and put in variable a.
input a
Aside from the “print” command, the “input” command is the second most useful you’ll ever learn. This asks the user to enter something that you’ve
asked for. In this case, we asked the user to guess a number between one and ten. Once the user enters something and presses enter, we will store what
he told us in the letter a. This is called a variable, just like the variables you studied in Algebra class.
This is what the user see’s when the input command is used: a simple question mark. But we explained above what we wanted them to enter, so they
should know what to do.

Now we will check and see if the user was right or not.

// See if what the user said is right.


if a>5 then
print “Sorry, the number is lower. Press enter to try again.”
end if
if a(less than symbol)5 then
print “Sorry, the number is higher. Press enter to try again.”
end if
if a=5 then print “You got it dude!”
end
end if
// Press enter to continue
input b
//Return to start of the round.
goto 2

end if means that we only want to do what is between then and end if if the ifcondition is true. Next, if a is less than 5 then we tell the user to guess
lower. And finally, if the user guessed 5, we tell him he is right and then use the endcommand to stop the program in its tracks.
Notice that the end command wasn’t used in the first two conditions (if a>5 and if a(less than symbol)5). That means that at this point, if the user
guessed higher or lower than 5, we need to turn him around and let him guess again. The input b line is used, but anything entered into the b variable is
useless to us. We just needed a break so that the user could read the screen before we clear it and start the game over.

After enter is pressed, the line goto 2 takes the game back to label 2. The screen clears, and the user has another chance to guess the number with some
inside knowledge as to whether he was too high or too low last time.
Now, go to your desktop and double-click the hilo.yab file. Yabasic will run your program. Go ahead, try and guess the number!

Of course, the number is 5 every time you run it. It is possible to make the number random, but that is beyond the scope of this basic introduction to
BASIC.

Interested in learning more about BASIC and Yabasic? Check out these links.

Introduction to Yabasic – A great tutorial for Yabasic.


Yabasic Tutorial – Another tutorial that doesn’t go as far into programming as the one above does.

I hope that this column was useful to you!

Filed in these categories: Cool Downloads, Online Columns,Technology

Ads by Google

3 Comments
Filed under Cool Downloads, Online Columns,Technology

Like
Be the first to like this post.

3 Responses to Learn to program with BASIC

1. allan
March 28, 2008 at 2:02 pm

Hi,
im intereseted in learning programming and have already dowloaded yabasic and done the above two tutorials, where can i go to learn further , i havent
been able to get some better results on google, basically its begun with me needing to make my own network monitoring system.
any help would be appreciated.

Reply

2. Alice
August 28, 2009 at 9:14 am

hey did u know that u taught me more than my i.s.t. teacher has ever taught me. she sucks! i had to teach her how to put a hyperlink in powerpoint.
yea.. i just wanted to thank you coz i was always curious about programming and it turns out its heaps fun! ^^ but where can i learn more?

Reply

3. Valdred
October 6, 2009 at 6:23 pm

when i try to open the setup it says that the file is broken, is there any other programs i can use?

Reply

Leave a Reply
Top of Form
Your email address will not be published. Required fields are marked *

Name *

Email *

Website
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite>
<code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Comment

Notify me of follow-up comments via email.

Notify me of site updates

Bottom of Form

You might also like