Control structure
Control structure
iEducat
ionSoci
ety'
s
St
.Xav
ier
s’Engli
shHi
ghSchool&Jr.Col
lege,
Manpada,
Thane(
w)
Subject:
-ComputerSci
ence–I
Topic–Dat aStr
uct
ure
Probablemarks–17
Lecture-2(
Notes)
Whati
sanAl
gor
it
hm ?
Analgorit
hm isafi
nit
esetofinst
ruct
ionsorl
ogi
c,wr
itt
eninor der
,toaccompli
shacert
ain
predef
inedtask.Al
gori
thm i
snotthecomplet
ecodeorprogram, i
tisj
ustthecorel
ogi
c(sol
uti
on)
ofaproblem,whichcanbeexpressedeit
herasani
nformalhighlevel
descri
pti
on
aspseudocodeorusingaf l
owchart.
Ev
eryAl
gor
it
hm mustsat
isf
ythef
oll
owi
ngpr
oper
ti
es:
1.I
nput
-Ther
eshoul
dbe0ormor
einput
ssuppl
i
edext
ernal
l
ytot
heal
gor
it
hm.
2.Out
put
-Ther
eshoul
dbeat
least1out
putobt
ained.
3.Def
ini
teness-Ev
eryst
epoft
heal
gor
it
hm shoul
dbecl
earandwel
ldef
ined.
4.Fi
nit
eness-Theal
gor
it
hm shoul
dhav
efi
nit
enumberofst
eps.
5.Cor
rect
ness-Ev
eryst
epoft
heal
gor
it
hm mustgener
ateacor
rectout
put
.
Anal
gori
thm i
ssai
dtobeef
fi
cientandf
ast
,ifi
ttakesl
esst
imet
oexecut
eandconsumesl
ess
memoryspace.
Whati
sanCont
rol
Str
uct
ure?
Acont r
olstructur
eisli
keablockofpr ogr
ammi ngthatanal
ysesvar
iabl
esandchoosesa
dir
ecti
oni nwhichtogobasedongi venparameters.Theter
mf l
owcontroldet
ail
sthedirect
ion
theprogram takes(whi
chwaypr ogr
am control"f
lows")
.Henceiti
sthebasicdeci
sion-
making
processincomput i
ng;I
tisapredict
ion.
OR
ControlStructuresarejustawayt ospecif
yflowofcontroli
nprograms.Anyalgor
ithm or
progr
am canbemor ecl
earandunder st
oodiftheyuseself
-cont
ainedmodulescal
ledaslogi
cor
contr
ol st
ructures.Itbasi
cal
lyanalyzesandchoosesinwhichdirecti
onaprogram f
lowsbased
oncertainparamet ersorcondi
tions.
Ther
earethreebasi ctypesoflogi
c,orf
lowofcont
rol
,knownas:
1.Sequencelogi c,orsequenti
alfl
ow
2.Select
ionlogic, orcondi
ti
onalfl
ow
3.Iter
ati
onlogic, orrepet
it
ivefl
ow
Letusseet
hem i
ndet
ail
:
1.SequentialLogic(
SequentialFl
ow)
Sequentiall
ogicasthenamesuggest sfoll
owsaser i
alorsequent
ialf
lowinwhi chthefl
ow
dependsont heseri
esofinstruct
ionsgiventothecomput er
.Unl
essnewi nst
ructi
onsare
gi
ven, t
hemodul esareexecutedintheobvioussequence.Thesequencesmaybegi v
en,by
meansofnumber edstepsexpli
cit
ly.Al
so,impli
ci
tl
yfoll
owst heor
derinwhichmodul esare
wri
tten.
“Al
gor
it
hm”
.
.
Modul
eA
Modul
eB
Modul
eC
“
Flowchar
t”
2.Sel
ecti
onLogic( Condit
ionalFlow)
Sel
ecti
onLogicsimpl yi
nvolvesanumberofcondi
ti
onsorparameter
swhichdeci
desone
outofsever
alwr i
ttenmodules.Thest
ruct
ureswhi
chusethesetypeofl
ogicar
eknown
asCondit
ionalStructur
es.
Thesestr
ucturescanbeoft hreety
pes:
Si
ngl
eAl
ter
nat
ive:
-
“Al
gor
it
hm”
I
f(condi
ti
on)t
hen:
[
Modul
eA]
[
EndofI
fst
ruct
ure]
“
Flowchar
t”
Doubl
eAl
ter
nat
ive:
-
“Al
gor
it
hm”
I
f(Condi
ti
on)
,then:
[
Modul
eA]
El
se:
[
Modul
eB]
[
Endi
fst
ruct
ure]
“
Flowchar
t”
Mul
ti
pleAl
ter
nat
ives:
-
“Al
gor
it
hm”
I
f(condi
ti
onA)
,then:
[
Modul
eA]
El
sei
f(condi
ti
onB)
,then:
[
Modul
eB]
.
.
.
.
El
sei
f(condi
ti
onN)
,then:
[
Modul
eN]
[
EndI
fst
ruct
ure]
Int
hisway
,thefl
owofthepr
ogr
am dependsonthesetofcondi
ti
onst
hatar
ewr
it
ten.Thi
s
canbemoreunderst
oodbyt
hefol
l
owingflowchar
ts:
3.It
erat
ionLogic(Repet
it
iv eFl
ow)
TheIt
erat
ionlogicemploy sal
oopwhi
chi
nvol
vesar
epeatst
atementf
oll
owedbyamodul
e
knownast hebodyofal oop.
Thet
wot
ypesofthesestr
uctur
esar
e:
Repeat
-ForStr
uctur
e
“Al
gor
it
hm”
Repeatf
ori
=At
oNbyI
:
[
Modul
e]
[
Endofl
oop]
“
Flowchar
t”
Repeat
-Whi
leSt
ruct
ure
Repeatwhi
l
e(condi
ti
on)
:
[
Modul
e]
[
Endofl
oop]