What are Programming Constructs? Explain any 5 programming constructs
Answers
Answer:
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.