Programming Constructs for Beginners 
Tags: programming, constructs, beginners
Published: 2018-03-31 11:00:30

---
title: Programming Constructs for Beginners 
published: true
description: Continuing from previous article which dealt with concepts, this article deals with constructs that control the flow of a program
tags: programming, constructs, beginners
cover_image: https://thepracticaldev.s3.amazonaws.com/i/riqrl2fo68ndwe4t6gse.png
---

<i>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.. </i>

<img src="https://thepracticaldev.s3.amazonaws.com/i/bmr8mqwbg62modu7fbzf.jpg"/>

<h2>Programming Constructs</h2>

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.

<small>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
<i>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</i></small>

now moving on ..
<h2>1. Sequence</h2>
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).

<img src="https://thepracticaldev.s3.amazonaws.com/i/obqvag8n61tuvce59ys8.png"/>

<small>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. 

<h2>2. Selection</h2>
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:

<img src="https://thepracticaldev.s3.amazonaws.com/i/7lobdqoi1uv572qbmymg.png"/>


Now on to repetation ..


<h2>3. Repetation</h2>
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 <i>repeated the same sequence of instructions</i> or <i>iterated</i> 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:

 
<img src="https://thepracticaldev.s3.amazonaws.com/i/ebiaw18wnm51np5fowji.png" />

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  - <i> statement after statement</i>.

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 - <i>choosing the right path under certain conditions</i>.

Lastly to the third floor, where a very significant thing takes place. There, <i>iterating</i> 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. 

<img src="https://thepracticaldev.s3.amazonaws.com/i/uppz8ul5yb0hfmphz5ev.jpg"/>

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.

<small><i>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 ..</i></small>


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


<h2>History:</h2>
Article first posted - March 31, 2018