Sei sulla pagina 1di 23

Control Statements

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

T+e tic8et "are is Rs. 122


RUN 2 4
$nter t+e a.e 4 12

T+e tic8et "are is Rs. 52


Note 4 There is only one statement in the if $loc'" the $races are optional.
#ut if there is more than one statement you must use the $races.
5.2.2. i" . . . else Statement
Sometimes" we can execute one group of statements if the condition is true
and another group of statements if the condition is false" in such a situation" the
if . . . else statement can $e used.
The if . . . else statement is an extension of simple if statement. The syntax
is :
if (test condition)
{
tr!e-block-statement(s) ;
}
else
{
false-block-statement(s) ;
}
statement-x ;
!f the test condition is true" then the true%$loc'%statement6s8 are executed. !f
the test condition is false" then the false%$loc'%statement6s8 are executed. !n
either case" either true%$loc'%statement6s8 or false%$loc'%statement6s8 will $e
executed" not $oth.
test
cond
.
5.6 Control Statements
The figure $elow shows the flowchart of simple if statement :
Entry
True 1alse
To understand this programming construct" let us consider an example of
chec'ing whether the person is eligi$le to vote or not $y getting the age of the
person. The tas' include :
=etting the input 6age8 from the user.
?a'ing the decision if the age is greater than or equal to 1@" then print
the person is eligi$le to vote.
Else print the person is not eligi$le to vote.
The following program demonstrates the a$ove pro$lem :
&' Demonstration o" i" . . . else statement '&
( inclu)e *st)io.+,
( inclu)e *conio.+,
voi) main()
-
int a.e 0
clrscr() 0
!rint"(3$nter t+e a.e 4 3) 0
scan"(35)3/ 6a.e) 0
i"(a.e ,1 1:)
!rint"(37nT+e !erson is eli.i;le to vote.3) 0
else
false%$loc'%
statement6s8
statement%x
true%$loc'%
statement6s8
Control Statements 5.7
!rint"(37nT+e !erson is not eli.i;le to vote.3) 0
.etc+() 0
9
RUN 1 4
$nter t+e a.e 4 25
T+e !erson is eli.i;le to vote.
RUN 2 4
$nter t+e a.e 4 1<
T+e !erson is not eli.i;le to vote.
5.2.3. Neste) i" . . . else Statement
/e can write an entire if . . . else construct within either the $ody of an if
statement or the $ody of an else statement. This is called nesting of ifs. The
syntax is :
if (test condition #)
{
if (test condition $)
{
statement-# ;
}
else
{
statement-$ ;
}
}
else
{
statement-% ;
}
statement-x ;
!f the test condition 1 is false" the statement% will $e executed. !f the test
condition 1 is true" it continues to perform the test condition 2. !f the test
condition 2 it is true" the statement%1 will $e executes. !f the test condition 2 is
false" the statement%2 will $e executed and then the control is transferred to the
statement%x.
test
cond%
1
.
test
cond%
2
.
5.8 Control Statements
The figure $elow shows the flowchart of nested if . . . else statement :
Entry
1alse True
1alse True
+onsider the pro$lem for finding the $iggest num$er among three num$ers.
The pro$lem involves decision" that is" the tas's include :
=etting the three num$ers.
?a'ing the decision if the first num$er is greater than the second" if it is
true then chec' the first num$er is greater than the third" if it is true" then
print the first num$er is greater otherwise print the third num$er is
greater.
Else if first num$er is not greater than the second" then chec' the third
num$er is greater than the second" if it true" then print the third num$er
is greater otherwise print the second num$er is greater.
The following program demonstrates the a$ove pro$lem :
statement%x
statement% statement%2 statement%1
Control Statements 5.9
&' Demonstration o" Neste) i" . . . else statement '&
( inclu)e *st)io.+,
( inclu)e *conio.+,
voi) main()
-
int n1/ n2/ n3 0
clrscr() 0
!rint"(3$nter t+ree num;ers 4 3) 0
scan"(35) 5) 5)3/ 6n1/ 6n2/ 6n3) 0
i"(n1 , n2)
-
i"(n1 , n3)
!rint"(37n5) is .reater3/ n1) 0
else
!rint"(37n5) is .reater3/ n3) 0
9
else
-
i"(n3 , n2)
!rint"(37n5) is .reater3/ n3) 0
else
!rint"(37n5) is .reater3/ n2) 0
9
.etc+() 0
9
RUN 1 4
$nter t+ree num;ers 4 22 32 12
32 is .reater
RUN 2 4
$nter t+ree num;ers 4 32 22 12
32 is .reater
RUN 3 4
$nter t+ree num;ers 4 12 22 32
5.10 Control Statements
32 is .reater
5.2.=. else i" a))er
=enerally" the case where the statements in the if part of an if . . . else
statement is another if statement tends to $e confusing and $est avoided.
Aowever an extremely useful construction occurs when the else part of if
statement contains another if . . . else statement. This construction is called as an
else if ladder or chain. The syntax is :
if (test condition #)
{
statement-# ;
}
else if (test condition $)
{
statement-$ ;
}
. . .
. . .
. . .
else if (test condition n)
{
statement-n ;
}
else
{
defa!lt-statement ;
}
statement-x ;
Each condition is evaluated in order and if any condition is true" the
corresponding statement is executed and the remainder of the chain is terminated
and the control is transferred to the statement%x. The final else statement
containing the default statement will $e executed if none of the previous n
conditions are not satisfied.
The figure $elow shows the flowchart of else if ladder statement :
test
cond%
1
.
test
cond%
2
.
test
cond%
n
.
Control Statements 5.11
Entry
True 1alse

True 1alse
True 1alse
default
statement
statement%
statement%2
statement%1
statement%x
5.12 Control Statements
+onsider the following program to find the grade of an exam $y reading the
mar' scored in it :
&' Demonstration o" else i" la))er statement '&
( inclu)e *st)io.+,
( inclu)e *conio.+,
voi) main()
-
int mar8 0
clrscr() 0
!rint"(3$nter t+e mar8 score) 4 3) 0
scan"(35)3/ 6mar8) 0
i"(mar8 ,1 :2)
!rint"(37n>asse) in )istinction.3) 0
else i"(mar8 ,1 <2)
!rint"(37n>asse) in "irst class3) 0
else i"(mar8 ,1 52)
!rint"(37n>asse) in secon) class3) 0
else i"(mar8 ,1 =2)
!rint"(37n>asse) in t+ir) class3) 0
else
!rint"(37n?aile) in t+e e@am.3) 0
.etc+() 0
9
RUN 1 4
$nter t+e mar8 score) 4 <<
>asse) in "irst class
RUN 2 4
$nter t+e mar8 score) 4 35
?aile) in t+e e@am.
5.3. sAitc+ ST#T$%$NT
The control statement which allows us to ma'e a decision from the num$er of
choices is called a switch" or more correctly a switch . . . case . . . default" since
these three 'eywords go together to ma'e up the control statement.
Control Statements 5.13
The switch statement tests the value of a given varia$le 6or expression8
against a list of case values and when a match is found" a $loc' of statements
associated with the case is executed. The syntax is :
s&itc' (expression)
{
case (al!e-# )
statement-# ;
break ;
case (al!e-$ )
statement-$ ;
break ;
. . .
. . .
. . .
defa!lt )
defa!lt-statement ;
}
statement-x ;
/here the expression is an integer expression or characters. value%1" value%2
are constants or constant expression 6valua$le to an integral constant8 and are
'nown as case la$els. Each of these values should $e should $e unique with a
switch statement. statement%1" statement%2 are statement lists and may contain
one or more statements. There is no need to put $races around these $loc's. The
'eyword case is followed $y an integer or a character constant. Each constant in
each case must $e different from all others and the case la$els end with a
semicolon 6:8.
/hen the switch is executed" the value is computed for expression" the
list of possi$le constant expression values determined from all case statements is
searched for a match. !f a match is found" execution continues after the matching
case statement and continues until a $rea' statement is encountered or the end
of statement is reached.
The $rea' statement at the end of each $loc' signals the end of a particular
case and causes an exit from the switch statement" transferring the control to the
statement%x following the switch. The default is an optional case. !f a match is not
found and the default statement prefix is found within switch" execution continues
at this point. 9therwise" switch is s'ipped entirely and the control goes to the
statement%x.
switc
h
expr
.
5.14 Control Statements
Note 4 &t the end of every case" there should $e a $rea' statement.
9therwise" it will result in causing the program execution to continue into the next
case whenever case gets executed.
The figure $elow shows the flowchart of switch statement :
Entry
Expression value 2 1
Expression value 2 2
. . .
. . .
-efault 6no match8 . . .
statement%1
statement%2
default statement
statement%x
Control Statements 5.15
+onsider the following program to read a num$er from 1 to 1< and print the
corresponding num$er in words :
&' Demonstration o" sAitc+ ... case ... )e"ault statement '&
( inclu)e *st)io.+,
( inclu)e *conio.+,
voi) main()
-
int num 0
clrscr() 0
!rint"(3$nter a num;er *1 to 12, 4 3) 0
scan"(35)3/ 6num) 0
sAitc+(num)
-
case 1 4
!rint"(37nON$3) 0
;rea8 0
case 2 4
!rint"(37nTBO3) 0
;rea8 0
case 3 4
!rint"(37nTCR$$3) 0
;rea8 0
case = 4
!rint"(37n?OUR3) 0
;rea8 0
case 5 4
!rint"(37n?ID$3) 0
;rea8 0
case < 4
!rint"(37nSIE3) 0
;rea8 0
case F 4
!rint"(37nS$D$N3) 0
;rea8 0
case : 4
!rint"(37n$IGCT3) 0
;rea8 0
case H 4
!rint"(37nNIN$3) 0
;rea8 0
case 12 4
!rint"(37nT$N3) 0
5.16 Control Statements
;rea8 0
)e"ault 4
!rint"(37nInvali) In!ut.3) 0
9
.etc+() 0
9
RUN 1 4
$nter a num;er *1 to 12, 4 5
?ID$
RUN 2 4
$nter a num;er *1 to 12, 4 12
Invali) In!ut.
5.=. .oto ST#T$%$NT
The goto statement is used to alter the normal sequence of program
execution $y unconditionally transferring control to some part of the program. The
syntax is :
goto label )
/here la$el is an identifier used to la$el the target statement to which the
control would $e transferred. +ontrol may $e transferred to any other statement
within the current function. The target function must $e la$eled followed $y a
colon. The syntax is :
label ) statement ;
+onsider the following program for finding winning team in a cric'et match :
&' Demonstration o" .oto statement '&
( inclu)e *st)io.+,
( inclu)e *conio.+,
voi) main()
-
int in)/ !a8 0
clrscr() 0
!rint"(3$nter t+e In)iaIs score 4 3) 0
scan"(35)3/ 6in)) 0
Control Statements 5.17
!rint"(37n$nter t+e >a8istanIs score 4 3) 0
scan"(35)3/ 6!a8) 0
i"(in) , !a8)
.oto in)ia 0
else
.oto !a8istan 0
in)ia 4
!rint"(37nIn)ia Ains t+e matc+3) 0
.oto en) 0
!a8istan 4
!rint"(37n>a8istan Ains t+e matc+3) 0
en) 4
.etc+() 0
9
RUN 1 4
$nter t+e In)iaIs score 4 315
$nter t+e >a8istanIs score 4 252
In)ia Ains t+e matc+
5.5. A+ile ST#T$%$NT
The while statement is a general repetition statement that can $e used in a
num$er of programming environments. !t is an entry controlled loop statement"
since the test condition is tested $efore the start of the loop execution. This loops
statement is used when the num$er of passes are not 'nown in advance. The
syntax is :
&'ile(test condition)
{
bod* of t'e loop ;
}
statement-x ;
/here the $ody of the loop may have one or more statements. The $races
are need only if the $ody contains two or more statements. The test condition is
evaluated and if the condition is true" then the $ody of the loop is executed. &fter
execution of the $ody" the test condition is once again evaluated and if it is true"
the $ody of the loop is executed once again. This process of repeated execution of
the $ody of the loop continues until the test condition $ecomes false and the
test
cond
.
5.18 Control Statements
control is transferred out of the loop. 9n exit" the program continues with the
statement%x which is immediately after the $ody of the loop.
Note 4 The test condition specified in the while loop should eventually
$ecome false at one point of the program" otherwise the loop will $ecome an
infinite loop.
The figure $elow shows the flowchart of while statement :
Entry
1alse
True
$ody of the loop
statement%x
Control Statements 5.19
+onsider the following program to find the sum of first n num$ers :
&' Demonstration o" A+ile statement '&
( inclu)e *st)io.+,
( inclu)e *conio.+,
voi) main()
-
int n/ count 1 1/ sum 1 2 0
clrscr() 0
!rint"(3$nter t+e value "or n 4 3) 0
scan"(35)3/ 6n) 0
A+ile(count *1 n)
-
sum 1 sum J count 0
count 1 count J 1 0
9
!rint"(37nT+e sum o" "irst 5) num;ers is 4 5)3/ n/ sum) 0
.etc+() 0
9
RUN 1 4
$nter t+e value "or n 4 5
T+e sum o" "irst 5 num;ers is 4 15
5.<. )o . . . A+ile ST#T$%$NT
There is a minor difference $etween the wor'ing of while and do . . . while
loops. This difference is the place where the condition is tested. The while test
condition $efore executing any of the statements within the while loop. The
do . . . while tests the condition after having executed the statements within the
loop. This means that do . . . while would execute its statements at lease once"
even if the condition fails for the first time itself. The while" on the other hand will
not execute its statement if the condition fails for the first time.
do . . . while is an exit controlled loop statement" since the test condition is
performed at the end of the $ody of the loop and therefore the $ody of the loop is
executed unconditionally for the first time. The syntax is :
test
cond
.
5.20 Control Statements
do
{
bod* of t'e loop ;
} &'ile(test condition) ;
statement-x ;
/here the $ody of the loop may have one or more statements. 9n reaching
the do statement" the program proceeds to evaluate the $ody of the loop first. &t
the end of the loop" the test condition in the while statement is evaluated. !f the
condition is true" then the program continues to evaluate the $ody of the loop
once again. This process continues as long as the condition is true. /hen the
condition $ecomes false" the loop will $e terminated and the control goes to the
statement%x that appears immediately after the while statement.
The figure $elow shows the flowchart of do . . . while statement :
Entry
True
1alse
statement%x
$ody of the loop
Control Statements 5.21
+onsider the following program to find the sum of two num$ers for n num$er
of times :
&' Demonstration o" )o . . . A+ile statement '&
( inclu)e *st)io.+,
( inclu)e *conio.+,
voi) main()
-
int num1/ num2 0
c+ar c+oice 0
clrscr() 0
)o
-
!rint"(37n$nter t+e "irst num;er 4 3) 0
scan"(35)3/ 6num1) 0
!rint"(37n$nter t+e secon) num;er 4 3) 0
scan"(35)3/ 6num2) 0
!rint"(37n5) J 5) 1 5)3/ num1/ num2/ num1 J num2) 0
""lus+(st)in) 0
!rint"(37n7nDo Kou Aant to continue (L&N) 4 3) 0
scan"(35c3/ 6c+oice) 0
9 A+ile((c+oice 11 ILI) MM (c+oice 11 IKI)) 0
.etc+() 0
9
RUN 1 4
$nter t+e "irst num;er 4 2
$nter t+e secon) num;er 4 5
2 J 5 1 F
Do Kou Aant to continue (L&N) 4 K
$nter t+e "irst num;er 4 12
$nter t+e secon) num;er 4 22
12 J 22 1 32
Do Kou Aant to continue (L&N) 4 n
5.22 Control Statements
5.F. "or ST#T$%$NT
The for loop is another entry controlled loop that provides a more concise
loop control structure. The syntax is :
for(exp# ; exp$ ; exp%)
{
bod* of t'e loop ;
}
#efore the first iteration" 4expr15 is evaluated. This is usually used to
initiali,e varia$les for the loop that is used to set the loop control varia$le. The
4expr25 is a relational expression that determines when the loop should
terminate. &fter each iteration of the loop" 4expr5 is evaluated. This is usually
used to increment or decrement the loop counters. The $ody of the loop is
executed repeatedly till the condition in 4expr25 is satisfied. The expressions
must $e separated $y a semicolon. &ll the expressions are optional. !f 4expr25 is
left out" it is assumed to $e 1 6i.e. True8.
+onsider the pro$lem for printing the num$ers from 1 to n.
The following program demonstrates the a$ove pro$lem :
&' Demonstration o" "or statement '&
( inclu)e *st)io.+,
( inclu)e *conio.+,
voi) main()
-
int i/ n 0
clrscr() 0
!rint"(3$nter t+e value "or n 4 3) 0
scan"(35)3/ 6n) 0
!rint"(37nT+e num;ers are 4 7n7n3) 0
"or(i 1 1 0 i *1 n 0 iJJ)
!rint"(35) 7t3/ i) 0
.etc+() 0
9
RUN 1 4
$nter t+e value "or n 4 F
T+e num;ers are 4
Control Statements 5.23
1 2 3 = 5 < F

Potrebbero piacerti anche