Programming

Programs are logical sequences of commands for the computer to perform. Once a program has been written, the computer will do that series of steps as a result of being told only one command--the program's name. This way, it can be thought of as the computer learning how to do something new. For example, as an infant, you knew only how to do very basic things such as lift your arm, move your leg forward, or open your mouth. However, as you grew older, you learned how to do more complex things. But were they really more complex? Think about what you do automatically when you turn a page in a book. It is really just a sequential series of basic steps:

  1. Lift hand.
  2. Move hand to right side of book.
  3. Grasp top corner of page.
  4. Move hand from right to left until page is positioned so that what is on the other side can be read.
  5. Let go of the page

But now, with practice and experience, you turn a page without even thinking about it. It becomes automatic. It is done as one continuous action. This is what programming does. It defines an action as its pieces so that the computer can execute many basic actions as one continuous action as fast as possible. It allows the computer to do all of its commands right after the other without having to wait for a human to type the next command after it finishes each one. Also, if you want the computer to run a program more than once, you just have to write out the small steps once and then execute the program many times. If you were to do this without writing a program, you would have to write out the many steps many times, which could take huge amounts of time. For these reasons, it is essential to know how to program if you want to efficiently use the computer. So, here is how to program:


Since computers don't understand any language like English--only binary (which would be nearly impossible to program in), several programming languages have been developed. Programming languages allow humans to write programs in commands which are closer to English than binary. Once the program has been written, the language then converts into computer commands with a compiler. Most programming languages have both a base version as well as a visual/graphic and/or object oriented version. Some languages, such as Fortran, are more difficult to learn than others because they aren't very much like English. Others, like COBOL, are very much like English and easy to learn and use. However, both of these languages are fairly old and not used very widely anymore. CIRCUIT has decided to only concentrate on three more common, new languages that aren't too hard to learn. These languages are Basic, Pascal, and C.

Before CIRCUIT begins explaining these three languages, it would like to first explain another type of programming from compiling programs. With most operating systems, things are done by telling the system to execute commands. Normally, the operating system has a method of writing collections of system commands. This is either accomplished through batch programming (which is just that--a collection of system commands) or by writing macros. Macros are similar to batch programs except that they are less like normal programs. They are a collection of keystrokes and mouse moves/clicks. Therefore, they are more useful in a graphic environment, but can't be edited or viewed in a text editor.


Programming tells the computer to do certain steps in a certain order. At first, when the programs you write are small and simple, it may seem easiest to just sit down and write it. But even with simple programs, it is necessary to first write out your algorithm. An algorithm is the logical sequence of steps that is converted into your programming language. The algorithm should be written out in English and can then be converted into any suitable programming language. However, when writing your algorithm, you should keep the capabilities of the language you plan on using in mind. There are several techniques in programming that are common to most languages. These are good techniques to stick with when writing your algorithm so that your program can be written in any language you want to write it in.

Before explaining these techniques, it is first necessary to learn about variables, their uses, and how important they are. Variables are just what they sound like-- non-constant program components. Variables are labels to a location/address in RAM. This RAM address can store data, and be changed when needed. However, the computer would get confused if you did something like store the character "A" in a variable and then try to multiply it by the number "4". For this reason, data types have been created. The main data types are "character", "integer", "real", and "boolean". A "character" is a one byte symbol from the keyboard. An "integer" is a whole number with no decimal part. A "real" is a number with a decimal part (this can be confusing because while 7 is an integer, 7.0 is a "real" since there is a number after the decimal point-- even if it is 0). A "boolean" value is a simple yes/no value. It can only be "True" or "False". However, it can often have numbers stored in it since the computer considers "0" to be false and everything else to be true.

Although these are the four main data types, they aren't the only ones. Many languages divide the "real" into two separate data types: "long" and "short". The only difference between these two data types is their maximum and minimum values. "Long" has wider limits, but uses up more RAM. Therefore, "short" should be used when possible. Another common data type is the "string" data type. This data type can contain many characters. A word or sentence or even more can be stored in a "string". Two other kinds of data types are the set and the array. A set is of one of the other data types and can contain many values of that data type. Sets are mainly used to store lists of data and can then be asked if some variable is in the set. Arrays are kind of like ordered sets. In an array, you can request such things as the 3rd (example) value in the array. Another function that arrays can be used for is to find out what precedes or what succeeds a value. However, the main use of arrays is their ability to be multidimensional arrays. With multidimensional arrays, you can find out what value corresponds to (again only an example) the 5th in one dimension, and 7th in another.

Since variables are the only way to store information while running a program, and are the only way to keep track of data that changes during execution, they are an essential part of any program that will run for more than one, very specific situation's problem that it is solving. Therefore, it is very important that you understand how data types work. Please click here if you would like to review data types before continuing.

