Your First Program
From codeTank
To create a program, you need three things.
- A goal, which determines...
- A language, which is executed by...
- A program
Since this is your first program, the goal is pretty easy. The goal is simply: to learn how to program, starting with the basics.
Based on that goal, I have taken the liberty of choosing a language for you, since I know a lot about programming already. I chose the language Lua because it's easy to learn, makes sense, and is easy to modify and extend.
I have also taken the liberty of choosing a program for you to execute your Lua code in. It's called Brain Damage. Actually, I created Brain Damage for that very purpose. To get Brain Damage, go to the Brain Damage page, and download the latest version: Brain Damage.
So it turns out we have everything we need to start writing code!
- A goal: learn how to program, which determines...
- A language: Lua, which is executed by...
- A program: Brain Damage.
Contents |
Tour of Brain Damage
Download and run Brain Damage. You should see something like this:
Let's do a basic tour:
That's about it! Some other notes of interest:
- The numbers on the left side of the screen indicate what number each line is. This is useful for errors - for example, if the Output screen tells you you have an error on line 20, then you look at where line 20 is and try to figure out what your error is.
- You don't have to hit the button on the toolbar to run your program - you can use the short-cut Shift+F5. If you lose control of your program, you can always force it to stop by using the short-cut Shift+F7.
- Treat Brain Damage like Notepad on steroids. It colors what you type to make it easier to read - but when you save your file, it saves it just like a normal text file. The coloring is done by Brain Damage - it is not saved to the file itself. This is called syntax highlighting, and is very helpful.
What do I type?
For your first program, type the following code in the main window. Resist the urge to copy/paste, and actually type it out:
print("You're in the demon produce isle!")
Hit Shift+F5 to execute the program. You should get something like this:
Congrats!
How did it work?
Hit Shift+F5 again. And again. Every time you run your program, it will output the same thing to the Output window.
You can add multiple lines to your program. Try:
print("Quake is an online virtual reality used by hackers") print("Lunix is an illegal hacker operation system") print("Over-exposure to computer radiation can cause schizophrenia")
What is going on? When the program starts, it executes the command on line 1. Then moves to the next line, and executes that command. It keeps going to the next line, executing one command at a time, until it reaches the end.
The command you are invoking is a function called print. The function print will take whatever you give it, and send it to the Output window.
Easy? Easy!
You might wonder what the parentheses ( ) and quotes " " are for. The parentheses tell Lua that you are executing a function. And the quotes are used to start and end a string.
Basically, when Lua reads the line, it goes from left to right... it reads print, and thinks to itself, "Ok, what am I doing with print?" Then it reads the parenthesis ( and thinks, "Ah, I am calling a function called print". Then it sees the quote, and says, "Ok, there is going to be some text", and does that until it hits the end quote. Then it hits the end parenthesis ) and executes the print function, with the text as a parameter.
Keep it Simple
This might be a little confusing at first. Don't let the strange terminology confuse you... just look at the code, and keep it simple at first. Play around with the print function until you feel comfortable with how it works. Here are some things to try:
print ( "hi" ) print( "yo") print(" yo") print("Whats", "up") print ( "hi mom" ) print("am", "I", "just", "wasting", "my", "time")




