How to Create Your Own Computer Programs the Easy Way


FlagPost a comment
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 column, 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 languages 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 available for free: most compilers, such as Microsoft's Visual Basic, 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 a MS-DOS style window - this is Yabasic.

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 programmers 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 below. After typing in both lines, don't press return just yet.

// This is my first program.
Print "Hello, World!"

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. 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 or another text editor.

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

// The Hi-Lo Game, by [[Insert Your Name Here!]]
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."
// Ask the user what he thinks the answer is with input and put in variable a.
input a
// 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 aprint "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

Once you are done typing it into Notepad, save your code to the desktop. Call it "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 sees 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 aprint "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 if condition is true. In this code, if a is greater than 5 or less than 5 then we tell the user to guess lower or higher. If the user guesses 5, we tell him he is right and then use the end command 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 aAfter 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.

Filled Under:

0 comments:

Post a Comment