# fishhf.com

## Basic Program Structures

Revision and introduction

A program gets executed from top to bottom as previously mentioned.
And we mentioned that there are instructions that read and write to the RAM or let's call it memory.

In this lesson, we'll talk about some basic structures in a program, using a pseudo language. We'll use a real programming language in the future, but we are focussing on the basic concepts first.

Variables

If you remember, in my previous lesson, there was an programming example:

```a=1
a=a+1
if a=2 then
ShowMessageBox "a is now 2!!"```

There's a name for things that store stuff, in the program world, called "variables". So, "a" is a variable.

Variables in a program behaves just like in mathematics, it can store things like numbers and it stores them in the RAM. But it can store other kinds of data too. We'll go through them in the latter lessons.

Example

Now let us do something fun with variables, let us calculate what 1+2+3+4+5+6+7+8+9+10 is by writing a program.

```answer=1

Now "answer" is our variable, and we calculate it by adding to it with the numbers in "1+2+3+4+5+6+7+8+9+10". When we run this program, it will tell us the answer is 55.

You may ask, the program looks dumb, because all the lines except the last one looks repeating.
There's a solution to it.

For loops

For loops is a structure that tells the computer that some instructions can be executed repeatedly, but some variables will change on each execution.

Basically, we want "answer=answer+" to be executed repeatedly, but we want the number at the back to be added to change from 2 to 10 on each execution.
So we can write the same program shorter, but still does the same thing.

Let's call that changing number "magic" and rewrite our program:

```answer=1
for magic=2 to 10

Now you can see that our program is much shorter by having a "for loop" and by introducing a new variable called "magic". It will be changed from 2 to 10 while executing the 3rd line ("answer=answer+magic").

If then else

You have actually already seen the if then structure in lesson 1.

```if a=2 then
ShowMessageBox "a is now 2!!"```

Basically it tells the computer to only execute some instructions, when it can meet some criterias. Therefore, in the example above, the message box will be shown only when "a" equals to 2.

Because it is too simple for you, let me introduce another simple idea to it - "else.

```if a=2 then
ShowMessageBox "a is now 2!!"
else
ShowMessageBox "a is not 2!!"```

Since it is so simple, you should have guessed it right. The second message box will be shown only when "a" is not 2.

So if we have the program:

```a=1234
if a=2 then
ShowMessageBox "a is now 2!!"
else
ShowMessageBox "a is not 2!!"```

We will get the "a is not 2!!" message box, instead of the previous one!

Conclusion

We have gone through 3 concepts in this lesson.

```    Variables
For loops
If then else```

I hope my explainations are easy to understand, email me if you have any comments. Stay tuned!

Written by fish