Programming for Absolute Beginners

Programming is basically telling you're computer what to do. In that sense, you are programming your computer every time you use it!

But when most people talk about computer programming, they are referring to writing code in a computer language. The language discussed on this site is called QBasic.

Computers can only do math. Everything we do on our computers is ultimately converted to binary code which is completely made up of 1's and 0's. (like : 101001101)

There are two main ways to store values in Qbasic, constants and variables.

Variables : Values which can change. If you've done algebra you've probably dealt with variables already. The most common variable is 'x'. Unlike Algebra, variables can be MORE THAN ONE LETTER (I could have a variable called 'thisIsAReallyUnneccessarilyLongVariableName').

A variable is like a locker. It holds information. But in programming, variables can only hold one value at a time (That means ONE number or string, we'll talk about strings later) .

For example, I can say : x = 1. Now, whenever I refer to x, it will be 1 (If I said y = 2 + x, then y would be 3). If I want to change x to 2, then i would write x = 2.

However, what if I don't know the value of x, but want to add 1 to it? I would just write x = x + 1. Some people find this confusing so I'll walk you through it.

When you say x = x + 1, this is what the computer does :

     1. Let me take the value in x. (Because x is like a locker, which stores a value)

     2. Now let me add 1 to x (that is the  x+1 part)

     3. Finally, let me put 'x+1' back into x.

So, if x was 4, now it is 5. If it was 21, now it is 22.

Types of Variables
For now, you only need to worry about two types of variables.

     1. Numerical variables : These store numbers. e.g : x = 1, y = 1, z = 86 etc.

     2. String variables : String variables hold chains of letters and/or numbers.
        These are mostly used for names. For a string variable, put a '$' sign at
         the end of the variable. e.g : x$ = "This is Cool 101". A string variable
         CANNOT BE USED FOR MATH. If I said y$ = "2" and z$="3" and then                            
         said a$ = y$ + z$, then a$ would be "23" NOT "5". When you add two  
         string variables, QBASIC concatenates them. This means it joins them together.


Constants : These are numbers which do NOT change. Examples of constants : 1,2,3 etc.

You CANNOT say 2 = 2 + 1 (because then the universe would explode!).

Let me give you an example of where we might use constants.

If I wrote a program to calculate the area of a circle (the formula is A = pi * r *r), I would need a constant for Pi (because Pi never changes!!).

However, I don't want to write 3.1415926535897932 every single time I use Pi.

So, I would just declare a constant, like this : CONST Pi = 3.1415926535897932

Now, whenever I say Pi, the computer will know I mean 3.14159..etc. REMEMBER, I CANNOT SAY Pi = Pi + 1!! (because we can't change the value of a constant)

We're all done here! Head over to the tutorials section to get started!






2 comments:

  1. thank you after long time i really found the use of constant in qbasic

    ReplyDelete
  2. thank you after long time i really found the use of constant in qbasic

    ReplyDelete