V. M. Dhivya
When a car is assembled, the order of assembling it is important. For instance, the frame must be built before the wheels are put on it. Sometimes the major components are built separately and then brought together in an assembly line. Therefore the order of assembly for each component matters a lot.
Similarly, in computer programming too, a control structure determines the order in which statements are executed. The program’s logic will flow through statements from top to bottom. Control Structures are used to alter the flow of execution of the program. Why do we need to alter the program flow? The reason is “decision making!” For example, in school, students are given different options to choose their streams of subjects such as science or management. Students choose different streams based on certain conditions such as personal interest or scope of job opportunities. With the decision, the direction of their life is decided. Similarly, in programming also we use control structures to make decisions and alter the direction of program flow in one or the other path(s) available.
A Control Structure is a block of programming that analyzes variables and chooses a direction in which to go based on the given parameters. The term ‘flow control’ details the direction the program takes (which way program control “flows”). Hence it is the basic decision-making process in computing; flow control determines how a computer will respond when given certain conditions and parameters. Control structures are classified into two types, namely, selective statements and iterative statements.
Students will learn how statements are connected by simple but powerful control structures that have a single entry and exit point. Collectively, these structures can handle any situation. Their proper use leads naturally to a well-structured program.
Let’s learn the concepts through a card game.
Game 1: The student will play one on one. A deck of cards is shuffled and placed in the middle. The game rule is very simple. The one who draws a black card will get 10 points and the one who draws a red card will get 5 points.
Here is a sample algorithm explaining the above game,
This game explains the simple if statement. The if statement allows selection (decision making) depending upon the outcome of a condition. If the condition evaluates to true then the statement immediately following if will be executed and if the condition evaluates to false then the statements following the else clause will be executed. The selection statements are also called conditional statements or decision statements.
Game 2: The students will play one on one. This time the rule changes. Team one will choose a card. The team first has to check if the card is black. If it is black, then the team has to check if the number is less than 7. If it is less than 7, the team gets 10 points, else 5 points.
The above concept is called as Nested-if statement. These control structures are used to test for multiple conditions as against the simple if statement which can be used to test a single condition.
Game 3: Now, let’s make a small change in the game. This time, each student will get one card and the teacher will randomly read out the suits. If the teacher says spade, the students holding the card spade will form a group and sing a song. Similarly, a different activity has to be done by the students for different suits.
Below is a sample algorithm explaining the above game,
The switch statement allows us to test the value of an expression with a series of character or integer values. On finding a matching value, the control jumps to the statement pertaining to that value and the statement is executed, till the end of the switch is reached. The expression must either evaluate to an integer value or a character value. It cannot be a string or a real number.
Now, let’s move on to the next type of statement, which is the iterative statement. These statements are used to perform a set of instructions repeatedly while the condition is true. Iteration statements are also called looping statements.
Activity 1: loops
Ask 20 children to stand in line one behind the other. Ask the first student to say his/her favourite colour loudly and move out of the queue. Then the next student says and then this goes on till the last student. Here we can observe that the same action is repeated by different students. This repetitive action can be represented using a single block of code using the loops concept.
Below is the sample algorithm for for loop;
Here, we can start the action from any student in the queue, need not to be the first student. Similarly the ending point can also be anywhere in between the queue, and need not be the last student in the queue. Also, the increment can happen by any number.
For this example, students who are in the even number position alone will say their favourite colours.
Let’s take another example,
In this example, students in the position of multiples of three will say their favourite colours.
for loop – These statements are used to perform a set of instructions repeatedly while the condition is true. For loop is called as entry control loop because the control goes inside the body of the loop only if the condition at the entry level is true. Else the entire loop will be skipped. For loop is used when you know the number of iterations required.
The loop has four different elements that have different purposes. These elements are:
a) Initialization expression: Before entering in a loop, its variables must be initialized.
i=1 or i=2 …
b) Test expression: The test expression decides whether the loop body will be executed or not. If the test condition is true, the loop body gets executed otherwise the loop is terminated.
i<=20 or i<18 …
c) Increment/decrement expression: The increment/decrement expression changes the value of the loop variable. This expression will be executed after the execution of the body of the loop.
i++ or i+2 …
d) The body of the loop: The statements which are executed repeatedly while the test expression evaluates to true form the body of the loop.
While loop – The same activity can be designed using while loop also. The while loop is also an entry-controlled loop. The difference is only in the flow of the program. It means that the loop condition is tested before executing the loop body. If the loop condition is initially false, for the first iteration, then the loop may not execute even once. The main characteristic of the while loop is that it can be used in both cases, i.e., when the number of iterations is known as well as when it is unknown.
Below is the sample algorithm for while loop;
In this section, you learned about the selective control structures such as if-else, nested-if and switch and iterative control structure such as for and while loops.
You learned how to:
i. Build logic using if-else and switch statements.
ii. Initialize, begin and end simple loop.
iii. Control the flow of the program using control structure.
Selection and iteration statements use the values of expressions to control their execution.
The author teaches at Chinmaya International Residential School. She can be reached at email@example.com.