Control Structures
While problem-solving it is necessary to fulfill every condition of the problem. Therefore, while designing the program logic we require to change the program flow according to certain conditions. This is because it is not necessary that the program always executes sequentially. Hence, for such conditions, we use the control structures.
Control structures basically control the flow of the program according to pre-decided parameters. Hence, the name control structure. It is important to identify such conditions while designing the solution for the problem. While writing the algorithms and pseudocode the programmers can decide the need for the control structures.
Types of Control Structures
There are three types of control structures as follows:
Decision Control Structure
We use such a control structure when we need to execute a particular set of statements based on a particular condition. Hence, they work on certain conditions that give boolean result in ‘true’ or ‘false’. Therefore, before executing the statements inside this it checks the expression that always gives a boolean result.
The block of statements will execute if the expression results in true otherwise the block is ignored. For example, the ‘if-else’ control structure.
Selection Control Structure
We use such a control structure when we need to execute the statements depending on the user’s input. Usually, such conditions occur when we have certain choices for the user and he needs to select one choice as the input. Hence, the name selection control structure. For example, giving a menu to the user and asking to input a particular dish which he wishes to order. Examples of such control structures can be the ‘switch’ statement, ‘case’ statement.
Loop/Repetition Control Structure
We use such a control structure when we need to repeat a particular instruction or number of instructions multiple times. Moreover, there can be a condition until which the particular block of statements will execute. Besides, the number of repetitions can be re-decided or may depend on the result of a particular expression.
Examples of such control structure are for loop, while loop, do-while loop, etc. Moreover, based on the number of repetitions, we can divide it into two types:
Conditional Looping/Finite Looping
In this structure, repetition happens only a particular number of times. Moreover, the number of repetitions depends upon a particular condition. Hence, once this condition satisfies, the control stops executing the block of statements. We can call this the termination of the loop. For example, let us take a C program
#include
void main()
{
int i;
for (i=0; i<=5; i++)
{
printf ("%d ",i);
}
}
Output: 0 1 2 3 4 5
Here, we can see that the initial value of i is 0 and the value of i keeps printing until i is less than or equal to 5. Hence, the output prints the numbers from 0 to 5.
Browse more Topics Under Problem Solving Methodologies
- Understanding of the Problem
- Solution for the Problem
- Breaking Down Solution into Simple Steps
- Identification of Arithmetic and Logical Operations
- Introduction to Algorithms/Flowcharts
Infinite Looping
This type of looping can happen in two cases. One, we do not specify any condition. Two, the condition never gets satisfied. Therefore, in such cases, the condition never gets fulfilled and hence, the repetition keeps taking place infinite times. Hence, the name infinite looping. This type of looping is the opposite of finite looping.
For example, let us take a C program
#include
void main()
{
int i;
for (i=6; i>5; i++)
{
printf ("%d ",i);
}
}
Output: 6 7 8 9 10 11 12 13 .......
Here, the value of i will always be greater than 5 therefore, the loop will keep executing.
Leave a Reply