Programming Constructs
While there are several high-level languages, each with a different syntax, they are semantically similar. In other words, the instructions may be worded differently, but they will perform the same or a very similar operations. Programs use the same set of basic constructs, regardless of what language they are written in. If you understand these constructs you will be able to write programs in any language, you would only need to learn the syntax.
The instructions that can be given to a computer in a computer program can be broadly categorized into the following:
-
Input:
Get data to the computer from the keyboard, some other device, or by reading a file from a storage device or over the network. The data can be text, numbers, images, or even audio and video data. -
Processing:
Do something with the data, such as computations on numbers, modifications to an image, or edits to a video. Processing can be further categorized into:- Arithmetic, which includes operations, such as addition, multiplication, and many other complex mathematical operations.
- Conditional, which uses data and logic to decide what instructions should be executed under what conditions.
- Repetitive, which can repeat the same process on different data (loops) unless instructed to stop or all the data has been processed.
- Arithmetic, which includes operations, such as addition, multiplication, and many other complex mathematical operations.
-
Output:
Present the processed data to the user, either displaying it on a screen, or printing it on paper, or sending it to some other computer.
Programming Paradigms
There are two main programming paradigms. The purpose of the program usually determines the choice of approaches.
-
Imperative Programming:
In this style of programming you write a series of instructions that specify to the computer what to do and how to do it. For example, algorithms to perform computations or instructions to manipulate the hardware to receive inputs or display data, give precise instructions on how the computation should be performed, or what action is to be taken. -
Declarative Programming:
In this style of programming, the instructions specify what needs to be done without specifying exactly how it should be done. For example, when retrieving data from a structured data store, the program may ask for what data it wants, and the data store will determine how to extract and send the data to the program. Or, when programming a web page, the program states what should be displayed, and the rendering application (in this case, the browser) will decide how to show it, based on the computer screen size.
These basic constructs will be explained using the JavaScript programming language as a reference, but they are relevant to most languages with only differences in the syntax (words in the programming language and how the program is organized). Instructions in a program are referred to as statements.