3 Basic Structures of Programming in any LanguageSubmitted by Michael Taylor on Sun, 01/20/2013 - 15:02
In programming, in any language, their are 3 basic structures that should be maintained in the first steps of planning and implementing your logic. Mathematicians have long proved that any system or process can be fully described using only these three structures.
The 3 basic structures are:
Here I will briefly cover each of these structures and use examples on where they might be used.
Structural loops are very useful when performing iterative calculations. These types of calculations are very common in any application that involves manipulating large amounts of data or abstract data types that handle collections of objects. The loop allows you to instruct the compiler to repeat the same steps for as many iterations as necessary in a very small block of code as oppose to directly programming each step which is often times not feasible realistically.
There are several kinds of loops that should be considered, the first is the counted loop. The counted loop is iterated over a predefined number of times. This is most common when you are working with a predefined data set. Like an employee list for example.
Another kind of loop is called a continuously evaluated loop. This type of loop doesn't know ahead of time how many times it will be iterated. It will continue to evaluate until it finally test a predefined condition to be true or it encounters an error or null reference exception. For example until the loop runs out of resources to allocate.
Then there is the endless loop, this will run forever once it's started or the system exits or crashes. Common uses for this might be a game loop in most video games, other uses might include any type of system that runs cron jobs, application and hardware monitoring.
Finally, there is the iterator loop. These types of loops are very useful for iterating through a dynamic collection of objects and running routines on them individually.
Sequences, or sequential statements are structures in which the order of execution matters and must be performed one after another. The concept of sequences is a concept of dependencies. Each individual statement depends on the result of the previous statement. These are very useful while utilizing transformative routines in which a multi step algorithm is used to derive certain data out of a data source. The most common example that I have personally used is using XML to save the state of a system and then using it to reconstruct that state at a latter time. (Aka saved game files) Often times you might encounter an abstract data type that cannot be serialized as XML. So instead you might serialize a set of more primitive data types that describe the ADT and use that to reconstruct the ADT. Almost every instance when I have tackled this, I have had to use sequential statements to transform and derive the XML into the proper ADT.
Another name for a selection is called a conditional. Conditionals are used any time a decision needs to be made. Based on the data provided a conditional must decide whether to go left or right. The biggest challenge to consider while implementing conditionals is managing nesting. Nesting happens when you have conditionals within conditionals. If improperly managed, nesting can cause code that is hard to read and follow. Improper nesting is also error prone. It is important to keep the level of nesting a low as possible. A common useful technique is to place exit and return conditionals first in a routine and then implement the main block of code afterwards. What I mean by exit and return blocks of code is any conditional that if test to be true exits the routine immediately.