 ## Program control structure

Subject: Computer Science

Find Your Query

#### Overview

The structures which regulate the order in which program statements are executed are called Control Structures. Sequence is the set of program instructions which follow one another and are to be executed unconditionally. Selection is the set of instructions which are to be executed conditionally. Iterations are the computer instructions which are to be performed repeatedly and conditionally.

### INTRODUCTION

The structures which regulate the order in which program statements are executed are called Control Structures. There are 3 types of control structure. They are:

• Sequence:
It is the set of program instructions which follow one another and are to be executed unconditionally (not dependent on any program conditions). Instructions are put in a predefined sequence (just like a queue in a cinema hall) and the next instruction is executed by CPU only after the execution of the previous instruction (C never comes before B). Fig: Sequential StatementSource: www.slideshare.net

• Selection:
It is the set of instructions which are to be executed conditionally i.e. they are executed based on a condition that can be either true or false. Commonly used logic for selection are if condition, if else condition, if else if condition, nested if else condition and switch case condition.

1. If condition:
If condition is used in case the given problem has only one condition and only one action. Considering either true or false part, if the given condition is true then the statement will be executed. Otherwise, the control exits from the condition. Fig: if conditionSource: www.ustudy.in

Example: Check the number is positive. Algorithm

Step 1: start

Step 2: input N

Step 3: if (n>0), output is positive

Step 4: end

Figure: 2. If else condition:
This condition is used if the problem has one condition but two alternative actions. Here, if the condition is true, statement 1 will be executed; otherwise ,statement 2 will be executed. Example: Find the greatest between 2 numbers.

Algorithm

Step 1: start

Step 2: input X, Y

Step 3: if (X>Y)

output X is greatest
else
output Y is greatest

Step 4: end

Figure: 3. If else if condition:
Also known as ladder type if else, we can use this condition if the given problem has more than one interrelated conditions with their respective actions. Here, on a check, if condition 1 is true then, statement 1 is executed. Otherwise, condition 2 is checked and if it is true, statement 2 is executed and so on for next conditions. If all conditions are false, then the last statement will be executed. Example: Find the greatest among 3 numbers.

Algorithm

Step 1: start

Step 2: input P, Q, R

Step 3: if (P>Q && P>R)
output P is greatest
else if (Q>R)
output Q is greatest
else
output R is greatest

Step 4: end

Figure: 4. Nested if else condition:
Nested if else condition is an entire if-else statement which is written within the body of if part or else part of another if else statement. This condition is used when a condition is to be checked that is inside another condition at a time in the same program, to make a decision. Example: Find the greatest 2 positive numbers.

Algorithm

Step 1: start

Step 2: input X, Y

Step 3: if (X>0 && Y>0)
{
if (X>Y)
output X is greatest
else

output Y is greatest
}
output X or Y may be
negative

Step 4: end

Figure: 5. Switch case condition:
If the given problem has one condition and respective more than two actions, then in this type of case scenario, we can use Switch case condition. It is the multiple branching statements which checks the value of the variable to the case value and then, the statements that are associated with it will be executed. If any expression does not match any of the case value, then the default statement will be executed. Example: Write an algorithm and draw a flowchart which takes the integer value 1 to 7 and prints respective day.

Algorithm

Step 1: start

Step 2: switch (day)
{
case 1: output Sunday
break
case 2: output Monday
break
case 3: output Tuesday
break
case 4: output Wednesday
break
case 5: output Thursday
break
case 6: output Friday
break
case 7: output Saturday
break
default: output out of range

}

Step 4: end

Figure: • Iteration:These are the computer instructions which are to be performed repeatedly and conditionally i.e. loop statements are driven by the loop condition. Commonly used logic for iteration are while loop, do while loop and for a loop.

1. While loop:
In this loop, first, the condition is checked by the computer and if the condition turns out to be true, then the statement inside the loop is executed. This process is repeated and the value of increment and decrement operator is always changing. When the condition is false, the loop stops.

Algorithm Syntax
Initialization
while (condition)
{
statements
……………….
……………….
increment/ decrement
} Example: Write an algorithm and draw a flowchart to print 1 to 10.

Algorithm

Step 1: start

Step 2: I=1

Step 3: while (I<=10)
{
output I
I++
}

Step 4: stop

Figure: 2. Do while loop:
In this loop, first, the computer checks the initial value; second executes the statements inside the loop and finally, checks the condition. The process is repeated for next pass, if the condition is true. Otherwise, the loop stops. If the condition is initially false, it will execute for at least one time.

Algorithm Syntax

Initialization
do {
statements
………………..
………………..
increment/ decrement
} while (condition) Example: Write an algorithm and draw a flowchart to print 100 to 1.

Algorithm

Step 1: start

Step 2: A=100

Step 3: do {
output A
A++
} while (A>1)

Step 4: stop

Figure: 3. For loop:
It is the most commonly used loop. It consists of 3 expressions; initialization, condition and counter, which are defined within a statement.

Algorithm Syntax

for (initialization; condition; counter)
{
statements
……………….
……………….
}
Where, initialization is starting point,
the condition is stopping point and increment/ decrement is a counter. Write an algorithm and draw a flowchart to print a multiplication table of 7.

Algorithm

Step 1: start

Step 2: for (i=1; i<10; i++)
{
m= i*7
output m
}

Step 4: stop

Write an algorithm and draw a flowchart to print a multiplication table of 7.

Algorithm

Step 1: start

Step 2: for (i=1; i<10; i++)
{
m= i*7
output m
}

Step 4: stop (Koirala & Shrestha, 2015)

#### Bibliography

Koirala, H., & Shrestha, R. K. (2015). Computer Science I. Anamnagar, Kathmandu: Buddha Publication.

Adhikari, Deepak et.al., Computer Science- XI, Asia Publication Pvt. Ltd, ktm

##### Things to remember

Program Control Structure & its types:

• Sequence.
• Selection; if condition, if else condition, if else if condition, nested if condition and switch case condition.
• Iteration; while loop, do while loop and for loop.

• It includes every relationship which established among the people.
• There can be more than one community in a society. Community smaller than society.
• It is a network of social relationships which cannot see or touched.
• common interests and common objectives are not necessary for society.

© 2019-20 Kullabs. All Rights Reserved.