Problem Solving Methodologies

Control Structures

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

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.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.