Programming constructs
Programming constructs
32
26
Mukit, Ataul
Posted on Mar 31, 2018
26 6
ANTAS
WIGANS
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.
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.
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).
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 -
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
AWS PROMOTED
Register now
Today I want to tell you more important information. For those looking for a
more affordable option, garage floor paint is another possibility. Although
not as durable as epoxy or tiles, garage floor paint is still a great option to
improve the appearance and functionality of garage flooring LaGrange
With the various choices available, homeowners can find the perfect
garage flooring solution to meet their needs and style preferences.
1 like Reply
Like
Reply
Like
Sentry PROMOTED
Learn more
Read next
Mukit, Ataul
Follow
I consider myself as an enthusiast in learning new things everyday, about our history and future as
well as the present. My interests: Ancient History, Comparative Religion, Science and Tech, C++
LOCATION
Dhaka, Bangladesh
JOINED
Dec 14, 2017
TON PROMOTED