Professional Documents
Culture Documents
5.1. INTRODUCTION
The term flow of control refers to the order in which a programs statements
are executed. Every programming language supports three control flow structures
namely :
1. Sequential control structure
2. Selective control structure
. !terative control structure
5.1.1. Sequential Control Structure
The normal flow of control of all programs is sequential. !n sequential
structure" a sequence of programs statements are executed one after another in
the order in which they are placed. #oth selection and repetition statements
allows allow the programmer to alter the normal sequential flow of control.
Sequential programming can also $e called linear programming. The
sequential programs are non%modular in nature. That is" reusa$ility of code is not
possi$le. Thus" they are difficult to maintain and understand. Examples of
sequence control structure statements are" the program will have statements that
are placed sequentially and there is no decision involved in the process. &lso" the
program does not require a specific tas' to $e repeated over and over again.
5.1.2. Selective Control Structure (or) Decision Control Structure
The selective structure allows the usual sequential order of execution to $e
modified. !t consists of a test for a condition followed $y alternative paths that the
program can follow. The program selects one of the alternative paths depending
upon the result of the test for condition. Examples of selective control structures
statements are :
1. Simple if statement
2. if . . . else statement
. (ested if . . . else statement
). else if ladder
*. switch . . . case . . .default statement
5.2 Control Statements
5.1.3. Iterative Control Structure (or) oo! Control Structure
The iterative structure provides the a$ility to go $ac' and repeat a set of
statements. !terative structure is otherwise referred to as repetitive structure.
The + language has the a$ility to repeat the same calculation or sequence of
instructions over and over again" each time using different data. The iterative
structure consists of an entry point that includes initiali,ation of varia$le" a loop
continuation condition" a loop $ody and an exit point. Examples of iterative
control structure statements are :
1. while statement
2. do . . . while statement
. for statement
5.2. i" ST#T$%$NTS
+ allows decisions to $e made $y evaluating a given expression as true or
false. Such an expression involves the relational and logical operators. -epending
on the outcome of the decision" program execution proceeds in one direction or
another. The + statement that ena$les these tests to $e made is called the if
statements.
The if statements may $e implemented in different forms depending on the
complexity of conditions to $e tested. They are :
1. Simple if statement
2. if . . . else statement
. (ested if . . . else statement
). else if ladder
5.2.1. Sim!le i" Statement
The simple if statement is used to specify conditional execution of program
statement or a group of statements enclosed in $races. The syntax is :
if (test condition)
{
statement-block ;
}
statement-x ;
test
cond
.
Control Statements 5.3
/hen an if statement is encountered" test condition is evaluated first and if it
is true" the statement%$loc' will $e executed. !f the test condition is false" the
statement%$loc' will $e s'ipped and the execution will 0ump to the statement%x.
/hen the test condition is true" $oth the statement%$loc' and the statement%
x are executed in sequence. The test condition is always enclosed within a pair of
parenthesis. The statement%$loc' may $e a single statement or a group of
statements.
The figure $elow shows the flowchart of simple if statement :
Entry
True
1alse
Ta$le $elow shows the various expressions that are used as conditions inside
an if statement :
Conditional
Expression
Meaning For e.g.,
al!e of a
For e.g.,
al!e of b
"es!lt
a 22 $ a is equal to $ *
*
*
True
1alse
a 32 $ a is not equal to $ *
*
*
True
1alse
a 4 $ a is less than $
*
*
True
1alse
a 5 $ a is greater than $ *
*
True
1alse
a 42 $ a is less than or
equal to $
*
*
True
True
1alse
a 52 $ a is greater than * True
statement%$loc'
statement%x
5.4 Control Statements
or equal to $ *
*
*
True
1alse
Two or more conditions may $e com$ined in an if statement using a logical
&(- operator 6778 or a logical 9: operator 6;;8. !t can compare any num$er of
varia$les in a single if statement. Ta$le $elow shows the various expressions that
are used as conditions inside an if statement :
Conditional
Expression
Meaning
For
e.g.,
al!e
of a
For
e.g.,
al!
e of
b
For
e.g.,
al!
e of c
"es!lt
66a5$8 77 6$5c88
a is greater than
$ &(- $ is
greater than c
< 2< 1< True
< 1< 2< 1alse
1< < 2< 1alse
1< 2< < 1alse
66a5$8 ;; 6$5c8
a is greater than
$ 9: $ is greater
than c
< 2< 1< True
< 1< 2< True
1< < 2< True
1< 2< < 1alse
+onsider the pro$lem for finding the tic'et fare for adult and children. The
pro$lem involves decision" that is" the tas's include :
&ssuming that the tic'et fare as 1<< 6for adult8" at the first stage.
=etting the input 6the age8.
!f the age entered is less than or equal to 12" then the tic'et fare should
calculated as half of the original fare 6here 1<<>2 2 *<8.
-isplaying the fare.
The following program demonstrates the a$ove pro$lem :
&' Demonstration o" sim!le i" statement '&
( inclu)e *st)io.+,
( inclu)e *conio.+,
voi) main()
-
int a.e/ "are 0
clrscr() 0
"are 1 122 0
!rint"(3$nter t+e a.e 4 3) 0
scan"(35)3/ 6a.e) 0
i"(a.e *1 12)
"are 1 "are & 2 0
!rint"(37nT+e tic8et "are is Rs. 5)3/ "are) 0
Control Statements 5.5
.etc+() 0
9
RUN 1 4
$nter t+e a.e 4 25