Programming Constructs: A Beginner’s Guide

Falalu Ibrahim
4 min readNov 5, 2023

--

The three basic programming constructs

What are the basic structures of computer programming?

The concept of the three programming constructs

Sequence, selection, and looping (Iteration/repeatition) represent the three fundamental structures in computer programming. When these three logical constructs combine, they give rise to algorithms capable of solving a wide array of logical challenges. This amalgamation is at the heart of what we call structured programming.

  • Sequence
    — Sequence is the first of the three programming constructs. In programming, instructions are executed one after another. Sequence is the order in which the instructions are executed.
    — The sequence of instructions is extremely important because carrying out instructions in the wrong order may result in a program performing incorrectly.
    — The concept of sequence can be written as pseudo-code or flow chart. Pseudo-code is a method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding. While a flowchart is a method of writing up a set of instructions for a computer program using diagrams. The following is an example of an algorithm for adding two numbers:
#Pseudo code for adding two numbers.
Begin.
WRITE “Please enter two numbers to add”
READ num1.
READ num2.
Sum = num1+num2.
WRITE "The total is: ", Sum.
End.
// The same algorithm implemented in C programming language.
int main(){
float num1, num2; // identifiers must be known before use.

std::cout<<"Please enter two numbers to add";
std::cin>>num1;
std::cin>>num2;
sum = num1 + num2;
std:cout<<"The total is: "<<sum;
return 0;
}
  • Selection
    — Sequence is the second of the three programming constructs. In programming, there are occasions when a decision needs to be made. Selection is the process of making a decision based on a result or outcome of certain event. The result of the decision determines which path the algorithm will take next.
    — Selection logic for example, could tell a user whether they are old enough to cast a vote for a candidate during an election. If the user’s age meets the required voting age, the program would follow one path and
    execute one set of instructions. Otherwise, it would follow a different path and execute a different set of instructions.
    — The concept of sequence can be written as pseudo-code or flow chart. as well. The following is example that checks if a person is eligible for voting based on his or her age:
#Pseudo code for cehecking eligibility of voters.
Begin.
WRITE "Please enter user's age to check"
READ age.
IF age is greater or equal 18
WRITE "Eligible to vote"
ELSE
WRITE "NOT eligible for voting"
END IF
End.
// The same algorithm implemented in C programming language.
int main(){
int age;
std::cout<<"Please enter user's age to check";
std::cin>>age;
if (age >= 18){
std:cout<<"Eligible to vote";
}else{
std:cout<<"NOT eligible for voting";
}

return 0;
}
  • Looping / Iteration / Repeatition
    — Looping is the third of the three programming constructs. In programming, there are occasions when an action or set of actions needs to be repeated as long as certain conditions hold true. There are two types of iteration; controlled iteration and condition-controlled iteration.
    — Count-controlled iteration: this is king of loop that repeats a set number of times, usually implemented using a FOR loop. A loop counter is used to keep track of how many times the loop has iterated.
    — Condition-controlled iteration: this is a kind of loop that repeats until a condition is either met or no longer met. This kind of loop is usually implemented using a WHILE or DO WHILE loop. The following is example print natural numbers from 1 to 10:
#Pseudo code for printing / displaying digits from 1 to 10.
Begin.
FOR i = 1 to 10:
WRITE i
END FOR
End.
// The same algorithm implemented in C programming language.
int main(){
int i;
for (i = 1; i<=10; i++){
std:cout<<i;
}
return 0;
}

All programs use one or more of these three programming constructs. The longer and more complex the program, the more these constructs will be used repeatedly in the program.

Programming constructs are the fundamental elements that empower developers to bring their ideas to life in the digital realm. This comprehensive guide has unraveled the intricacies of these constructs, shedding light on their significance and practical applications. Whether you’re a novice coder or an experienced developer, understanding these building blocks is essential for creating efficient, reliable, and elegant software. As you continue your coding journey, remember that programming constructs are the threads that weave the tapestry of your digital creations. Happy coding!

--

--