1. How Programming Works
Begin your journey here.
Sherman Chin, Wednesday 06 October 2004 - 10:13:48

This article is meant as an introduction to the concept of programming. It is aimed at the absolute beginner so feel free to skip this if you know programming like the back of your hand. I feel obligated to write this article since we are here to programming without any prior computing expertise. In fact, the reason why I started Sherman3D is because I myself could not find any web site dedicated to absolute novices when I took my first plunge into programming. So, if this applies to you at this very moment, let us boldly go where no other site has gone before.

What is programming? In order to answer the question, we have to know what a computer program is. A program can be described as a collection of instructions that humans type into computers to get computers to perform specific tasks.

A program is basically lines of code that we type into the computer that represent instructions to make the computer do some useful work for us. Hence, programming is the act of keying in the lines of code to make the computer do what we want it to do.

How does programming work? First of all, you, as the programmer, will type in the programming language (whether C++, Java or assembly language) source codes into a plain text document on the computer. You will then have to type the command on the command prompt (usually the black and white screen with the cursor) to compile the program, e.g. tasm myProgram.asm. (Here I am assuming that you are compiling an assembly language program, thus the .asm extension. If you are programming in some other language, you have to name your text file with the extension of that particular programming language so that the compiler will recognize them e.g. myProgram.cpp -for C++ and myProgram.java - for Java.

During the compilation stage, your programming language compiler will compile your plain text source code into object code that is understandable by the computer. The compiler will check if your source code complies with the syntax of the particular programming language. However, the object code will still not be able to run as you may have more than one file that contains your code or there may be other files that are required by your specific computer platform (DOS, UNIX, Macintosh, OS/2) to produce the output required by your program e.g. DOS might have a different method of displaying text on your computer screen than UNIX, hence we need the extra files to be linked with your source code to tell the computer how to display the text. For this matter, we will need to link our object code files with other relevant files.

The compiler will have generated object code files e.g. myProgram.obj after you compile your program. To link our object code file, we just type in the command to link the files, e.g. tlink myProgram.obj - for assembly language. After linking, the linker will generate our executable file e.g. myProgram.exe. Now to run the program, all we need to do is type the program name on our command line (and press the "Enter" key, of course) e.g. myProgram to get the program to actually run and perform the tasks we programmed it to do.

Okay, some beginners will now be feeling very confused after all that talk about compiling and linking. Well, the good news is that most programming languages today come with a Integrated Development Environment (IDE). When we start the IDE, there will already plain text document ready for us to type our programming language source codes in. In fact, the IDE might even automatically format and color our source codes while we are typing them in to help us identify errors. Furthermore, when we are done typing, we just need to use the mouse to click on a graphical button to run our program. The IDE will take care of the compiling and linking of our program automatically and straight away display the output of our program without hassling us to compile and link our programs manually. So, if I were you, I would spend some money on a user-friendly IDE than go through the tedious process of manually compiling and linking my source codes. I just detailed the entire process in this article so that you will know what goes on behind the scenes of your IDE.
Time to take a closer look at the source code - the main focus for you as a programmer. The source code is just a collection of plain text statements that are significant only because they mean something to the compiler. In brief, these statements consist of commands that tell the computer to accept input from the user, display output on the computer screen, calculate using our standard mathematical operations (+, -, *, /), so on and so forth. Besides, direct commands, there are certain logical constructs that govern the flow of a program. These are known as the programming logic. Let us see one of these logical constructs - the "ifelse" construct. Basically it is used to evaluate if a logical operation is true of false. If it is true, then the program will execute the statements following the "if" keyword. If it is false, the program will execute the statements following the "else" keyword. E.g.

if (value > 5) 
    display "The value is more than 5"; 
    display "The value is less than 5; 

Observe the example source code segment above. It tells the program to display the message, "The value is more than 5" and then quit the program if "value" is more than 5. Otherwise if "value" is not more than 5, the program should display the message, "The value is less than 5". Notice that we have a few delimiter symbols such as the parentheses, (), braces, {}, and semicolon, ;. The parentheses enclose our logical operation statement(s), the braces encloses the command statement(s) that specifically tell the computer what to do, and the semicolon indicates the end of a particular command statement. So that is how the compiler separates your statements into something it understands.

In our example, "if", "display" and "quitProgram" are keywords, meaning that they are part of the particular programming language's vocabulary - it is part of the language. Therefore, the compiler has no trouble identifying them as commands telling it what to do. However, "value" is not a keyword. Instead, it is a user-defined variable. Think of a variable as a box in the computer's memory that can store different values. The word "value" is user-defined, meaning we can name it anything we want as long as it complies with the language specifications. We could have simply named "value" to "x" and achieve the same result when we use the "if...else" statement i.e. if (x > 5) {...

Definitely, before we use the "if...else" statement to check "x" is more than 5, we should have assigned an actual value to "x". We should have typed: x = 1 or something similar to indicate the value of x so that the logical "if...else" operation has actually something to compare with. In this case, the "box" in the computer's memory labeled "x" has the value 1 and is therefore less than 5. Usually, the values of variables vary during the course of the program due to some commands that the programmer has entered into the source code or because they are dynamically modified by the user during the execution of the program. That is the reason why they are named variables. Variables are an important aspect of programming and although daunting to the beginner, it should be of second nature once you start programming.

On the other hand, constants are a contrast to variables. They are constant and never changing during the execution of the program. We have typed in the string constant, "The value is more than 5", as well as, the string constant, "The value is more than 5" in our example. We also typed in our integer constant, 5, when doing our logical comparison. These values never change during the execution of the program since they are hard-coded into the program by the programmer. They are fixed simply because we typed them into the program source code without assigning them to variables in memory.

I guess that is enough yapping from me. If you are still a little confused, do not worry. Just follow the articles on this website and you should be able to catch on easily. I have structured my tutorials so that they flow smoothly if you are faithful to the series. I only explain what I think you should know at that point of time to understand any one of my tutorials and leave out the gritty details until the time you need to know them to understand something I am explaining. Therefore, if you would like to know more at a quicker pace, please buy any reference book on the subject matter. Nevertheless, I will try to be as comprehensive as possible and you can be sure that if I did not cover some important aspects in a particular tutorial, I will cover it in an upcoming one. If not, please feel free to e-mail me and tell me to do so. In fact, please feel free to contribute articles and tutorials that will help the beginner to build their knowledge from the ground up. After all, Sherman3D is all about gaining and sharing knowledge.

With that in mind, let us move on to our first tutorial of the C++ programming language.

this content item is from Sherman3D
( http://sherman3d.com/S3Dplugins/content/content.php?content.2 )