BWD Chapter 03: Learning a foreign language

2004-10-27 20:45 - Beginners Web Development

Chapter 03
Learning a foreign language

When programming a computer we say we are using a programming language. Of course, because that is just what it is! A programming language is much like a spoken language. A spoken language has syntax rules about where the verbs and nouns go, and a programming language has rules about where the operators and arguments go. a spoken language has pre defined words whick you are allowed to use, often laid out in a dictionary. A programming language has specific keywords which are defined as having special meaning, often laid out in the manual.

If you already speak a foreign language, you may be in luck. Many of the concepts and processes of learning a foreign language are just like those used in learning a programming language. The biggest diffrence is that computer languages are always much more strict. A computer usually can't figure out what you mean unless you say just the right thing. even one letter being wrong can totally ruin a program, while a human hearing a language spoken can usually fill in the gaps for themselves.

* * *

You may be surprised to hear me say it, but computer programming only takes two steps. They can be difficult steps, but if you keep them in mind it makes doing them that much easier. They are quite simple reeally:

  1. Divide the problem into smaller pieces.
  2. Apply fixed knowledge to solve problems.

It's that easy! The trick is doing both "at the same time." Or, burning the candle at both ends. You must know what knowledge to apply in order to break down the original problem into the correct smaller problems. You also must know how to break a big problem down to get it to a point that you can solve the small problems with one of the small pieces of knowledge that you have in your arsenal.

In computers, everything eventually boils down to a one or a zero. getting there is the challenge. Remember that computers are stupid, and to tell them how to solve even simple problems, we have to do the work of getting the problems to a truly trivial level.

You probably already know how to break problems down, without realizing it. It will help you to think about even small problems on a smaller scale in your day to day life. When you have to get dressed in the morning, first you choose your pants, then your shirt, and socks and shoes. Even if you are used to it and do it quickly and easily, the big problem is made of these smaller problems. Once you have chosen your clothes, you put each article on, one piece at a time.

Even putting on a sock is a complicated process. First, take the single sock in your hand. Line it up properly for your foot. Put your foot into the sock, then sretch the sock out evenly.

We must always approach computer programming with the tactic, "Divide and conquer." A few minutes of planning ahead, attacking the problem from the right angle, can save hours of effort and aggrivation down the line.

Still, once the big problem has been split into little easy problems, the little problems must be solved. This is where learning the language really comes into play. For most computer languages, dividng the problem is the same. Knowing another language is simply knowing how to divide the problem to match the tools available in that language. This is a big lesson, and stresses the importance of learning to think the right way. Thinking the problem through is the hard, and important step. Actually writing down the steps once you figure them out is the easy part.

* * *

In this series, we will start with a cute language called BASIC. BASIC was one of the early high level languages. It is called high level because the tools it provides are capable of solving bigger problems than those available in low level languages. The B in BASIC also happens to stand for beginners. A perfect place to begin!

It is a long computer programming tradition to begin a new programming language with a particular program, called Hello World. Hello World is a simple program, it simply displays the phrase, "Hello World" to the screen, to announce to the world that here I am, and I am a computer programmer!

When we are done with Hello World, the program will be a whopping one line long. Ther are still a good few things to learn even to make this first one liner.

The first lesson is that of language. Like we said, computer languages have syntax just as spoken languages do. Computer language syntax is critical though! There are often slight leniancies, but otherwise syntax must be followed exactly or dire concequences will result.

To begin, the basic programming language unit is a statement. This is most often a command, to identify a particular action, then often some extra information to specify how this action should be carried out. To solve th Hello World problem, we must split the problem in two. First, we must display something on the screen, then we must control which thing it is we display.

For this simple program, in BASIC, we have split it as far as we need to. First, to print something to the screen, we have a command designed just for that, it is called print. The print command, though, is useless on its own. Without saying what to print, we cannot accomplish much.

So what do we want to display? A specific phrase. To the computer, this phrase is just data, a series of ones and zeros. A data type lets the computer know how to interperet these bits into something meaningful. There are many data types, and each language has a different set of data types that it can handle. For now we will concetrate just on two. Numbers and strings.

Numbers are easy. We all know what numbers are. It is also rather easy to tell a computer what a number is, just type it in. Things get a bit more complicated when we want to do something with the number, but we'll learn more about numbers later. For our current purposes, we need a string. A string is a number of characters "strung" together. A string can hold letters, numbers, spaces, commas, and much more. This is what makes strings special, they hold most anything. So, how do we know where it starts and where it ends, if it can hold anything? Strings are deliniated by quote marks. In BASIC, we have to use double quotes, like these: "".

So, we have what we need now to write our first program. Let's try it! First we need to have a copy of the programming language to work with. You can get one from the following places: QBasic 1.1, QuickBasic 4.5, or many other places, Google it. If the one you choose has .bas files included you may ignore them for now, they are BASIC source code files and probably too advanced to dive into now. Once you download and extract the file, you should have a program called qbasic.exe or qb.exe. Run it and you should get a window looking something like this (You'll probably have to press ESC to get past the loading screen.):

Type this in, exactly as it is shown:

print "Hello world!"

Now, run it! Can you figure it out yourself? You can get to it from the Run menu. But no mouse! Press the Alt key to activate the menu. Use the arrows to get to Run and press enter or just press R. Then you see the run options. We see "start" which is what we want. Since we haven't started yet, "continue" will start also. The hotkey for continue is F5, so normally we can just press F5 to run our program. If everything went right, you just wrote and ran your first program!

Comments:

No comments!

Post a comment:

Username
Password
  If you do not have an account to log in to yet, register your own account. You will not enter any personal info and need not supply an email address.
Subject:
Comment:

You may use Markdown syntax in the comment, but no HTML. Hints:

If you are attempting to contact me, ask me a question, etc, please send me a message through the contact form rather than posting a comment here. Thank you. (If you post a comment anyway when it should be a message to me, I'll probably just delete your comment. I don't like clutter.)