SEARCH THIS BLOG

FLOW OF CONTROL IN C/C++

Programs not only store data but also manipulate data in terms of consolidation, rearranging, modifying data. To perform all these tasks, programs need tools for performing repetitive actions or for making decisions.

 

FLOW OF CONTROL

 

As we know, a C/C++ program starts its execution from the first statement of main() function and continues till the end. But what if we need to send control back a few statements or if we want to repeat a sequence of statements a given number of times? Most of the problems in real life has two outcomes, either a yes (i.e. true) or a no (i.e. false). How to let our program take a decision?

Well all these problems are solved using special tools which are called the flow of control manipulators.

But before we learn about them, we must learn what is a statement and what is a block of statements.

Statements

Statements are the instructions given to the computer to perform any kind of action, be it data movements, be it making decisions or be it repeating actions. Statements are the smallest executable unit of a C/C++ program. Statements are terminated with a semicolon (;). The simplest statement is the empty or NULL statement. It is simply a semicolon (;). A NULL statement is useful in those instances where the syntax of the language requires the presence of a statement but the logic of the program does not (As in loops and their bodies).

A compound statement in C++ is a sequence of statements enclosed by a pair of curly brackets ({}). For instance,

{

statement 1;

statement 2;

statement 3; // & so on

}

A compound statement is called a block. A block is regarded as a single statement and can be used anywhere in a program where a single statement can be used.

Now we will see in brief, all the tools in C++ to control the normal flow of execution.

 

The if statement of C/C++

 

An if statement is used to test a particular condition; if that condition is true then only the control will move to the statement following if, otherwise that statement is ignored (Here the statement following if may be a single or a compound statement).

Obviously, any condition can yield two possible results, either true or false. If the condition in if part is false then the statement in the else clause gets executed. The following figure shows a flow chart on how if-else statements work.

 

FLOW OF CONTROL

It is important to know that C/C++ doesn’t make it compulsory to use an else part with if. The rule is simple. If the condition is true, the immediately following statement of if gets executed, otherwise the control simply moves to the next statement. This next statement may be an else or it may be a normal statement.

Also, if can be used without else but the converse is not true. Therefore, there cannot be an else statement without an if. The following codes will make the concepts clear :

 

if(condition)

statement 1;

else

statement 2;

Here, if the condition is true, statement 1 is executed; otherwise statement 2 is executed.

 

if(condition)

statement 1;

statement 2;

Here, if the condition is true, statement 1 is executed and then the control moves to statement 2; otherwise statement 1 is ignored and control directly passes to statement 2.

 

if(condition)

statement 1;

statement 2;

else

statement 3;

This gives an error. The control on executing 2 has already come out of if clause. When it encounters else, it cannot figure out the if part of this else clause and so declares an error.

 

Note :

In if-else statements, either only the if statement is executed or only the else statement but never both.

In all the above examples, statement 1,2 & 3 could be a single statement or a compound statement or even a NULL statement.

 

The switch statement of C++

 

C++ provides a multiple-branch selection statement known as switch. This selection statement successively tests the value of an expression against a list of integer or character constants. When match is found, the statements associated with that constant are executed.

The syntax for switch case is :

switch (exp)

{

case constant 1 : statement sequence 1; break;

case constant 2 : statement sequence 2; break;

case constant 3 : statement sequence 3; break;

……….

case constant n-1 : statement sequence n-1; break;

default : statement sequence n;

}

where exp is any expression or a variable (only int or char).

The expression is evaluated and the result is compared sequentially from top to bottom, starting at case constant 1. The moment the match is found, all the statement sequences of that case are executed.

Now the importance of break comes into play. Suppose the exp value matches constant 2 and there is no break statement in that case. Then the control keeps on executing all the statement sequences of the following cases until it encounters a break.

The default case is executed in case none of the constants match exp.

Let us see a few examples :

int n; cin>>n;

switch(n)

{

case 1 : cout<< “ONE ”; break;

case 2 : cout<< “TWO”; break;

case 3 : cout<< “THREE”; break;

case 4 : cout<< “FOUR”; break;

default : cout<< “GREATER THAN FOUR”;

}

 

Input : 2

Output : TWO

Input : 3

Output : THREE

Input : 6

Output : GREATER THAN FOUR

 

Now let us see what happens if we omit break statement.

 

int n; cin>>n;

switch(n)

{

case 1 : cout<< “ONE ”;

case 2 : cout<< “TWO ”; break;

case 3 : cout<< “THREE ”;

case 4 : cout<< “FOUR ”;

default : cout<< “GREATER THAN FOUR”;

}

 

Input : 1

Output : ONE TWO

Input : 2

Output : TWO

Input : 3

Output : 

THREE FOUR GREATER THAN FOUR

 

Other flow of control statements in C/C++ are the loops. C/C++ provides 3 looping structures to execute the same set of statements any desired number of times. These are :

for loop

while loop

do while loop

Click on them to learn in details about them. (Sorry the link will be restored shortly.)

 

 

 

 

 

 

Did you find the article helpful? Do you want to add something to it? Feel free to comment on this post below.

Do not stop here. To read all about C/C++ click here.

To download pdf format of this article, click here.

If you want to contribute any article or any piece of information you can send it on coding.nkcoder@gmail.com. We will publish it in your name.

 


Post a Comment