What is a Control Statement?

 

All programs can be broken down into 3 controls:

  1. Sequence – handled automatically by compiler.

  2. Selection – if, if/else or switch.

  3. Repetition – while, do/while or for.

The flow of control

 

When a programmer is crafting a program, it is good practice to break the program down into pieces that can be thought of independently. Once the program has been completed, we can think of its execution as being a series of these pieces that work together in a certain sequence. These pieces then pass the control of the program between each other. While one piece has the control, the other pieces are inactive. This is known as the flow of control in a program. If our program had three parts, called Start, Middle, and End, the flow of control could look like:

 

[flow of control diagram]

 

Control statements

 

Control Statements, then, are ways for a programmer to control what pieces of the program are to be executed at certain times.By default, statements are executed in sequence, one after another. We can, however, modify that sequence by using control flow constructs which arrange that a statement or group of statements is executed only if some condition is true or false, or executed over and over again to form a loop.The syntax of Control statements are very similar to regular English, and are very similar to choices that we make every day. There are two basic types of control statements: branching statements and loops.

 

(1) Branching statements

 

We will first look at branching statements. Let's say Yaser is shopping at a mall and he finds a CD that he wants to buy. Yaser then checks his pocket to see if he has enough money to pay for the CD. When he pulls his money out of his pocket Yaser may be thinking: "if I have more money than the price of the CD then I will buy the CD." In pseudocode that thought could be translated into:

if (my_money > cost_of_CD) then
	buy_CD
else
	get_a_job
end if;

Note that the pseudocode statement end if means "end the previous if statement." This is to make it clear what statements are inside the if statement and what statements are outside of the if statement.

 

Depending on a certain condition a certain series of events will be executed. Another type of branching statement is called a switch statement. A switch statement is just a shorter way of writing a lot of if statements. Switch statements will be explained in more detail in the next subsection.

 

Nesting control statements

 

In the preceding situation, if Yaser doesn't have enough money, before going out to get a job, he could look for a friend to borrow the money from. Now the pseudocode for this could be:

if (my_money > cost_of_CD) then
	buy_CD
else
	if (see_a_friend) then
		borrow_money
	else
		get_a_job
	end if;
end if;

Now there is one control statement that is inside of another control statement. This is known as nesting.

 

Loops

 

Let's pretend now that Yaser was buying a house instead of a CD. If Yaser wanted to buy the house without taking a loan from the bank, he would have to wait until he had enough money to buy the house. The pseudocode for this could be:

if (my_money > cost_of_house) then
	buy_house
end if;

But this means that Yaser would only check once if he had enough money to buy the house. What we want to describe is the fact that Yaser needs to keep waiting until he has enough money to buy the house.

while (my_money < cost_of_house) 
	work_more
end while;
buy_house;

This is a loop statement. Another loop statement is the for command. Let's say Yaser wanted to add up how much money he would make this year. Let's say Yaser is paid $1000 each month. The pseudocode to figure this out could be:

int total=0;
for x = 1 to 12
	total = total + 1000
next x;
output total;

A for statement execute a specified number of times. In this instance it executes 12 times . In this example, it would actually be easier to write this code as:

total = 12 * 1000;
output total;

But, what if Yaser earns interest on any money that he saves? Now a for statement will be a handy tool. Let's decide that Yaser spends $400 a month on rent, $75 a month on food, and $100 a month on other expenses. Let's also assume that Yaser earns 2% per month on any money that he saves. Now our pseudocode could look like:

int monthly_expenses= 400 + 75 + 100;
int monthly_income = 1000;
float interest_rate = .02

// compute the amount Yaser will have saved after one year
int total = 0;
int interest_earned =0;
for x = 1 to 12
	interest_earned = total * interest_rate;
	total = total + interest_earned + monthly_income - monthly_expenses
next x;

// display the value
 output  total;

Control statements allow a programmer to craft a program so that certain parts of code execute multiple times, or not at all based on the current state of the program. Control statements are the most basic form of logical control within a program.