Programming
Concepts & Tips
1001101110100110
Microprocessors such as the one in the BS2 function
with series of bits. Bits can have 2 values: 0 or 1, true or false,
open or closed, etc. Fortunately we do not need to program with
bits anymore. Over the years people have invented several languages
that can be translated to binary strings. These languages share
common characteristics. Below are the most important programming
concepts.
Variables
Variables are containers for information. You
can define a variable by giving it a name and a maximum
size. Once a variable is defined, you can initialize
it. That is to put information in it. In general the information
is a number.
For example, you could define a variable called AccountNumber
and say it can hold values less than 1 000. Then
you could say that AccountNumber contains the value 345.
This way everytime you refer to AccountNumber, you refer
in fact to the number 345.
Assignment
At any time you can put a new value in a variable.
This is called an assignment operation.
For example, using the previous variabe AccountNumber,
at any time in your program you could assign it a new value. You
could say "now put 755 inside AccountNumber".
This would erase the prevoius value of 345 and AccountNumber
would now correspond to the number 755.
Arithmetics
You can perform simple arithmetic operation on
numbers and variables containing numbers. You can perform additions,
substractions, multiplications, divisions, modulo, absolute value,
etc.
For example, you could create the variable MyMoney that
can hold a maximum of 10 000. Then say that MyMoney
currently holds 5 000. You could then add numbers to
MyMoney by using the addition and the assignment operators.
You would say something like "take the value of MyMoney
add it 600 and store the result back in MyMoney".
MyMoney would then correspond to the number 5 600.
Comparisons
You can compare variables with each other.
For example, you could ask if MyMoney is greater than
AccountNumber.
Loops
You can ask the program to perform a set of operations
for a specific ammount of times. It is a very useful
feature of most programming languages. This way you do not have
to type the same commands over and over.
For example you could tell the program to flash a light on and
off 50 times.
Control statements
This a powerful tool in programming.
Control statements are of the form of " if this do that".
For example, you in a banking system, you could want to add a
money bonus to your 10 first clients (account number from 001
to 010). You would do something like: if AccountNumber
is smaller than 10 then add 25 to the money
in that bank account.
Subroutines
A subroutine is a set of operations that has
a name. You can refer to this name at anytime in your program.
When you refer to a subroutine, the program jumps to this set
of operations, performs them and goes back to the previous statement
when it has finished.
For example, you could define a subroutine called FlashLights.
You could insert statements in FlashLights such as: turn
the lights on for 1 second, then turn the lights off. This way
anywhere in your program, you could simply say "FlashLights".
That would cause the program to perform the operations in the
subroutine FlashLights and then go back to what it was
doing before.
Comments
A comment is a string of information that will
not be read by the microprocessor. In general, it is used to explain
what a specific line of code means. You should use them as much
as possible. This way when you read a program you wrote 2 months
ago, you remember what each line of code was for.
|