0% found this document useful (0 votes)
3 views

Programming constructs

The document discusses basic programming constructs for beginners, focusing on three main types: sequence, selection, and repetition. It explains how these constructs control the flow of instructions in a program, using relatable examples such as calculating employee salaries. The article aims to simplify programming concepts for newcomers, encouraging them to understand and apply these constructs in their coding journey.

Uploaded by

Gurbaksh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Programming constructs

The document discusses basic programming constructs for beginners, focusing on three main types: sequence, selection, and repetition. It explains how these constructs control the flow of instructions in a program, using relatable examples such as calculating employee salaries. The article aims to simplify programming concepts for newcomers, encouraging them to understand and apply these constructs in their coding journey.

Uploaded by

Gurbaksh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Find related posts...

Powered by Algolia Log in Create account

32

26

Mukit, Ataul
Posted on Mar 31, 2018

26 6

Programming Constructs for Beginners


#programming #constructs #beginners

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

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.

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

AWS PROMOTED

Innovation streaming live from AWS re:Invent


Join hosts from AWS and AWS Partners as they discuss the latest news, trends,
and strategies for driving success on the cloud. Streaming live from AWS re:Invent
in Las Vegas.

Register now

Top comments (2) Subscribe

Add to the discussion

Enambriat1996 • Nov 11 '23

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

perplexingcode • Dec 28 '21 • Edited on Dec 28

Thanks Mukit, the explanation is beautiful

Reply
Like

Code of Conduct • Report abuse

Sentry PROMOTED

See why 4M developers consider Sentry, “not bad.”


Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can
help.

Learn more

Read next

What is Feature Switching in .NET 9 ?!


Hamza - Nov 22

Como publicar um pacote no npm: um guia passo a passo


Juliana Macêdo - Nov 13

Small But Mighty: Survey of Small Language Models in the LLM


Era
Mike Young - Nov 9

Demystifying CXL Heterogeneous Systems with Heimdall


Benchmark
Mike Young - Nov 9

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

More from Mukit, Ataul

Connecting LLM to a Real-World Robot


#robotics #ai #programming #automation

What Exactly is a Programming Problem? My Perspective ..


#programming #problem

Basic Programming Concepts for Beginners


#programming #tutorial #beginners

TON PROMOTED

Hackers, Founders, Builders: This $3M Hackathon Is for You.


TON Hackers League is calling all builders. Compete for a $3M prize pool, join 19 IRL bootcamps, and

You might also like