A major part of programming is input and output. The input of the program is what the user tells the computer and the output is what the computer tells the user. It is always necessary to have output in a program so that the user can view the solution to the problem the program was solving and so that the user knows when execution has finished. Input is also important. If the user can't tell the computer anything, the program will only be able to do one thing. After one run, it will have to be rewritten for any other problem it is going to solve. So all languages have a way to store input in variables and to show text, pictures, or other media on the screen, etc. for output.

A technique that all languages have is the conditional statement. The conditional statement is a statement that causes the flow of control in the program to deviate from its normal, logical sequence. The conditional statement is usually in the form of an "If...Then...Else..." statement. This causes the program to be different each time it is run. For example, the program may be set up as: 1) Ask for a name. 2) If the name is Bob then say "Hi Bob." Otherwise say "Hi someone other than Bob." Maybe you'll run the program one time and the results will be "Hi Bob.", but "Hi someone other than Bob." another time. The conditional statement is used to evaluate two or more expressions for equality, inequality, or to test for the larger/smaller of two expressions. As you've probably guessed, the conditional statement is basically evaluating a boolean expression. It is either "yes" or "no". For this reason, there are two acceptable ways to form a conditional statement. The most common is as follows: If [EXPRESSION] [COMPARISON] [EXPRESSION] Then [STATEMENTS] Else [STATEMENTS]. The second is just another way of saying the same thing (the variable "bool" is of the boolean data type): bool = [EXPRESSION] [COMPARISON] [EXPRESSION] :::***NEW STATEMENT***::: If bool Then [STATEMENTS] Else [STATEMENTS].

Another common technique is the loop. There are two main types of loops. One, called the Do Loop, is a loop that executes either while a condition is true or until a condition is true. This is the equivalent of having a list of commands, and then at the end of the list saying "If [CONDITION] Then repeat all that stuff you just did". Each time it passes through the loop, it tests the condition again. "Until" is the same as saying "While Not". The other type of loop, the For Loop, isn't too much different from the Do Loop. The syntax is normally, "For [VARIABLE NAME] = [NUMBER OR VARIABLE] To [NUMBER OR VARIABLE] (optional: Step [NUMBER OR VARIABLE])". The way the computer looks at this command is like this: the first variable name is set equal to the first number/variable. All of the commands in the loop are executed. The variable is increased by the step value (negative numbers are used to count down). If the variable is now equal to the or has bypassed the second number/variable, the flow of control is passed to the next command after the loop. Otherwise, the loop is repeated (without setting the variable's value to the first number/variable anymore). A For Loop is normally used for convenience only. To manually create one, you can say, "Do: [STATEMENTS]; variable = variable +/- ??? While variable = ???".

A final common programming technique for all languages is the execution jump. The command to do this is almost always called goto. Goto causes the flow of control to immediately jump to a label. In some languages, this label is a line number. In others, you have to make a special label that serves no other purpose than to tell the goto where to go to. Gotos are good for shortcuts after If statements, but can be confusing and should be avoided. Procedure calls should be used instead. With these, control returns to where it left instead of just remaining where it jumps to. This keeps the logic in the program much easier to keep track of than with gotos.

These cross-languaged techniques are very useful and should be kept in mind while writing the algorithm for a program. These techniques are the basics of programming. The exact syntax used to execute them in different languages varies, but should normally be the basis for any program you write. Since knowing these techniques without knowing the syntax to use them doesn't help you too much, CIRCUIT will now explain three common programming languages with similar, but not too similar syntax.


Basic

Basic is one of the simplest languages to learn and use. However, it is one of the least powerful and useful as well. It is good for a first language and is good for learning the basics of programming before moving on to more difficult languages.

First off, you need to know about how variables are declared. Variables are declared like this: "dim color as integer". As you can see, there are three parts to the declaration. The first part is the word "dim". This part is what determines the life of the variable. If the word "dim" is used, the variable will remain in memory until the procedure has finished executing. If the word "static" is used in the place of "dim" however, the variable will remain in memory until the entire program has finished executing. These two types of variables are local variables. They can only be used by the procedure in which they are declared and any subprocedures below it. Variables which are to be used by all parts of the program should be declared with the "global" expression in this first part of the declaration.

The next part of the declaration is the variable name. The example given above was "color". The variable name is what other parts of the program will reference it by. The name can be just about anything with these exceptions: 1)The name must start with a letter. 2)The name must have no spaces in it. 3)The name must be only made up of letters, numbers, and the period (.). 4)There can only be a total of 40 characters in the variable name.

