|
|
|
There are some fundamental computer programming concepts that all programming languages share.
Regardless of whether a computer program is written in "C", Java, FORTRAN or AppleScript it will depend on four core concepts. They are:
What is a variable?A variable is something that can vary while a program is running. It may be a checkbook balance that gets increased or decreased or the statistics of an athlete. Variables have names assigned by the programmer. Some languages have strict rules about the form of variable names, for instance they must begin with a "$". In most languages variables can either be used everywhere in the program (global) or just in one part of the code (local.) Since global variables can be used throughout the program their names have to be unique. Local variables only exist within a specific section of the program and therefore can have the same name as other local variables in different program sections. The value of a variable is set while the program is running. TIP: Many programmers start each global variable's name with a "g" to indicate it's a global variable. What is a constant?Constants are really just a way of assigning a name of a value. For instance, instead of having to enter 3.14159265358979323846 to use PI in calculations, our program can create a constant called kPIand assign it the appropriate value. Saves a lot of typing and potential mistakes. The value of a constant is set when the program is "stored". TIP: Many programmers start each constant's name with a "k" to indicate it's a constant. Types (of data)The basic types of data are:
Most programming languages handle and store different types of data differently. These languages use "strict typing" of variables and generally don't allow code that does operations (e.g. addition) on different types. For instance: "c" + 7 would not be allowed because it doesn't make strict sense to add a number to a word. Other languages are more relaxed and change the types of variables in a statement to suit the need, if possible. They don't all do this the same, though, so it's important to keep in mind the type of data all your variables represent and the automatic conversions that the language performs. For instance "c"+7 might result in "c7" in one language and "j" in another and 106 in yet another. It's a good idea to have a standard naming scheme to document what kind of variable each one is. For instance an identifying letter can be added to the end of the name of each variable to indicate its type. Flow ControlVery few tasks can be achieved without decision points. These decision points might test a variable (such as creditcard_balance) and cause different parts of the program to run depending on the result. For instance: IF creditcard_balance is greater than a certain amount THEN send a notice to the card holder ELSE don't do anything. There are several types of flow controls in most languages. They are:
Here is a very brief description of each: Branching
Loops
Which kind of branching or looping you use is largely a matter of your programming style, although you'll find in most cases, one type will make more sense than another. These are the basic concept your need to understand any computer programming language. The specifics can be found in language references.
The copyright of the article Computer Programming Fundamentals in Computer Programming is owned by Peter Jorgensen. Permission to republish Computer Programming Fundamentals in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|