DEV Community

loading...
Cover image for Programming Constructs for Beginners

Programming Constructs for Beginners

lucpattyn profile image Mukit, Ataul ・4 min read

Last article (https://dev.to/lucpattyn/basic-programming-concepts-for-beginners-2o73) discussed how variables reside in memory, how they are referred and used for basic operations and finally getting assigned or overwritten. Considering that as basement, it is time to move on to the first floor and the higher ones..

Programming Constructs

Recall from last time, a program is a set of instructions that the computer executes. But I did not get into details about the order they would be executed.
This is where programming constructs come into action. They are used to control the order/flow in which instructions are executed (or not executed). In programming languages, the expression which translates to an instruction is called a programming statement or just statement.

There are a number of recognized basic programming constructs that can be classified as follows:

1) Sequences (First Floor)
2) Selection (Second Floor)
3) Repetition (Third Floor)

We can also add routine invocation to this (as Fourth Floor). But lets stick to 1st, 2nd and 3rd for the moment.

I have used the concept of 'Floors' to help you visualize better how to go about constructing a program.

Remember from last time - a program is a set of instructions loaded in the CPU that the CPU executes to achieve an outcome. Let me rephrase it by
a program is a set of instructions loaded in the CPU that the CPU executes in a certain order (or certain orders when you have more than one cpu) to achieve an outcome

now moving on ..

1. Sequence

A sequence construct tells the CPU (processor) which statement is to be executed next. By default, in popular languages, this is the statement following the current statement or first statement in the program. In other words, this is the very basic construct of writing a program. You just write line by line what you have in mind (of-course related to programming).

Apart from the default sequence, some languages support "goto" statement to skip certain statements and jump to a completely different set of statements; however this is very much discouraged. Eager readers may look it up in the web, given they have understood the other programming constructs well.

2. Selection

A selection statement provides for selection between alternatives, alternative as in available route options for instruction execution.
A program can take certain route depending on a situation and selection statements help in choosing between the routes.

For example,
In a factory, if an employee is present then calculate the salary for 8 hours,
otherwise do not calculate the salary, just put a big zero (no work no doe).

So depending on the state of the employee (whether present or not) you are asking the program to do one of two things - a) when employee is present calculate salary, b) when employee is absent put salary as 0 and the processor will choose between taking "path a" or" path b" but not both.

So this is an example of Selection with a little bit of illustration below:

Now on to repetation ..

3. Repetation

A repetition construct causes a group of one or more program statements to be invoked repeatedly until some end condition is met.

Let's stick with the salary case, and try to give the salary for a whole month.
For simplicity sake, let's consider we have 20 working days in a month and we have to calculate the whole month's salary for the employee.

Since we already calculated the salary for 1(one) day based on employees's present or absent status, we repeat the same process for 20 days and Ta'Da we have calcuated the whole month's salary for the employee. Simple (!) isn't it ?

In this case "some end condition is met" would refer to the whether we have repeated the same sequence of instructions or iterated 20 times or not. We also use the term 'loop' instead of repetation or iteration.
(Here 20 iterations are required for 20 days).

So the whole process in one visualization below:

Notice that almost seamlessly we have moved from one floor to the next starting from ground zero (basic programming concepts).
The first floor is where normal things happen one after the other, where we have only a basic flow - statement after statement.

Then we go up to second floor where employee salaries are calculated for the day. In this floor we learnt how to chose between calculating salary or skipping it - choosing the right path under certain conditions.

Lastly to the third floor, where a very significant thing takes place. There, iterating over experiences (steps taken) of previous floors, we actually developed a complete work flow where employee salaries are are calculated and given at the end of a (20 working day) month.

Believe it or not, we just stepped over the very the essence of programming -

  1. Give instructions one after another
  2. Select certain paths based on certain conditions
  3. Iterate/loop through previous steps over and over until we have achieved a certain outcome.

Now with this in mind, if you start looking at codes from internet, hopefully you can make sense of some of it, given you learn the basic constructs for a language you are interested in.

Happy Coding.

This is second in the series of articles for my niece Ahna. She has just finished her high school. She has given judgment along with those still in their late teens - the approach is easy to make sense of programming. However comments and constructive criticism are always welcome ..

Acknowledgements:

https://cgi.csc.liv.ac.uk/~frans/OldLectures/2CS45/progCons/progCons.html

History:

Article first posted - March 31, 2018

Discussion (0)

Forem Open with the Forem app