The final part of the declaration is the only part that is optional. This part is the type definition. The syntax for the type definition is "as [TYPE]". Leaving out this section will result in the type being defined as the type of the first value stored in it. For example, if the declaration "dim a" was used, a would have no type until a value was stored in a. So if the first value stored in a was 4, and the next was the text string "Hello.", there would be a compiler error. But if the string were stored without storing the 4 first, there would be no error. The syntactic names for the available variable types in Basic are: integer (a 16-bit signed integer variable), long (a 32-bit signed integer variable), single (a single-precision 32-bit floating-point variable), double (a double-precision 64-bit floating point variable), string * n% (a fixed-length string variable n% bytes long), and string (a variable-length string variable). As you can see, all of the variable types have different variations of themselves. This is so that you can optimize your programs for speed and efficiency. For example: if you want a variable that is always going to contain an integer value between say 1 and 100, you don't want to tie up a memory block big enough to hold huge numbers. So you would use an integer variable. But if you want a variable that is going to contain small integers some of the time and large integers other times, or will always contain large integers, you need the extra memory. So you would use a long variable.

The next thing you need to know about Basic is how it puts output on the screen, and how it asks the user for input. The syntax for output is "print [EXPRESSION]" where expression is a variable or a text string. But if the expression is a text string, it must be enclosed in quotation marks ("TEXT STRING" as opposed to TEXT STRING) so that the compiler doesn't confuse it with a variable. Input is entered into variables from the keyboard. The way to do this is to prompt the user for input with an input command. The syntax for the input command is "input [VARIABLE NAME]". What this command means is wait until something is entered from the keyboard. When something is, store it in this variable.

The syntax for the conditional statement in Basic is "If [CONDITION(S)] Then [STATEMENT(S)] Else [STATEMENTS] End If". This means if the condition is true, then execute the statements before the else. Otherwise, execute the statements after the else, but before the end if. In the conditional statement, the "Else" and the statements between it and the "End If" are optional.

Basic contains two loop types: the do loop and the for loop. The syntax for the do loop is "Do [STATEMENT(S)] While/Until [CONDITION]". The syntax for the for loop is "For [VARIABLE] = [NUMBER/VARIABLE] to [NUMBER/VARIABLE] Step [NUMBER/VARIABLE]". The "step" is optional.

Visual Basic

Visual Basic uses the Windows programming environment and uses object-oriented techniques. It allows you to create any of the Windows objects such as check boxes, command buttons, and images. These objects have many properties such as size, position, color, etc. which can be changed in design-mode or in run-mode. But these objects don't do anything by themselves. You still have to write code to tell the objects what to do. This is where the Basic coding comes in. By double clicking on an object while in design-mode, you can open a window where you can type code. This will create a procedure that is executed every time something is done with that object. For example, you could write a procedure that was executed every time the user clicked on the object, and have a different procedure be executed every time the user moved the mouse over the object.

As you may have guessed, there are some differences in the coding for Basic and for Visual Basic. But these differences are only additions so that the language can be compatible with Windows. Any Basic commands may be used in Visual Basic, but not vice versa. There is one main difference about variables. That is, that Visual Basic doesn't require you to declare your variables before you use them. And when you do declare them, it doesn't require you to declare what type they are. How does it do this you ask? Any variables used in Visual Basic that aren't specified otherwise are assigned to the type of variant. Variant is an extremely versatile data type that can store just about anything. The disadvantage of it is that is uses much more memory than any other type (except sets and arrays). So if you know exactly what will be stored in the variable, it is to your advantage to declare it as the correct type. But it isn't essential as in Basic.

Visual Basic can have multiple windows open at once. So it is necessary to keep track of all of them with names. For this reason, they have a special name. In Visual Basic, windows are called forms. Forms can be called and/or unloaded by other forms, objects, or code modules while in run-mode. The command to show a form is "[FORM NAME].Show". By executing the command "[FORM NAME].Hide", the form is made invisible, but is still in memory. By executing the command "Unload [FORM NAME]", the form is removed from view and is also removed from memory. You will have to decide which of these two techniques best fits the situation. When a form is loaded and unloaded many times during a program's execution, it should only be hidden so that it can be retrieved more quickly. However, if the program uses a lot of memory and can't afford such a luxury, the unload technique should be used.

Pascal

Pascal is one of the oldest languages there is. It is fairly simple to learn and use, but can be annoying to debug. This is because it has some syntax rules which aren't at all like English. But once you've learned them, they are logical enough that debugging shouldn't be too difficult.

The first difference between Basic and Pascal is that in Basic, the way to signify the end of a statement is by pressing "Enter" and going down to the next line. This is not the case, however, in Pascal. If your editor would allow it, you could theoretically write your program all on the first line. This is possible because in order to signify the end of a statement, you must type ";". So your program could be written as "[STATEMENT]; [STATEMENT]; [STATEMENT]; [STATEMENT]; [STATEMENT];" or as
"[STATEMENT];
[STATEMENT];
[STATEMENT];
[STATEMENT];
[STATEMENT];".
It is recommended that even though you can write many statements on one line, that you don't. Doing so makes your program very difficult to read.

The next difference between Basic and Pascal is the difference in their data types. Pascal has four main data types. These data types are the integer, real, char, and boolean. In addition to these four main types, Pascal also contains variables of type string, set, and array. Pascal also contains an ability to create user-defined types. A user-defined type is a custom data type that you create before running the program. Then, you can make variables of this type. For example, you could make a data type called "color". You could define this type as "red, yellow, blue, green, orange, and purple". Then, any variables you declare as type color could only contain values of "red, yellow, etc.". This can be very useful when working with data that has definite types, but that can't be any number or any character.

Now for the syntax of Pascal. The syntax for a conditional statement in Pascal is "If [CONDITION] Then [STATEMENT]". Notice that [STATEMENT] was used and not [STATEMENTS]. This is because every programming language has its own way of keeping track of what statements belong with what loop or what conditional statement. In Basic, all of the statements between the If and the End If would be executed. This is how it knows what statements go with the conditional statement and what statements go with something else. In Pascal, the way it keeps track of what goes where is by making only the first statement after a conditional, loop, etc. be part of that function. However, to overcome the problem that you may want more than one statement to be executed as part of a conditional statement, etc., Pascal has two commands called "Begin" and "End". Anywhere one statement can be executed in Pascal, it can be replaced with a Begin-End pair. All statements between the two commands are executed as if they where one command. This means that unless you are going to write a one command program, you also have to put Begin and End around your entire program. The only difference is that the End at the end of the program should be followed by a period (.), while all other Ends should be followed by a semicolon (;).

There are three types of loops in Pascal. The first type of loop is the while loop. The syntax for this loop is "While [CONDITION] Do [STATEMENT]". This will execute the statement as long as the condition is true. Since the check to see if the condition is true happens before the statement, it is possible for the statement to never be executed even once. The next type of loop is the repeat loop. Its syntax is "Repeat [STATEMENTS] Until [CONDITION]". This is sort of the opposite of a while loop. It executes all of the statements (not just statement-you don't need a begin-end pair) between the repeat and until as long as the condition is false. Also, since the conditional check isn't preformed until the end of the loop, the loop will definitely be executed at least once. The final type of loop is the for loop. The syntax for the for loop is "For [VARIABLE NAME] = [VARIABLE NAME/CONSTANT] To/Downto [VARIABLE NAME/CONSTANT] Do [STATEMENT]". The for loop will execute the loop and then either increase a loop counter by 1 if "To" is between the the two variable/constants or decrease it by one if "Downto" is between them. When the loop counter reaches the second variable/constant, the loop exits and execution proceeds to the next statement following the loop.

Delphi

Pascal isn't very widely used by any major businesses. This is mainly because of its simpleness. It isn't very powerful and has several limits. However its Windows, object-oriented version is one of the most powerful languages there is. This language is Delphi. Delphi is extremely similar to Visual Basic, but is Pascal-based instead of Basic-based. In fact, Delphi is so similar to Visual Basic, you may as well read about it to learn about Delphi. So click here if you would like to return to Visual Basic. Remember though, that the objects are coded with Pascal, not Basic.

C

C is one of the most powerful languages there is. You can do just about anything with it. However, it is also very difficult to learn. There is a very steep "learning curve", but once you get over it, programming in C isn't very hard to do. However, it is very similar to Pascal, so CIRCUIT will attempt to compare it to Pascal when possible.

First of all, C is similar to Pascal in the way that it keeps track of what goes with what by only allowing one statement to go with one function. However, instead of using a Begin-End pair to create multiple statements, you use the curly braces. These look like this: { and }.

A major difference between C and Pascal is that while Pascal tries to stick to syntax that is as close to English as possible by using English words (examples: not, and, or if..then), C uses special characters instead. This can make it very difficult to read a C program if you don't know C, while with Pascal, you would be able to guess with a good deal of accuracy as to the basic concept of the program--even if you didn't know Pascal. A good example of this is that in C, the way you negate a Boolean value (not) is by using an exclamation mark (!). So "!on" means "not on". Also, any functions such as If or While must be followed by parentheses for their conditions. So the syntax is "If([CONDTION]) {[STATEMENTS]}" instead of "If [CONDITION] Then [STATEMENTS]". Likewise, the syntax for the While loop is "While([CONDITION]) {[STATEMENTS]}". But C is a little more flexible about this than Pascal. Remember that in Pascal, you were required to write the condition before the loop. The only way to avoid this was to use the Repeat loop. But in C, it is fine to make a loop as shown above or by saying "Do {[STATEMENTS]} While([CONDITION])". Also, you have the choice of using While or Until in any loop.

Visual C++

Again, Visual C++ is the Windows version of C. Read about Visual Basic to learn about Windows programming and just remember that everything is coded in C instead of Basic.