Elementary Programming Principles
Elementary Programming Principles
NGPRI
NCI
PLES
Def
ini
ti
onoft
erms
ComputerProgr
am:
Acomputerpr
ogr
am i
sasetofcodedi
nst
ruct
ionsgi
vent
othecomput
er,
and
repr
esentsalogi
calsol
uti
ontoaprobl
em.Itdi
rect
sacomput
eri
nper
for
mingv
ari
ous
operat
ions/
tasksonthedatasuppl
i
edtoi
t.
Computerpr
ogr
amsmaybewr it
tenbyt
hehardwar
emanufact
urer
s,Sof
twar
ehouses,
orapr
ogrammertosol
veuserprobl
emsonthecomput
er.
Pr
ogramming:
Pr
ogr
ammingist
hepr
ocessofdesi
gni
ngasetofi
nst
ruct
ions(
comput
erpr
ogr
ams)
whichcanbeusedt operfor
m apar ti
culartaskorsol veaspecificproblem.
I
tinvolvesuseofspeci alcharacter
s,signsandsy mbolsfoundi napar t
icul
ar
programmi nglanguaget ocreatecomput erinstr
uctions.
Thepr ogrammi ngprocessisquiteex t
ensi ve.Itincl
udesanaly zingofanappl i
cati
on,
designingofasol ut
ion,codi
ngf orthepr ocessor, t
estingtoproduceanoper at
ing
program, anddevelopmentofot herprocedur estomaket hesy stem functi
on.
Theprogr
am creat
edmustspeci
fyi
ndetai
lthel
ogi
cal
stepst
obet aken&themethod
ofpr
ocessingt
hedatai
nputint
othecomputeri
nor
dertocar
ryoutthespeci
fi
edt
ask.
Acomput erprogr
am perf
ormst hefoll
owing:
1.Accept sdataf
rom outsi
dethecomput erasit
sinput.
2.Carriesoutasetofprocessesont hedatawit
hinthecomput
ermemor
y.
3.Presentstheresul
tsofthi
spr ocessi
ngasitsoutput
,and
4.Storesthedataforfut
ureuse.
Pr
ogr
ammi
ngLanguages:
Apr ogr
ammi ngl
anguageisasetofsy mbols(alanguage)whi
chacomputer
programmerusestosolveagivenproblem usi
ngacomput er.
Thecomput ermustbeabletotransl
atetheseinst
ruct
ionsi
ntomachi
ne-
readabl
efor
m
whenar r
angedinaparti
cul
arsequenceoror der.
TERMSUSEDI
NCOMPUTERPROGRAMMI
NG
Sour
cepr ogr
am (
sourcecode)
Thet
erm Sour
cepr
ogr
am ref
erst
opr
ogr
am st
atement
sthatt
hepr
ogr
ammerent
ersi
n
theprogram edi
torwi
ndow,andwhichhav
enoty
etbeentr
anslat
edintomachi
ne-
readablefor
m.
Sourcecodei st
hecodeunderst
oodbythepr
ogr
ammer,andisusuall
ywri
tt
eninhi
gh-
l
ev ell
anguageorAssemblyl
anguage.
Objectcode(objectprogram).
Theterm Obj
ectcoderef
erstot
hepr
ogr
am codet
hati
sinmachi
ne-
readabl
e(bi
nar
y)
for
m.
Thi
sisthecode/l
anguagethecomputercanunder
stand,
andisproducedbyaCompiler
orAssembl
eraftert
ransl
ati
ngtheSourceprogr
am i
ntoafor
mt hatcanbereadi
l
yloaded
i
ntothecomputer.
-1-
LANGUAGETRANSLATORS
Acomput eruses&st or
esinf or
mat i
oninbi naryf or
m, andt heref
or e,i
tcannot
under st
andpr ogramswr itt
eni neit
herhigh-levelorlow-levellanguages.Thi smeans
that,anyprogram codewr i
tteninAssembl yl anguageorhi gh-levellanguagemustbe
translat
edi nt
oMachi nelanguage, beforethecomput ercanr ecogni ze&r unthese
programs.
ATr anslatorisspecialsystem softwareusedt oconverttheSour cecodes( program
statementswr it
teninanyoft hecomput erpr ogrammi nglanguages)t ot rObj
hei ect
codes( comput erlanguageequi val
ents).
TheTr anslator
sr esideinthemai nmemor yofthecomput er
,anduset heprogram code
ofthehigh- l
evelorAssembl ylanguageasinputdata,changesthecodes, andgiv
est he
outputprogram inmachi ne-r
eadablecode.
I
naddi ti
on, t
ranslatorscheckf or&identi
fysomet ypesoferror
s( e.
g.,
Syntax/grammat icalerrors)thatmaybepr esentintheprogram bei
ngt r
anslat
ed.They
wil
lproduceer rormessagesi fthereisami st
akeinthecode.
Eachl
anguageneedsi
tsownt
ransl
ator
.Gener
all
y,t
her
ear
e3t
ypesofl
anguage
tr
ansl
ator
s:
1.Assembler.
2.Int
erpr
eter
.
3.Compiler
.
Note.I
nter
pret
ers&Compil
erstr
anslat
esour
ceprogr
amswr
it
teni
nhi
gh-
lev
el
l
anguagestothei
rmachi
nelanguageequi
val
ent
s.
Assembler
Anassembl
ert
ransl
atespr
ogr
amswr
it
teni
nAssembl
ylanguagei
ntomachi
nel
anguage
t
hatt
hecomput
ercanunder
standandex
ecut
e.
Funct
ionsofanAssembl
er.
1)
.Itcheckswhet
hert
hei
nst
ruct
ionswr
it
tenar
eval
i
d,andi
dent
if
iesanyer
ror
sint
he
pr
ogram.
TheAssembl erwi l
ldisplayt
heseerrorsaswel lasthecompl etesour ceandobj ect
programs.Ifthepr ogram hasnoerrors,t
hej obcont r
ol wi
l
lletitruni mmedi ately
,
orsav etheobj
ectpr ogram sothati
tmayr unitl
aterwi t
houttranslatingitagain.
2)
.Itassi gnsmemor ylocationstothenamest heprogrammeruses.
E.
g. ,
theAssembl erkeepsat ableofthesenamessot hati
fani nstructi
onr efersto
i
t,theAssembl ercaneasi l
ytel
lthelocati
ont owhichitwasassi gned.
3)
.Itgener atesthemachinecodeequi val
entoftheAssembl
yinst
ruct
ions.
Usuall
y, t
heAssembl ergeneratesamachinecodeonlywhennoerrorsar
edet
ect
ed.
Someoft heerrorsi
nclude;
-Ty pingmi stakes.
-Usi ngt hewr ongf
ormatf oraninstr
uct
ion.
-Speci fyingamemor yl ocat
ionoutsi
detherange0–2047.
Note.TheAssembl ercannotdetectLogicerror
s.Theprogrammerknowsof
theseerror
sonlywhent hepr
ogram isrun&t heresul
tspr
oducedar eincorr
ect(
not
whatt hepr
ogrammerexpected).Thepr ogrammermustt herefore,
gothrought
he
program &tryt
odiscoverwhyani ncor
rectresul
twasbeingpr oduced.
I
nter
pret
er
-2-
Aninterpretertr
anslatesasourceprogram wor dbywor dorli
nebyli
ne.Thisal l
owst he
CPUt oexecut eonel i
neatat i
me.
TheInterpretertakesonelineofthesour ceprogram,tr
anslat
esiti
ntoamachi ne
i
nstructi
on, andtheni ti
simmediatel
yexecut edbyt heCPU.Itthentakest
henext
i
nstructi
on, tr
anslatesiti
ntoamachinei nstr
uction,
andt hent
heCPUexecut esit,andso
on.
Thetransl
atedl
inei
snotstor
edinthecomput
ermemor y.Ther
efor
e,ev
eryt
imet
he
progr
am isneededf
orexecut
ion,
ithastobet
ransl
ated.
Compil
er
Acompi
lert
ransl
atest
heent
ir
e/whol
esour
cepr
ogr
am i
ntoobj
ectcodeatonce,
and
thenexecutesi
tinmachinelanguagecode.Thesemachi necodeinstruct
ionscanthen
berunont hecomputertoperfor
mt heparti
cul
art
askasspeci fi
edinthehigh-l
evel
progr
am.
Theprocessoftransl
ati
ngapr ogr
am wr i
tt
eninahigh-
levelsour
celanguageinto
machinelanguageusi
ngacompi l
eriscal
ledCompilat
ion.
Foragi
venmachine,eachlanguagerequir
esitsownCompiler
.E.g.,f
oracomputert
o
beabl
etransl
ateaprogram wri
tt
eninFORTRANi nt
omachinelanguage;
thepr
ogram
mustpassthr
oughtheFORTRANcompi ler(whichmust‘
know’FORTRANaswel l
asthe
Machi
nelanguageofthecomput er
).
Theobjectcodefil
ecanbemadei ntoaf ull
yexecutabl
epr ogram bycar
ryi
ngouta
Linki
ngprocess,whichjoi
nstheobjectcodet oalltheotherfil
esthatar
eneededfort
he
executi
onoftheprogr am.Aft
erthelinkingprocess,anexecut abl
efi
lewihan.
t EXE
extensi
onisgenerated.Thisf
il
eisstor edonast oragemedi a.
Pointst
onot e.
Th ej
obofaCompi l
eri
smuchmor ediff
icultthant hatofanAssemblerinthat,
a
singl
estatementi
nahigh-
level
languageisequi v al
enttomanymachi neinst
ructi
ons.
Th ef
ormatofanAssemblyinst
ructi
onisf air
lyfixed,whil
ehigh-
lev
ellanguagesgivea
l
otoff r
eedom i
nthewaytheprogrammerwr itesstatements.
Funct
ionsofacompi
ler
.
ACompi
l
erper
for
mst
hef
oll
owi
ngt
asksdur
ingt
hecompi
l
ati
onpr
ocess:
1)
.I ti
dent if
iesthepr operor derofpr ocessi
ng, soast oexecutethepr ocessasf astas
possible&mi nimi zethest or
agespacer equi r
edi nmemor y.
2)
.I tall
ocat esspacei nmemor yf orthestor agel ocati
onsdefinedint hepr ogr
am tobe
execut ed.
3)
.I treadseachl i
neoft hesour cepr ogram &conv er
tsiti
ntomachi nel anguage.
4)
.I tchecksf orSy nt
axer r
or sinapr ogram (i.e.,
statementswhi chdonotconf ormto
thegr ammat ical r
ul esoft hel anguage) .I ftherearenosy ntaxerrors,itgener
ates
machi necodeequi valentt othegi venpr ogr am.
5)
.I tcombi nest hepr ogram ( machi ne)codegener atedwiththeappr opr i
ate
subrout i
nesf rom t helibrary.
6)
.I tproducesal isti
ngoft hepr ogram, i
ndicat i
nger r
ors,i
fany .
Di
ff
erencesbet
weenCompi
ler
sandI
nter
pret
ers
I
nter
pret
er Compi
ler
1.
Transl
ates&execut
eseachstat
ement 1.
Translat
esallt
hesourcecode
ofthesourcecodeoneatati
me. stat
ement satonceasauni tint
othei
r
correspondi
ngobjectcodes,befor
ethe
Thesourcecodei
nstr
uct
ioni
s
comput ercanexecut
ethem.
-3-
t
ransl
ated&immediatel
yobeyedby ACompi lertransl
atestheent
iresource
t
hecomput erhar
dwarebefor
ethenext program fi
rsttomachinecode, and
i
nstr
ucti
oncanbet r
anslat
ed. thenthecodei sexecutedbytheCPU.
(
Transl
ati
on&executiongotoget
her
). (Transl
ati
on&execut ionareseparate
phases)
2.
Transl
atestheprogram eacht
imeiti
s 2.
Compil
edpr ogr
ams( objectcodes)can
neededforexecuti
on;hence,i
tis besavedonast oragemedi aandrun
slowert
hancompi li
ng. whenrequi
red;henceexecut esf
aster
thani
nter
pretedprograms.
3.
Int
erpretedobj
ectcodestakel
ess 3.
Compi
l
edprogr
amsrequi
remore
memor ycomparedtocompil
ed memoryast
hei
robj
ectfi
l
esar
elarger
.
programs. 4.
ForaCompil
er,t
hesy ntaxer
rorsar
e
4.
ForanInterpr
eter
,thesyntax repor
ted&corr
ectedaftert
hesource
(gr
ammat ical
)err
orsarereport
ed& codehasbeentransl
atedtoit
sobject
corr
ectedbeforetheexecuti
oncan codeequi
val
ent.
conti
nue. 5.
Oncet hesour ceprogram hasbeen
transl
ated,iti
snol ongeravail
abl
eto
5.
AnInterpretercanrel
ateerror theCompi ler,sotheerrormessages
messagest othesourceprogram, areusuallylessmeani ngful
.
whichisal waysavail
abletothe
Int
erpreter.Thismakesdebuggi ngofa
program easierwhenusingan
Int
erpreterthanaCompi ler
.
Li
nker
s&Loader
s
Computerpr
ogramsareusual
lydev
elopedinModul esorSubrout
ines(i
.e.
,progr
am
segmentsmeanttocar
ryoutt
hespecifi
crel
evanttasks).Duri
ngprogr
am transl
ati
on,
thesemodul
esaretr
anslat
edsepar
atel
yintothei
robject(machi
ne)codeequival
ents.
TheLinkeri
sautil
it
ysoftwarethataccept
stheseparatelyt
ranslatedprogram modules
asit
sinput,
andlogi
call
ycombi nesthem i
ntoonelogicalmodule, knownast heLoad
Modulethathasgotal
ltherequiredbi
tsandpiecesforthetranslatedprogram t
obe
obeyedbythecomputerhardware.
TheLoaderisaut
il
it
yprogram t
hattr
ansf
erstheloadmodule(
i.
e.t
heli
nkerout
put
)int
o
thecomputermemory,
readyfori
ttobeexecutedbythecomput
erhar
dware.
Sy
ntax
Eachprogramminglanguagehasaspecial
sequenceororderofwr
it
ingcharact
ers.
Theter
m Sy nt
axreferst
othegrammati
calrul
es,whichgover
nhowwor ds,symbols,
expr
essionsandstatementsmaybeformed&combi ned.
Semant
ics
Thesear
erul
es,
whi
chgover
nthemeaningofsy
ntax.Theydi
ctat
ewhathappens(
takes
pl
ace)whenapr
ogr
am i
srunorexecut
ed.
Rev
iewQuest
ions.
1.
Definethefoll
owingter
ms:
(i
). Comput erprogr
am.
(i
i)
.Pr ogrammi ng.
-4-
(i
ii
).Programmi nglanguage.
2.
Withreferencetoprogrammi ng,
disti
nguishbetweenSour cepr
ogram andObj
ectcode.
3.
Whatist hefuncti
on(s)of:Assemblers,I
nter
preter
sandCompi ler
sinacomputer
system?
4.
(a).Whatar ethemainfuncti
onsofaCompi l
er?
(b).Di
fferenti
atebetweenaCompi lerandanInterpret
er.
LEVELSOFPROGRAMMI
NGLANGUAGES
Ther
earemanypr
ogr
ammi
ngl
anguages.Thel
anguagesar
ecl
assi
fi
edi
nto2maj
or
cat
egor
ies:
1)
.Low-l
evelpr
ogrammi
ngl
anguages.
2)
.High-
lev
elprogr
ammi
ngl
anguages.
Eachprogramminglanguagehasit
sowngrammatical
(sy
ntax)rul
es,
whichmustbe
obeyedinordert
owr i
teval
idprogr
ams,
justasanatur
all
anguagehasit
sownrulesf
or
for
mingsentences.
LOW-LEVELLANGUAGES
Thesearethebasicprogramminglanguages,whi
chcaneasil
ybeunder
stoodbyt
he
computerdir
ectly,
orwhichrequi
reli
tt
leeff
orttobet
ransl
atedint
ocomputer
under
standableform.
Theyincl
ude:
1.Machi nelanguages.
2.Assembl ylanguages.
Feat
uresofl
ow-
lev
ell
anguages
Theyaremachinehardware-
oriented.
Theyarenotport
able,i
.e.
,aprogram writ
tenf
oronecomput
ercannotbei
nst
all
ed
andusedonanothercomput erofadi f
fer
entf
amil
y.
TheyuseMnemoni ccodes.
Theyfr
equentl
yusesy mboli
caddr esses.
st
Machi
nel
anguages(
1 Gener
ati
onl
anguages)
Machi nelanguageiswr ittenusingmachi necodes( binarydigit
s)thatconsi stof0’s&
1’
s.
Thecomput ercanreadi l
yunder standMachi necode( language)instr
uctionswi t
houtany
tr
anslat i
on.
Apr ogrammeri srequiredt owr i
tehispr ogram instri
ngsof0’ s&1’ s,calculate&
al
locat ethecorememor yl ocati
onsf orhisdat aand/orinstructi
ons.
Diff
erentCPU’ shavedif f
er entmachi necodes, e.
g.,codeswr i
tt
enfort heI ntelPent
ium
processor smaydifferfrom t hosewr it
tenforMot orol
aorCy ri
xprocessor s.Therefor
e,
beforei nt
erpr
eti
ngthemeani ngofapar ti
cularcode,apr ogrammermustknowf or
whichCPUt heprogram waswr it
ten.
Amachi necodeinstructioni smadeupof2mai nparts;
(i
). AnAddr ess(oper and) :
Itspecif
iest
hel ocat i
on( address)oft hecomput ermemor ywher et hedat atobe
-5-
wor
keduponcanbef
ound.
(
ii
). AFunct
ion(
oper
ati
on)code:
Itstat
estotheContr
olUni
toftheCPUwhatoper
ati
onshoul
dbeperf
ormedont he
data/i
tem hel
dint
headdress,
e.g.
,Addi
ti
on,
Subt
ract
ion,
Div
isi
on,
Multi
pli
cat
ion,
etc.
Note.Thecomput ercanonlyexecut
einstructi
onswhicharewrit
teninmachine
l
anguage.Thi si
sbecause;iti
stheonlylanguagewhichthecomput ercanunderstand.
Therefore,
anyprogr
am writ
teninanyotherprogramminglanguagemustf i
rstbe
tr
anslatedint
omachinelanguage(bi
narydigit
s)befor
ethecomput ercanunderstand.
Assembl
yl 2ndGener
anguage( ati
onLanguages)
.
Assembl yl
anguageswer edevel
opedinordertospeeduppr ogr
ammi ng( i
.e.
,to
over
comet hedif
fi
cult
iesofunderst
andingandusingmachinelanguages).
Thev ocabular
yofAssembl yl
anguagesiscloset
ot hatofmachinelanguage,andthei
r
i
nstructi
onsaresy mboli
crepr
esentati
onsofthemachi nel
anguageinstructi
ons.
Assemblylanguageprogramsar
eeasi
ert
ounder
stand,
use&modi
fycompar
edt
o
Machinelanguageprograms.
Assembl yl
anguageprogr
amshav el
esser r
orchances.
Towr i
teprogr
am st
atementsi
nAssemblylanguage,theprogrammerusesasetof
symbolicoper
ati
oncodescall
edMnemoni ccodes.
Thecodecouldbea2or3shor tenedl
ett
erwor dthatwil
lcauset hecomputert
o
perfor
m speci
fi
coperat
ion.E.
g.,MOV–mov e,ADD-additi
on, SUB–subtracti RD-
on,
read.
Example;
RD PAT, 15 ( readt hev alue15st or
edi nthepr ocessorr egisternamed
PAT)
SUB PAT, 10 ( subt ract10f rom thev alueinr egi
sterPAT)
Aprogram wr i
tt
eni nanAssembl yl anguagecannotbeexecut ed/obeyedbyt he
comput erhardwar edi r
ectly
.Toenabl et heCPUunder standAssembl ylanguage
i
nstruct
ions, anAssembl er(whi chi sstoredi naROM)i susedt oconv ertt hem i
nto
Machinel anguage.
TheAssembl eraccept sthesour cecodeswr it
teninanAssembl ylanguageasi t
sinput,
andtranslatesthem i ntotheircor respondingcomput erlanguage( machi necode/object
code)equi val
ent .
Comment sar eincorporatedi ntot heprogr am stat
ement st omaket hem easi ertobe
underst
oodbyt hehumanpr ogr ammer s.
Assembl ylanguagesar emachi ne-dependent .Ther efore,aprogr am wr i
tteninthe
Assembl ylanguagef orapar ticul
arcomput ercannotr unonanot hermakeofcomput er
.
AdvantagesofLow- l
evellanguages
1.TheCPUcaneasi l
yunderstandmachinelanguagewi thoutt
ranslat
ion.
2.Thepr ogr
am inst
ruct
ionscanbeexecutedbyt hehardware(processor
)muchfast
er.
Thisisbecause;complexinstr
uct
ionsarealreadybrokendowni ntosmall
ersi
mpler
ones.
-6-
3.Low-l
evellanguageshav eaclosercontrol
overthehar
dware,arehi
ghl
yef
fi
cient&
al
lowdirectcontrolofeachoperati
on.
Theyarethereforesuit
ableforwrit
ingOperat
ingsyst
em sof
tware&Gameprogr ams,
whichrequirefast&effi
cientuseoftheCPUt i
me.
4.Theyrequirelessmemor yspace.
5.Low-l
evellanguagesarest abl
e,i
.e.
,theydonotcrashoncewrit
ten.
Disadv antagesofLow- lev ellanguages
Veryfewcomput erpr ogramsar eact uallywr itt
eni nmachi neorAssembl yl anguage
becauseoft hef ollowi ngr easons;
1.Low- l
evel languagesar ediffi
culttol earn, under stand, andwr itepr ogr amsi nthem.
2.Low- l
evel languagepr ogr amsar edifficulttodebug( remov eer rorsf rom) .
3.Low- l
evel languageshav eacol lectionofv er ydet ail
ed&compl exi nst ructionsthat
controlthei nter nal cir
cui ti
ngoft hecomput er.Ther ef or e,itrequi r
esonet o
understandhowt hecomput ercodesi nternal l
y.
4.Rel ati
ngt hepr ogr am &t hepr oblem st ructur esisdi fficul t,
andt herefor e
cumber somet owor kwi th.
5.Thepr ogr amsar ev erylong; hence, wr it
ingapr ogr am i nal ow- level l
anguagei s
usuallytedi ous&t imeconsumi ng.
6.Thepr ogr amsar edi f
ficulttodev elop, mai ntain, andar eal sopr onet oer rors(i
.e.
,i
t
requireshi ghlyt rainedexper tstodev elopandmai ntaint hepr ograms) .
7.Lowl evel languagesar emachi ne-dependent( speci fic) ,hencenon- por table.
Thisimpl iest hat ,theyar edesi gnedf oraspeci ficmachi ne&speci ficpr ocessor,
and
therefore,cannotbet ransferredbet weenmachi neswi thdi ff
er enthar dwar eor
softwarespeci ficat i
ons.
8.I ti
snoteasyt or ev i
set hepr ogram, becauset hiswi llmeanr e-wr it
ingt hepr ogram
again.
-7-
Read(Taxabl
ePay);
I
FTaxablePay<1000THEN
Tax:=0;
ELSE
Tax:=TaxRat
e*TaxablePay
;
Writ
e(Tax:6:
2);
All
owmodul arizat i
on(sub- rout
ines).
Theyar e‘ user-friendly’andpr oblem-ori
entedr atherthanmachi ne-based.Thi s
i
mpl i
est hat ,dur i
ngapr ogr ammi ngsession,thepr ogrammerconcent r
at eson
probl
em- solv i
ngr atherthanhowamachi neoper ates.
Theyr equi reonet obeobeyasetofr ul
eswhenwr i
ti
ngt heprogram.
Programswr itteni nhigh- l
evell
anguagesar eshor terthant hei
rlow-l
evel language
equiv
al ent s,sinceonest atementtransl
atesint osever almachinecodei nst r
ucti
ons.
Thepr ogr amsar epor t
abl ebetweendi f
ferentcomput ers.
Pur
poseofHi
gh-
lev
ell
anguages.
1.Toimprovethepr oductivi
tyofapr ogrammer .Thisi
sbecause;thesourceprograms
ofhi
gh-l
evellanguagesar eshor terthant hesour
cepr ogr
amsofl ow-l
evel
languages,
si
nceonest atementt r
anslatesintosev eralmachinecodeinst
ructi
ons.
2.Toeasethet raini
ngofnewpr ogrammer s,si
ncethereisnoneedt ol
earnthedetai
led
l
ayoutofapr ocession/sequence.
3.Tospeedupt est i
ng&er rorcorrecti
on.
4.Tomakepr ogr amseasyt ounder stand&f ol
low.
AdvantagesofHi gh-level l
anguages.
1.Theyar eeasi l
ypor table,i
.e.,theycanbet ransferredbet weencomput ersofdi f
ferent
familiesandr unwi thlit
tl
eornomodi fi
cat i
on.
2.High-level l
anguagepr ogramsar eshor t, andt akeshor tertimet obet r
anslated.
3.Theyar eeasyt olean, under standanduse.
4.Theyar eeasyt odebug( cor rect/remov eer rors),&mai ntain.
5.Highl evel l
anguagepr ogramsar eeasyt omodi f
y,andal sot oincorporat
eaddi ti
onal
featurest husenhanci ngitsf unctional capabi l
it
ies.
6.Theyar e‘ user-
fri
endl y’&pr oblem- ori
ent ed; hence, canbeusedt osol v
epr oblems
arisi
ngf rom ther eal world.
7.Theyenabl eprogr ammer stoadapteasi lytonewhar dware.Thi sisbecause; they
don’thav et owor ryaboutt hehar dwar edesi gnoft hecomput er.
8.High-level l
anguagepr ogramsar esel f-
document i
ng, i.
e.,t
hepr ogram statement s
displayst hetranspar encyofpur posemaki ngt hev eri
fi
cationoft heprogram easy .
9.Highl evel l
anguagesar emor ef l
exi ble;hence, t
heyenhancet hecreati
vit
yoft he
programmerandi ncreasehi s/ herpr oduct ivit
yint hewor kplace.
Di
sadvantagesofusi ngHigh-levellanguages
1.High-
level
languagesar enotmachi ne-or
iented;hence,t
heydonotuseoft heCPU
andhardwaref acil
iti
eseffi
cientl
y.
2.Thelanguagesar emachi ne-i
ndependent ,andcannotbeusedi nprogrammingt he
hardwaredir
ectly.
3.Eachhigh-
levellanguagest atementconv ertsint
osev er
almachinecodeinst
ructions.
Thismeanst hat,theyusemor estoragespace, andital
sotakesmor eti
metor unt he
program.
-8-
4.Theirpr
ogram statement saretoogener
al;
hence, t
heyexecutesl
owl ythanthei
r
machinecodepr ogram equival
ents.
5.Theyhavetobei nterpretedorcompil
edtomachi ne-r
eadabl
eform beforethe
computercanexecut et hem.
6.Thelanguagescannotbeusedonv er
ysmal lcomput ers.
Thesourceprogram wr itt
eninahigh-
lev
ellanguageneedsaCompi l
er,whichis
l
oadedintothemai nmemor yofthecomput er,andthusoccupiesmuchofmemor y
space.Thisgreatl
yr educesthememor yavailableforasourceprogram.
TYPESOFHI
GH-
LEVELLANGUAGES.
Hi
gh-l
evell
anguagesar eclassi
fiedint
ofivedi
ff
erentgroups:
1.Thir
dgener ati
onl anguages(St r
uctur
ed/Procedurall
anguages)
.
2.Fourthgenerati
onl anguages( 4GLs).
3.Fi
fthgenerati
onl anguages( 5GLs)
4.Object-
ori
entedpr ogrammi nglanguages(OOPs).
5.Webscr i
pti
ngl anguages.
Thev
ari
oust
ypesofhi
gh-
lev
ell
anguagesdi
ff
eri
n:
Thedatastructur
est heyhandle.
Thecontrol
st r
ucturestheysupport
.
Theassi
gnmenti nstructi
onstheyuse.
Appl
icat
ionareas, e.
g.,educat
ional
,busi
ness,
sci
ent
if
ic,
etc.
STRUCTUREDLANGUAGES
Ast ruct ured(
procedural)l
anguageall
owsal argeprogr
am tobebrokenintosmal l
ersub
-pr
ogr amscal edmodul
l es,eachperf
ormingapar ti
cul
ar(si
ngl
e)task.Thistechniqueof
progr am designisreferr
edt oasstr
uctur
edpr ogramming.
Struct uredprogrammi ngalsomakesuseofaf ewsimplecont
rol
st r
uct
uresinpr oblem
solvi
ng.The3basi ccont r
olstr
uct
uresare:
Sequence
Sel ection.
I ter ati
on(l
ooping).
Adv
ant
agesofst
ruct
uredpr
ogr
ammi
ng.
1.Iti
sf lexibl
e.
2.Structuredpr ogr amsar eeasi
ertor ead.
3.Progr amsar eeasyt omodi f
ybecause; aprogrammercanchanget hedetail
sofa
sect i
onwi thoutaf fect
ingtherestoft hepr
ogram.
4.Iti
seasi ert odocumentspeci f
ictasks.
5.Useofmodul est hatcontainstandardproceduresthr
oughoutt heprogram saves
dev el
opmentt i
me.
6.Modul escanbenamedi nsuchawayt hat
,theyareconsist
entandeasyt of i
ndin
document ation.
7.Debuggi ngi seasi erbecause;eachmodul ecanbedesi gned,coded&t est
ed
i
ndependent ly .
Exampl
esofThi
rdgener
ati
onpr
ogr
ammi
ngl
anguagesi
ncl
ude:
1)
.BASI
C(Begi
nner
sAl
l
-pur
poseSy
mbol
i
cInst
ruct
ional
Code)
.
BASICisasi
mplegeneral
-pur
posehi gh-
levell
anguageusedinmostcomputer
pr
ocessingt
askssuchasdevelopi
ngbusi nessandeducati
onal
appli
cati
ons.
I
tiseasytol
earn&use;hence,sui
tableforstudent
swhowi sht
oeasil
ylear
n
-9-
pr
ogrammi ng.
Tr
anslat
ioninmostv
ersi
onsofBASI
Ciscar
ri
edoutbyanI
nter
pret
er.
Di
sadv
ant
agesofBASI
C.
BASICisav ai
l
abl
einsomanyv ersi
onswithdiff
erentdial
ects/l
anguages&
ther
efor
e, i
thasnost
andard.
Somedi al
ectsar
eli
mitedtodata&controlstr
ucturestheysupport.
Somev ersi
onsofBASICofferl
i
mi t
edfacil
i
tiesi
nt ermsofstructur
ed
progr
ammi ng&meaningfulvar
iabl
enames.
2)
.PASCAL
PASCALi sageneral-
purpose,high-l
evelprogrammi nglanguage,whichwasnamed
aft
eraFrenchmat hematiciancall
edBlaisePascal.
Itwasdevel
opedasanacademi ctooltohelpinthet eachingandlearningof
str
uctur
edprogrammi ng.
PASCALsupportsst r
ucturedprogrammi ng,i.
e.,
itusespr ocedur
es&f unct
ions,
whichal
lowa‘top-down’ approachtosolvingproblems.
I
tisnoteasytol
ear
nbecause;i
thasst
rictr
ulesi
nit
sgr
ammaront
ypi
ngof
v
ariabl
es(dat
anames)&declar
ati
ons.
I
tispoor(
hasl
i
mit
edabi
l
ity
)onhandl
i
ngofdat
afi
l
es.
3)
.COBOL(
COmmonBusi
nessOr
ient
edLanguage)
COBOLisdesignedfordevelopingprogramsthatsol
vebusinesspr
oblems,e.g.
,can
beusedtodevelopcommer cialdataprocessi
ngappli
cati
onssuchascomput er
-
basedi
nvent
orycont r
olsy
st ems.
COBOLismost l
yusedwher elargeamount sofdataaretobehandl
ed,becauseit
suppor
tspowerfuldata&cont r
olstruct
ures.
COBOLprogramsar esemi-compiled,andtheint
ermediat
ecodeisi
nterpr
eted.
Apr
ogr
am wr
it
teni
nCOBOLl
anguageconsi
stsof4di
vi
sions:
-
(
i)
. Ident
if
icati
ondiv
isi
on:Wherethepr
ogrammer&thepr
ogr
am det
ail
sar
e
speci
fied,
e.g.
,pr
ogram I
D,pr
ogrammername,et
c.
(
ii
). Envi
ronmentdi
vi
sion:Wher
et heequipment
st obeusedbythesour
ce&t
he
obj
ectprogr
amsaredefi
ned,e.g.
,thecomputerhar
dware.
(
ii
i)
.Dat adivi
sion:Wherethev ar
iousf
il
estobeusedbyt
hepr
ogr
am ar
e
descr
ibed,e.g.
,adescr
ipt
ionoftheinputf
il
es.
(
iv)
.Proceduredi
visi
on:Whereal
ltheprocedur
esrequir
edto
mani
pulat
e/i
nter
rel
atet
hedat
ai nt
oinformat
ionaredef
ined.
Adv
ant
agesofCOBOL.
I
tiseasyt oread.
I
tisportable,i
.e.
,canbeusedondi fferentt
ypesofcomputer
s.Thi sisbecause;
thasanAmer
i icanNati
onalInsti
tut
est andard.
Amer i
canNat i
onalStandardsInsti
tute(ANSI )
:–Anint
ernat
ionalorgani
zati
on
thatdevi
sed/inventedthegroupofst andar
dizedsy
mbolsusedi nfl
owcharti
ng.
I
tiswi
del
yused,
andhasapool
ofski
l
ledpr
ogr
ammer
s.
-10-
Di
sadv
ant
ageofCOBOL.
Thestr
uct
ureofaCOBOLprogr
am i
stool
ongevenforsi
mpl
epr
ogr
ams.
E.
g.,
consi
derthef
oll
owi
ngassi
gnmentst
atement
:
DIVIDEAi nt
oBgivingC.
ThisstatementwhenusedinBASIClanguagecanmuchshort‘
C=A/
B’.Howev
er,
noti
cet hatt
heCOBOLst at
ementaboveismoresel
fdefi
ning.
4)
.FORTRAN(
FORmul
aTRANsl
ator
)
Itwasdevel
opedf ormathemati
cians,scient
ist
sandengi neer
s.I tprovi
desan
easi
erwayofwr it
ingsci
enti
fi
c&engi neeringappl
icat
ions.
FORTRANst atementsar
emost lyinform ofmathemat i
calexpressions;hence,
iti
s
useful
inwri
tingofprogr
amst hatcanpr ocessnumericdata.
FORTRANpr ogramsarecompi l
ed.
Adv
ant
agesofFORTRAN.
I
tispor
tabl
e,i
.e.i
tcanbeusedondi
ff
erentt
ypesofcomput
ers.
Di
sadv
ant
ageofFORTRAN.
I
tisnotsui
tedf
orbusi
nessappl
i
cat
ions.
5)
.Ada
Thi
slanguagewasnamedaft
erthefi
rstladyprogr
ammerAdaLov el
ace.
I
tissui
tabl
efordev
el ngmi
opi l
it
aryi
,ndustri
alandreal
-t
imesy
stems.
6)
.C
Ci smainl
yusedf ordevel
opingsy st
em sof
twaresuchastheoperati
ngsy
stem as
wellasdevelopi
ngt heappl
icati
onpackages.
Ithaspowerfulcommandst hatpermitt
herapi
ddevelopmentofprogr
ams,and
all
owsdi r
ectcontrolov
erthehar dware.
Di
sadv
ant
ageofC
I
tisdi
ff
icul
ttor
ead&l
ear
nbecauseofi
tsst
ri
ctdi
alectr
ules.
7)
.LOGO
LOGOwasdesignedforeducat
ional
useinwhi
chchi
ldrencanexpl
ore&dev
elop
concept
sthr
oughprogr
ammi ngthemovementofapen.
8)
.COROL
COROLi
susedinReal
-t
imeprocessi
ng.
COROLpr
ogramsarecompi
led.
9)
.RPG(
Repor
tPr
ogr
am Gener
ator
)
RPGisusedinrepor
tgener
ati
ngappl
icat
ions,
(i.
e.i
tisdesi
gnedt
ofaci
l
itat
ethe
out
putofrepor
tsofbusi
nessdat
a).
ARepor
tgenerat
orisasoft
waretool
thatextract
sstoreddat
atocreat
e
cust
omi
zedreport
sthatar
enotnormall
y/usuall
yproducedbyexi
sti
ngappl
i
cat
ions.
10)
.SNOBOL(
Str
ingOr
ient
edSy
mbol
i
cLanguage)
.
I
tisahigh-
lev
ell
anguagedesi
gnedt
omanipul
atest
ri
ngsofchar
act
ers.I
tis
t
heref
oreusedfornon-
numeri
cappl
i
cat
ions.
-11-
FOURTHGENERATI
ONLANGUAGES(
4GL’
S).
4GLsmakepr ogrammi ngev eneasi ert
hant he3GLsbecause; t
heypr esentthe
programmerwi thmor epr ogrammi ngtools,suchascommandbut t
ons,forms,
textboxesetc.Theprogr ammersi mpl yselectsgraphicalobjectscal edcont
l r
olsont he
screen,andthenusest hem tocr eatedesignsonaf orm bydr aggingamousepoi nter.
Thel anguagesalsouseappl icat
iongenerat ors(whichinthebackgr ound)togenerate
thenecessaryprogram codes; hence, t
hepr ogrammeri sfreedf rom thetediouswor kof
writi
ngt hecode.
4GLsar eusedt oenquire&accesst hedatast oredindatabasesy stems;hence,theyare
descr i
bedastheQuer ylanguages.
Pur
poseoff
our
thgener
ati
onl
anguages.
The4GL’swer edesignedtomeett hef oll
owi ngobj ect i
ves:-
1.Tospeedupt heappl i
cati
on-bui
ldingpr ocess, t
her ebyincreasingtheproducti
vi
tyofa
programmer .
2.Toenabl equick&easyamendment sandal terationofpr ograms.
3.Tor educedev el
opment&mai ntenancecost s.
4.Tomakel anguagesuser -
fr
iendly.Thisi sbecause, the4GL’ saredesi
gnedt obeuser
-ori
ented,unl
ikethe3r dgenerati
onl anguageswhi charepr oblem &programmer
ori
ented.
5.Toal l
ownon- professi
onalend-userst odev elopt heirownsol uti
ons.
6.Togener atebug-freecodesfrom high-levelexpr essionsofr equir
ements.
Exampl
esof4GLsar
e:
Vi
sualBasi
c
Del
phiPascal
Vi
sualCOBOL(Obj
ectCOBOL)
AccessBasi
c
Adv
ant
agesoff
our
thgener
ati
onl
anguages.
1.Theyareuser-
based,andt heref
or e,easyt olear
n&under stand.
2.Thegrammarof4GL’ sisv erycloset othenat uralEngl
ishlanguage.Itusesmenus&
promptstoguideanon- special
i
stt oretrievedatawi t
hease.
3.Veryl
it
tlet
rai
ningisrequiredinor dertodev elop&use4GLpr ograms.
4.Theyprovi
defeatur
esf orformat t
ingofi nput,processi
ng, &instantr
eport
ing.
FI
FTHGENERATI
ONLANGUAGES(
5GL’
S).
The5GL’ saredesignedtomakeacomput ersolv
eapr oblem byport
rayinghuman- l
i
ke
i
ntell
igence.
Thelanguagesar eabletomakeacomput ersol
veapr oblem fort
heprogr ammer;hence,
he/shedoesnotspendal otofti
mei ncomingupwi ththesoluti
on.Thepr ogr
ammer
onlythinksaboutwhatproblem needstobesolvedandwhatcondi ti
onsneedt obemet
wit
houtwor ryi
ngabouthowt oi
mpl ementanalgori
thm tosolvetheproblem.
5GLsar emost l
yusedinar t
if
ici
ali
ntell
i
gence.
Exampl esof5GLsar e:
PROLOG
-12-
LI
SP
Mercur
y
OCCAM.
1)
.LI
SP(
LIStPr
ocessi
ng)
I
nLISP,bothprograms&dataarearr
anged(st
ruct
ured)asli
sts.
I
tisusedinart
ifi
cial
int
ell
i
gence.However
,iti
snotsuit
ableforcommer
cial
dat
a
pr
ocessi
ngappl i
cati
ons.
2)
.PROLOG(
PROgr
ammi
ngi
nLOGi
c)
PROLOGwasdev elopedfrom LISPbyt heJapanese.
Itisdesi
gnedforusewi t
hExper tsyst
ems&Ar t
if
ici
alInt
ell
igence.Itismostl
yused
forsolvi
ngprobl
ems, whichinvolveobj
ectsandrelat
ionshipsbetweenobject
s.
LikeLISP,i
tisnotsuit
ableforcommer cialdat
aprocessingapplicat
ions.
OBJECT-
ORI
ENTEDPROGRAMMI
NGLANGUAGES(
OOPs)
Obj ect-
OrientedPr ogrammi ngi sanewappr oacht osof twar edev elopmenti nwhi ch
data&pr ocedur est hatoper ateondat aarecombi nedi ntooneobj ect.
OOPsuseobj ects.AnObj ecti sar epresentati
onofasof twar eent i
tysuchasauser -
definedwi ndoworv ariable.Eachobj ecthasspeci f
icdat av aluest hatar euniquetoit
(calledstate)andasetoft het hingsi tcanaccompl i
shcal l
ed( functionsorbehav i
our).
Sev eralobjectscanbel inkedt oget hertof or
m acompl etepr ogr am.Pr ogr
amssend
messagest oanobj ectt oper f
orm apr ocedurethati salr
eadyembeddedi nit.This
processofhav i
ngdat aandf unctionst hatoperateont hedat awi t
hinanobj ectiscalled
encapsul ation.
Thedat ast ructure&behav iourofanobj ecti
sspeci fi
ed/descr ibedbyat emplate(call
ed
acl ass).Cl assesar ehi erar chical,andi tispossibletopasst hedat a&behav i
ourofan
objectinonecl assdownt hehi erar chy.
Object -
Orientedpr ogr ammi ngenabl esrapidprogram dev elopment .Everyobjecthas
proper t
iessuchascol our,si ze,dat asour ce,et
c,whi chcanbeseteasi lywithoutmuch
effort.Inaddi ti
on, ever yobj ecthasev entsassociatedwi thitthatcanbeusedt otri
gger
certainact i
ons, e.g.remov et hewi ndowf r
om thescr eenoncl ickingt he‘Cl
ose’ butt
on.
OOPhascont
ri
butedgr
eat
lytothedev
elopmentofgr
aphi
caluseri
nter
faceoper
ati
ng
sy
stemsandappl
i
cati
onprogr
ams.
Exampl
esofObj
ect
-or
ient
edpr
ogr
ammi
ngl
anguagesar
e:-
Simula
C++
SmallTal
k
Java
Javaissometi
mesassoci
atedwit
hdev el
opmentofwebsi
tes,buti
tcanbeusedt
o
creat
ewholeappl
i
cati
onprogramsthatdonotneedawebbr owsertor
un.
JAVA
JavaisanOOPl anguagethatresembl esObjectC(asimpl
if
iedform ofC++)
.
ThecodeofJav adispl
aysgraphics,accessesthenetwor
k,andinter
faceswi
thuser
svi
a
asetofcapabil
it
iesknownascl asses.Cl assesdef
inesi
milarst
ates&common
methodsforthebehaviorofanobject.
JAVApr ogr
amsarenotcompi
ledi
ntomachinecode;
inst
ead,t
heyar
econver
tedi
ntoa
col
lect
ionofby
testhatr
epr
esentt
hecodeforanabstactJav
r aVirt
ualmachi
ne(VM).
-13-
AJavai
nterpr
eterrunni
ngonaphysi
calmachineisthenusedtotr
ansl
atet
hoseby
tes
i
ntol
ocalacti
ons,suchaspr
int
ingastr
ingordrawingabutton.
WEBSCRI
PTI
NGLANGUAGES.
Webscri
pti
nglanguagesaremost l
yusedt ocr
eateoraddfunct
ionali
ti
esonwebpages.
Webpagesareusedf orcr
eati
ngWebsi t
esontheInt
ernetwher
eal lsor
tsofadver
ti
sing
canbedone.
Webpagesarehypertext(
plai
n-t
ext)documentswri
tt
enusingalanguagecal l
ed
Hyper
TextMarkupLanguage( HTML) .HTMLdocument shaveaf i
leextensi
onof.
Html
or.
Htm.
Note.HTMLdoesn’ thavethedeclar
ationpartandcontrol
str
uctur
es,andhasmany
l
imitat
ions.Ther
efore,
todevelopfunctionalwebsi
tes,
itmustbeusedtogetherwi
th
ot
herwebscr i
pti
nglanguageslikeJavaScript
,VBScri
ptandHypert
extPrepr
ocessor
.
Compar
isonofPr
ogr
ammi
ngl
anguages.
Machi
nel
anguage Assembl
ylanguage Hi
gh-
lev
ell
anguages
1.
Inst
ruct
ionseti
smade 1.
Instr
uct
ionseti
smade 1.
Instructionseti ssimi l
ar
upofbinarydi
git
s(0’
s& upofMnemoni cs& toEngl ishl anguage
1’s)
. label
s. statement s&
mat hemat icaloperators.
2.
Instr ucti
onismadeof2 2. Instructionismadeupof 2. Thei nst ructi
onv ari
es
par ts:operationcode& 2par ts: operati
oncode& dependi ngont he
oper and. oper and, butcomment s par t
icularlanguage.
canbeadded.
3.
Not ranslati
onisneeded. 3.
UsesanAssembl ert o 3.
Usescompi l
eror
(Thi sist hecomput er conv ertt heassembl y interpreter
language; hence,the languagesour cecodest o Compi l
ert ranslat
esal l
comput erunderst
andsit t heirobj ectcode thesour cecodeatonce
direct l
y). equiv alents i
ntoobj ectcode;
4.
Execut edbyt he I
nter pretert r
anslatesline
har dwar edirectl
y&i s 4.
Execut edf asterthanHi gh byl i
ne.
fast er. -lev
el ,butslowert hant he 4.
Translat ion&execut ionis
machi necodepr ogr ams. verysl ow.
5.
Dif
ficul
ttolear
n,devel
op 5.
It
’seasi
ert
olearn,
&mai ntai
n. devel
op&mai nt
ainas 5.
Easyt
ol ear
n,dev
elop,
comparedtomachine maint
ainanduse.
6.
Programsar el
engthy& code pr
ogr
ams .
t
edi
ous. 6.
Likemachi necode 6.
Progr
amsareshorter&
language,theprograms simpl
ert
hanMachi ne&
7.
Iti
sti
me-consumi
ngto arelengthy&tedious. assembl
ylang.pr
ograms.
devel
opmachinecode 7.
Theytakeashor tert
ime 7. Dev el
opingHi gh-
lev
el
progr
ams. todevelopascompar ed languagepr ogramstakes
tomachi necode veryshortti
me.
progr
ams, butt akelonger
8.
Usedi nappli
cati
ons thanH i
gh -
levellanguage
wher eeff
ici
entuseof progr
ams. 8.
MostHi gh-l
ev el
theCPUt imeis 8.
Likemachi nelanguage, languagesar egeneral-
necessary,e.
g., Assembl ylanguage purpose, &canbeusedt o
developi
ngOper at
ing progr
amsar eusedi n doal mostallcomput er
-
systems&ot herControl appli
cati
onswher e processingtasks.
-14-
programst hat eff
ici
entuseoft
heCPU
coordinatethewor
king ti
meisnecessar
y.
ofperipheral
s.
Fact
orst
oconsi
derwhenchoosi
ngaPr
ogr
ammi
ngl
anguage.
Thef
oll
owingf
act
orsshoul
dbeconsi
der
edwhenchoosi
ngaPr
ogr
ammi
ngl
anguaget
o
usei
nsolv
ingapr
oblem:
1)
.Theav
ail
abi
l
ityoft
her
elev
antt
ransl
ator
2)
.Whet
hert
hepr
ogr
ammeri
sfami
l
iarwi
tht
hel
anguage
3)
.Easeofl
ear
ninganduse
4)
.Pur
poseoft
hepr
ogr
am,
i.
e.,
appl
i
cat
ionar
eassuchaseducat
ion,
busi
ness,
sci
ent
if
ic,
etc.
5)
.Execut i
ont i
me
Appli
cati
onst hatrequi
requickresponsear
ebestprogrammedi nmachi
necodeor
assemblylanguage.Hi gh-
level
languagesarenotsui
tabl
eforsuchappl
i
cat
ion
because,t
heyt akelongtobet r
anslat
ed&executed.
6)
.Dev
elopmentt
ime
Developmentt imeistheti
meapr ogr
ammert akestowr i
teandr unaprogr
am.
High-l
evellanguagesareeasytoread,under
standanddev elop;hence,t
heyrequir
e
l
essdev elopmenttime.Machi necode&Assembl ylanguagesar erel
ati
vel
ydiff
icul
t
t
or ead,understandanddevelop;hence,
theyareti
me- consuming.
7)
.Popul ari
ty
Thelanguagesel
ectedshoul
dbesuit
abl
eand/
orsuccessf
uli
nthemar
ketwi
th
respectt
otheprobl
emst obesol
ved.
8)
.Document at
ion
I
tshouldhav
eaccompanyi
ngdocumentat
ion(
descri
pti
ons)onhowt
ouset
he
l
anguageormaint
aint
heprogr
amswrit
tenint
helanguage.
9)
.Mai
ntenance
Programsaredevel
opedtosol
vespeci
fi
cproblems,andthepr
obl
emskeepon
changing;
hence,
theprogr
amsarealsochangedtoperf
ormthenewf
uncti
ons.
Progr
am mai nt
enanceistheacti
vit
yofi ncor
porat
ingmorerouti
nesontothe
progr
am, modi f
yingtheexi
sti
ngroutinesorremovingt
heobsoleterout
inest
omake
theprogram adapttoafuncti
onall
yenhancedenv i
ronment.
Themai ntenanceismadeeasierifthelanguageusediseasytoreadand
underst
and.
10)
.Av
ail
abi
li
tyofski
ll
edpr
ogr
ammer
s
Thelanguageselect
edshoul
dhaveapool ofr
eadi
lyavai
l
abl
epr
ogr
ammer
stoease
theprogr
ammi ngacti
vi
ty,
andreducedev
elopmentti
me.
Rev
iewQuest
ions
1.(
a).WhatisaProgr
amminglanguage?
(
b).Expl
aint
hetwolev
elsofprogramminglanguages.
2.(
a).Whatismeantby‘
Machinelanguage’
?
-15-
(b).Explainwhymachi nelanguagepr ogr ammi ngissoer ror
-prone.
(c).Showt hediff
erencebet weenMachi nelanguageandAssembl ylanguage.
(d).Givet woadv ant ages&t hr eedi sadv ant agesofMachi nel anguagepr ogramming.
3.(a).Whatar eHigh-level languages?
(b).Givet hefeatures/ char act eristicsofhi gh-l
ev el
programmi ngl anguages.
(c).Descr i
bebr i
eflyhowapr ogr am wr it
teni nhigh-l
evelprogrammi nglanguage
becomesamachi necodepr ogr am r eadyf oroperati
onaluse.
(d).Explaintheadv ant agesanddi sadv ant agesofusi ngaHi gh-levelprogramming
l
anguagef orwr i
tingapr ogr am.
(e).Listfourexampl esofhi gh- level pr
ogr ammi nglanguages.I ndicatethe
appli
cat i
onofeachl anguagei ncomput ing.
4.(a).Whati smeantbypr ogr am por t abili
ty?
(b).Whyar elow-level l
anguagesnotconsi deredt obepor t
able?
5.List8f actorsthatneedt obeconsi der edwhensel ectingaprogr ammi nglanguage.
PROGRAM DEVELOPMENT.
St
agesi
nvol
vedi
nthepr
ogr
am dev
elopmentcy
cle.
Theprocessofpr ogr
am dev el
opmentcanbebr okendowni
ntot
hef
oll
owi
ngst
ages:
1.Problem recogni t
ion(I
dentif
icat
ionofthepr
oblem).
2.Problem definiti
on.
3.Program desi gn.
4.Program codi ng.
5.Program testing&debuggi ng.
6.Program Impl ement at
ionandmai nt
enance.
7.Program document ati
on.
Pr
obl
em r
ecogni
ti
on.
Probl
em r ecogni
ti
onref
erstot
heunderst
andingandinter
pret
ati
onofapar ti
cul
ar
probl
em.
Thepr ogrammermustknowwhatproblem he/shei
stry
ingtosolve.He/shemustal
so
understandclear
lyt
henatur
eofthepr
oblem &t hef
uncti
onofthepr ogr
am.
-16-
Inor
dertounder
standapr
obl
em,
lookf
ort
hekey dssuchascomput
wor e,ev
aluat
e,
compare,
etc.
Usuall
y, apr
ogrammeridenti
fiesproblemsintheenvi
ronmentandtri
estosolvethem by
wri
tingacomput erpr
ogram.
Therear e3sit
uati
onsthatcausethepr ogr
ammert oi
denti
fyaprobl
em thati
swor th
sol
v i
ng:
1.Pr oblemsorundesir
ablesituat
ionsthatpr
eventanindi
vi
dualororgani
zat
ionsfrom
achievi
ngthei
rpurpose.
2.Oppor
tunit
ytoimprov
ethecur
rentpr
ogram.
3.Anewdirecti
vegi
venbythemanagementrequi
ri
ngachangei
nthecur
rentsy
stem.
Samplepr
oblem:Dev
elopapr
ogr
am t
hatcanbeusedt
ocal
cul
ate/
findt
hear
eaofa
ci
rcl
e.Usetheequat
ionA=π*r.
2
Pr
obl
em def
ini
ti
on(
Probl
em Anal
ysi
s).
I
nPr oblem defini
ti
on, t
hepr ogrammert r
iestodef i
ne(det
ermine)the:
(
i)
. Out putexpect edfrom thepr ogr
am.
(
ii
).I nputsneededt ogener atetheoutputi nf
ormat i
on.
(
ii
i)
.Pr ocessingact i
vit
ies(requir
ement s),and
(
iv)
.Ki ndoff i
leswhichmaybeneeded.
Th eprogrammershoul dwr i
teanar rati
veonwhatt hepr
ogram willdo,andhowi ti
s
meantt oachi ev
et heintendedpur pose.Wi thinthi
snarrat
ive,
he/sheisr equi
redto
determinewhatdat aistobei nput&whati nformati
onistobeout put.
Forexample:
Incalcul
ati
ngt
heareaofanyci
rcl
e,t
hepar amet
ersneededt
odet
ermi
net
hear
eaofany
cir
cleare:
1.I nput
: ( a)Pi
e(π)whichi
saconst ant.
(
b)Theradi
usoft
hecircl
e.
2.Pr
ocess:Thef
ormul
aforcal
cul
ati
ngar
eaofaci
rcl
e,whi sπ*r
chi adi
us*r
adi
us.
3.Out
put
: Thear
eaoft
heci
rcl
e(A)
.
Attheendoft heproblem def
ini
ti
on,thepr
ogrammerisrequi
redt
owr i
tearequi
rements
repor
t/documentfort henewprogram.Thisdocumentwil
lenabl
etheprogr
ammert o
comeupwi t
hapr ogram desi
gnthatmeetstheneedsathand.
Note.Probl
em defi
nit
ionshouldbedonet hor
oughl
ytoensureusersat
isf
acti
on,
andto
faci
li
tat
ethesubsequentst
agesint hepr
ogram dev
elopmentcycl
e.Afail
ureatt
his
stageusual
l
yresult
sinasy st
em thatwil
lnotworkasint
ended,ort
hatmaynotworkat
all
.
Pr
ogr
am desi
gn
Program designistheact
ualdevelopmentoftheprogr
am’sprocessorpr
oblem sol
ving
l
ogiccall
edt heAlgori
thm.
I
tinvolv
esi dent
if
yingthepr
ocessingtasksrequi
redtobecar
ri
edoutinordertosolv
e
theproblem.
Thedesignstageenabl
estheprogrammertocomeupwi t
hamodeloft
heexpect
ed
progr
am (orageneralf
ramework(outl
i
ne)ofhowtosolv
ethepr
obl
em,andwhere
possi
ble,br
eakiti
ntoasequenceofsmall&simpl
esteps.
-17-
Themodelsshowthefl
owofev
entsthr
oughoutt
heent
ireprogr
am fr
om t
het
imedat
a
i
sinputt
othetimethepr
ogr
am gi
vesoutt
heexpect
edinfor
mation.
√ Thepr ocessi
ngt asksmustbei norder&sy st
emat i
c.Therefore,
theprogrammer
i
dentifi
esthepr ocessingt
asksrequired,andtheexactorderinwhichtheyaret
obe
carri
edout .
√ Thedesi gnprocessdoesnott akeaccountoft heprogr
ammi nglanguagetobeused
i
nt hefinalpr
oduct ,si
ncei
tonlydefinesprogram l
ogic.
√ Program designpr ovi
desforeasymai nt
enance.
Note.Iti si mpor tanttodesignpr ogramsbef or eent eri
ngt hem intothecomput er
.The
programmershoul donlyat t
emptt ocov ertadesi gni nt
oapr ogram codeaf terensur ing
thati
tisl ogical l
ycor r
ect.Ifpossi ble,checkt hel ogicalorderont hedesk.
Somepr ogr ammer sproducer ough&r eadysol utionsataKey board, andcont inueto
amendt hepr ogramsunt ilevent uall
ythepr ogr am appear stodowhatwasexpect ed.
Thisisnotr ecommendedi npr ogr ammi ngbecauseoft hef ol
lowingr easons:
1.Thef inal codemaynotbeeasyt ofoll
ow, sincei twasj ustcobbledt oget her .
2.Var iablenames&speci fici
t emsofcodemaynotbedocument ed.
3.Pr ogr amspr oducedbycont i
nuousamendment s&changi ngofcodesmost l
yleadt o
unforeseensi deef f
ects.
E.g.
, theremaynothav ebeenpl anfort est i
ngt hepr ogram orpr ocedur es, hence,the
program mayeasi lyfail
.
4.Apr ogr ammermaybeaskedt omodi fythecodeatal aterdate.Wi t
houtsuf fi
cient
document ati
on, t
hepr ogrammerwi llbef orcedt ot r
acet hrought hepr ogr am inor der
togai nani nsighti
ntohowt hepr ogram funct i
ons.
Modul
arpr
ogr
ammi
ng
Manyprogr amsarenon-monoli
thi
c(i
.e.
,theyarenotusual l
ymadeupofonel ar
geblock
ofcode)
.I nstead,
theyaremadeupofsev eralunitscall
edmodules,t
hatworkt
ogether
tofor
mt hewhol eprogr
am wi
theachmodul eper formingaspeci
fict
ask.
Thi
sappr oachmakesapr ogr
am fl
exi
ble,easiertoread,andcarr
youterr
orcorr
ect
ion.
Pr
ogr
am codi
ng
Pr
ogr
am codi
ngi
stheact
ual
processofconv
ert
ingadesi
gnmodel
int
oit
sequi
val
ent
pr
ogr
am.
Codi
ngrequi
restheprogr
ammertoconv
ertt
hedesi
gnspeci
fi
cation(al
gor
it
hm)i
nto
act
ualcomputeri
nst
ructi
onsusi
ngapar
ti
cul
arpr
ogramminglanguage.
Forexampl
e;
Thepr ogr
ammermayber equir
edt owrit
et heprogram codeeit
herinPascalC++,
, Vi
sual
BasicorJava,
anddevel
op( i
nvent)sui
tableidenti
fi
ers,var
iabl
enames,&t hei
rdat
a
types.However,
rememberthat,atthi
sstaget hecodingissti
llaPenci
l&paper
exerci
se.
Theendr esul
tofthi
sstagei
sasour
ceprogr
am thatcanbetransl
atedint
omachi
ne
readabl
ef or
mf ort
hecomputert
oexecut
eandsolvethet
argetproblem.
Rul
esf
oll
owedi
ncodi
ngapr
ogr
am.
1.Usethest
andardi
denti
fi
er sorr
eservedwor
ds.
2.Maketheprogr
am morereadablebyusingmeani
ngf
uli
dent
if
ier
s.
3.Don’
tusesimil
arv
ari
ables.
4.Keepspel
l
ingsasnormal aspossi
ble.
-18-
5.Usecomment stoexplainvar
iabl
es&procedur
es.Thi
smakestheprogr
am r
eadable.
6.Avoidt r
icks–writ
et heprogr
am usi
ngstr
aight
for
wardcodest
hatpeopl
ecanreadi
l
y
understand.
7.Modul ari
zeyourprogram.
Sampl
epr
ogr
amswr
it
teni
nPascall
anguage.
Exampl
e1:
Devel
opaprogram codethatwoul
dbeusedt
osol
vet
heequat
ionofast
rai
ghtl
i
ne
gi
venbytheexpressi
on:Y=mx+c
Pr
ogr
am St
rai
ghLi
ne(
input
,out
put
);
VAR
y,
m,x,
c:I
NTEGER;
BEGI
N
Writ
eln(
‘I
nputt
hev
alueofM’
);
Readln(
M);
Writ
eln(
‘I
nputt
hev
alueofX’
);
Readln(
X);
Writ
eln(
‘I
nputt
hev
alueofC’
);
Readln(
C);
Y:
=(m *x)+c;
Wr
it
eln(
‘Thev
alueofyi
s:’
,Y)
;
END.
Pascalcode Expl
anat
ion
Pr
ogr
am St
rai
ght
Line(
input
,out
put
); Thisist hepr ogr am Header .
Thewor d“ Progr am”i ndi catesthebegi nni ngof
theprogr am whosenamei sSt r
ai ght Line.
The( i
nput ,out put)st atement sshowst hatt he
program expect ssomei nputfrom t heKey board
anddi spl ayt heout putont heScr een.
VAR VARi sshor tformf orVar i
able.Av ar i
ablei sa
l
ocat i
onf ordat aint hecomput ermemor y.
Thisst atementt ellst hecomput ert hatv ariables
areaboutt obedecl ared.Whenav ari
abl ei s
declared, thecomput erset sasidesomememor y
spacet ost or eav aluei nt hev ariabl e.
y
,m,
x,c:
INTEGER; Fourv ariabl esoft y peI ntegerhav ebeendecl ared.
Thismeanst hat ,thememor yspacest hatwi l
lbe
setasidecanonl yhol dv aluesthatar ewhol e
number s.
BEGI
N TheBegi nst atementmar ksthest ar toft he
program body .St at ement sinthissect i
onar e
executedbyt hecomput er .E.g.,execut ionst arts
byaski ngt heusert oi nputt hev al ueofm.
Wr
it
eln(
‘I
nputt
hev
alueofM’
); TheWr itelnst atementdi splayswhat ev eris
betweent hei nvert edcommasi nt hebr acket s.
Thest atement swi llbesentt ot hescr eenexact l
y
thewayt heyappeari nt hebr acket s.Thi si s
-19-
because; thei nv er t
edcommasar emeantt o
maket heout putr eadabl eont hescr een.
Todi spl aythev al uehel di nav ari
ableont he
screen, r
emov et hei nvert edcommasandwr i
te
thenameoft hev ariablei nt hebr ackets, e.g.,
Wr i
teln( y)wi ll displ ayt hev aluehel dint he
vari
abl ey .
Readl
n(M)
; TheReadorReadl nst atementr eadsav alueand
storesi tinav ariabl e.
Whent hepr ogr am i sr unni ng, aRead/ Readl n
statementi nt hecodewi lldispl ay
sbl i
nkingcur sor
thatindi catest ot heuserwher etotypet hei nput.
Y:
=(m *x)+c; Calcul atesthev al ueofy .i nPascal ,thesy mbol ‘
:
=’iscal ledtheAssi gnmentst atement .
Thev al uesont her ightar ecal culatedt hent he
answerst oredi nt hev ariableywhi chi sont heleft
oftheassi gnmentsy mbol .
Wr
it
eln(
‘Thev
alueofyi
s:’
,Y)
; TheWr it
elndi spl ayst hev aluehel di nthev ari
able
yont hescr een.
Not e.Yi snotwi thint hei nv ertedcommas.
END. The‘ END. ’st at ementshowst heendofa
program.
Exampl
e2:
Pr
ogr
am Ar
eaCi
rcl
e(i
nput
,out
put
);
CONST
Pi=3.
142;
VAR
Radi
us,
Area:
REAL;
BEGI
N
Writ
eln(
‘Ent
erther
adi
us’
);
Readln(
Radius)
;
Ar
ea:
=Pi
*Radi
us*Radi
us;
Wr
it
eln(
‘TheAr
eai
s’
,Ar
ea)
;
END.
Pascalcode Expl
anati
on
Pr
ogr
am Ar
eaCi
rcl
e(i
nput
,out
put
); TheHeaderoft
hepr
ogr
am.
Thest atement sin()showst hattheuserinputs
datav iaKey boardandt heprogram displ
ay
i
nfor mat i
onont heScr een.
CONST Aconst anthasbeendecl ar
edwi t
hanamePi and
Pi=3. 142; value3. 142.
VAR Variabl eswi thfr
actionalpartshavebeen
Radi
us, Area: REAL; declared.
BEGIN Mar kst hebegi nningoft heprogram body.
Writ
eln(‘Entertheradi
us’
); Display sont hescreenthest r
ingbetweent he
i
nv ertedcommas.
Readl
n(Radi
us)
; Display sabl inki
ngcur sorthattel
lstheuserthat
ani nputi sneededbef oretheprogram can
-20-
continue.
Ar
ea:
=Pi
*Radi
us*Radi
us; Calculatest
heArea.Anassignmentst
atement(
:
=)hasbeenused.
Wri
tel
n(‘
TheAr
eai
s’
,Ar
ea)
; Displaystheval
uestor
edinthevar
iabl
eArea.
END. Mar kstheendoftheprogr
am.
Rev
isi
onQuest
ions.
1.St
atet
her
ulesf
oll
owedi
ncodi
ngapr
ogr
am.
Pr
ogr
am Test
ingandDebuggi
ng
Aft
erdesi
gning&codi
ng,
theprogram hastobet
est
edt
over
if
ythati
tiscor
rect
,and
anyer
rorsdetect
edr
emoved(debugged).
TESTI
NG:
Test
ingist
hepr
ocessofrunningcomputersoft
war
etodet
ect
/fi
ndanyer
ror
s(orbugs)
i
ntheprogr
am t
hatmighthavegoneunnoticed.
Dur
ingpr
ogr
am t
est
ing,
thef
oll
owi
ngdet
ail
sshoul
dbechecked;
Thereportsgener atedbyt hesyst
em.
Thefi
lesmai ntai
nedi nconnecti
ontothesyst
em’
sinf
ormat
ionr
equi
rement
s.
Thei
nputt othesy stem.
Theprocessingt asks.
Thecontrolsincorpor at
edwi t
hinthesyst
em.
Note.Thet
est
ingprocessisaconti
nuousprocess,anditendsonlywhenthe
Pr
ogrammer&theotherpersonneli
nvol
vedaresati
sfiedthatwhenoperat
ional
,the
pr
ogram wi
l
lmeettheobjecti
vesandthegrowingdemandsoft heorgani
zati
on.
Ty
pesofpr
ogr
am er
ror
s
Thereare5mai ntypesoferr
orsthatcanbeencount
eredwhent
est
ingapr
ogr
am.
Theseare:
1.Sy nt
axerrors.
2.Run-time(Executi
on)err
ors.
3.Logical(
arithmeti
c)err
ors.
4.Semant i
cer ror
s.
5.Lexiconerrors.
Syntaxer rors
Everyprogr ammi ngl anguagehasawel l-
definedsetofr ulesconcer ningf ormal spelli
ngs,
punctuations,nami ngofv ar
iables, et
c.Thei nstructionsar eaccept edonl yinaspeci fied
for
m &andmustbeobey edbyt hepr ogrammer .
Syntaxer rorsarether efore,progr ammi nger ror
s/mi stakest hatoccuri fthegr ammat ical
rul
esofapar t
icul
arlanguagear enotusedcor r
ectly
.
Exampl es:
(i
). Punct uationmi stakes,i.e.
,ift heprogrammerdoesnotuset herightpunct uations&
spacesneededbyt hetransl atorprogram, e.g.,omi tt
ingacommaorasemi colon.
(i
i)
.I mpr opernami ngofv ariabl
es.
(i
ii
).Wr ongspel li
ngsofuserdef inedandr eservedwor ds.
Reser vedwor dsar et hosewor dsthathav easpeci almeani ngt othepr ogrammi ng
language, andshoul dnotbeusedbyt hepr ogrammerf orany t
hingel se.
Syntaxer rorsarecommi t
tedbyt hepr ogrammerwhendev el
oping,ort ranscri
bingt he
-21-
program,andcanbedetectedbyt
helanguaget
ransl
ator
s,suchast
heCompil
erasi
t
attemptstotr
ansl
ateaprogram.Sucherror
smustbecorrect
edbythepr
ogr
ammer
beforethepr
ogram r
uns.
Logi
cal(
ari
thmet
ic)er
ror
s.
Theseareer r
orsi ntheprogram logi
c.
Logi
calerrorsrelatetothelogi
cofpr ocessingfoll
owedi nt
hepr ogram togetthe
desi
redresults.E.g.,t
heymayoccurasar esul
tofmi suseoflogicaloperator
s.
Logi
calerrorscannotbedet ectedbyt hetr
anslator.Theprogrammerwi l
l det
ectt
hem
whenthepr ogram result
sar eproduced.
Theprogram wi l
lrun,butgivethewrongout putorstopduringexecution.
Run-
ti
me(
Execut
ion)er
ror
s.
Theseerr
orsoccurduri
ngprogr
am executi
on.
Run-t
ime(execut
ion)er
ror
soccurwhent heprogrammeri
ntr
oducesnewf
eat
uresi
nthe
progr
am,whicharenotpartoft
hetransl
ator
’sstandar
ds.
Forexampl e;theymayoccuri f:
(i
). Thecomput erisaskedt odivi
deanumberbyzer o.
(i
i)
.Thenumbergener atedasar esultofaninst r
uctionistool
argetofi
tinamemory
l
ocation.
(i
ii
).Wheny our aiseanumbert oav erybigpowert hatcannotbeaccommodatedi
nthe
Register’
sst ructur
eoft hecomput er
.
(i
v).Incaseofacl osedloopinthepr ogram,leadingtoasetofinstr
uct
ionsbei
ng
executedr epet i
ti
vel
yf oralongt i
me.
Execut
ionerr
orsarenotdetectedbyt het
ranslat
orpr
ograms,butar
edetect
edbythe
computerduri
ngexecuti
on.Somet i
mes,executi
onerr
orsmayleadtoprematur
eendof
aprogram.
Todetectandeli
minateExecutionerr
ors,
at estr
unshouldbeperf
ormedonthe
progr
am aft
erithasbeentranslat
ed.
Semant
icer
ror
s.
Thesear emeani ngerror
s.Theyoccurwhent heprogr
ammerdevelopsstatements,
whichar enotprojecti
ngtowardsthedesiredgoal.Suchstat
ement
swi l
lcreate
deviat
ionsfrom thedesiredobject
ives.
Semant icer
rorsar enotdetect
edbyt hecomputer.Theprogrammerdetectsthem when
theprogram resultsareproduced.
Exampl e;
a).I
FGP>=1500OR2200THEN
TAX:=GP-( GP*13%)
b)
.IFGP>=1500ANDGP<=2200THEN
TAX:=GP-( GP*13%)
I
nt he1stst
atement,
iftheselect
ionisbetween1500&2200, thecomputerwi
l
lpi
ckonl
y
1500&2200, andtheothervalueswill
notbet ouched.
I
nt he2ndstat
ement,t
hecomput erwi
llbeabletopickal
lthev
aluesbet
ween1500&
2200becauseofthe‘AND’ operator
.
Lexi
coner
ror
s.
Thesearetheer
rors,whi
choccurasar
esul
tofmi
susi
ngReser
vedwor
ds(
wor
ds
reser
vedforapart
icul
arl
anguage)
.
-22-
Rev
isi
onQuest
ions.
1.Statethethreetypesoferrorsthatcanbeexperi
encedinpr
ogr
am test
ing,andhow
eachcanbedet ected.
2.Syntaxerror
scanbedet ectedbythehelpoftr
anslat
orswhi
lel
ogi
calerror
sar e
detecteddif
ferentl
y.Explai
nFIVEmet hodswhichcanbeusedtodetectLogical
error
s.
DEBUGGI
NG:
Thet
erm Bugi
susedt
oref
ert
oaner
rori
nacomput
erpr
ogr
am.
Mostprogr
ammi
nger
ror
sof
tenr
emai
nundet
ect
edunt
ilanat
tempti
smadet
otr
ansl
ate
apr
ogram.
Themostcommoner ror
si ncl
ude:-
Improperl
ydeclar
edConst ant
sandVar
iabl
es.
Aref
erencetoundecl
aredv ar
iabl
e.
Incor
rectpunct
uati
on.
Debuggingist
heref
ore,t
heprocessofdet
ecti
ng,
locati
ng&cor
rect
ing(r
emovi
ng,
el
iminat
ing)al
ler
ror
s( mi
stakesorbugs)t
hatmayexisti
nacomputerpr
ogr
am.
TYPESOFTESTI
NG(
Met
hodsofer
rordet
ect
ion)
Fortheprogr
am tobeassumedascorr
ect
,sever
alt
est
ingneedst
obeconduct
edbyt
he
programmertoascer
tai
n/est
abl
i
shthei
rval
idi
ty.
Ther
ear esev er
almethodsoft esti
ngapr
ogr
am f
orer
ror
s.Thesei
ncl
ude:
1.Dryrunni ng(Deskchecki ng).
2.Translatorsystem checking.
3.Functional t
esti
ng.
4.UseofTestdat a.
5.Useofdebuggi ngutil
i
ties.
6.Diagnost i
cprocedures.
7.System testwithactual data.
Dr
yRunning(Deskchecking):
Dr
yrunni
ngi
samet hodofchecki
ngapr
ogr
am f
orer
ror
sbymaki
ngt
hecor
rect
ionson
apaperbef or eenteri
ngitint hepr ogram edit
or.
I
tinvolvesgoi ngthrought hepr ogram whi l
estil
lonpaperv erif
ying&v ali
dati
ngit
s
possibl
er esults.Ifthefinal resultsagreewi t
ht heor i
ginaltestdataused, t
he
programmercant hentypet hepr ogram i
ntothecomput erandt ransl
ateit
.
√ Dryrunni nghelpst hepr ogr ammert oidenti
fyt heprogram instruct
ions,det
ectthe
mostobv ioussy ntaxandl ogicalerror
s,&t hepossi bleoutput.
√ Dryrunni ngismuchf ast er.Thi sisbecause; itinv
olvestheuseofhumanbr ai
nast he
pr
ocessor ,whichhasgotawel li
nbuil
tcommonsense.
Translatorsystem checki ng:
Thisisat ypeoft est
ing,whichinvol
vesthecomput er&t hetransl
atorprograms.
Afterenteri
ngthepr ogram, i
tischeckedusingatranslatortodetectanysyntaxerr
ors.
Thet r
anslatorcanbeaCompi l
eroranI nt
erpret
er,whichgoest hroughthesetof
i
nst r
ucti
ons&pr oducesal i
stoferrors,
oraprogram/ statementli
stingwhichisfr
ee
fr
om er r
ors.
Funct
ionalt
est
ing(
Whi
te-
boxt
est
ing)
:
-23-
Thi
stypeoftesti
ngi
sbaseduponexamini
ngtheinter
nalst
ruct
ureofaprogr
am &
sel
ect
ingtestdat
a,whi
chgi
veriset
othealt
ernat
ivecasesofcontr
olf
low.
UseofTestdata.
Theaccur
acyofaprogr
am canbetestedbyi
nputt
ingasetofval
uesr
efer
redt
oasTest
dat
a.Thetestdat
aisdesi
gnedtoproducepr
edict
ableout
put.
Therear
e2t ypesoftestdata;
(a)
.Realdata(l
ivedata):
-testdat
aobt
ainedf
rom t
her
eal
probl
em env
ironment
(pr
acti
calappli
cati
ons).
(
b).Dummydat
a:-assumedt
estdat
a.
Theprogrammeri nv entssi
mpl etestdata, whichhe/ sheusestocar ryouttr
ialrunsof
thenewpr ogram.Ateachr un,theprogrammerent ersvar
iousdatav ari
ati
onsi ncl
uding
datawit
her rorst
ot esthowt hesy st
em wi llbehav e.Forexample, i
ftheinputrequir
edis
ofnumerictype,thepr ogr
ammermayent eralphabeticcharact
ers.Thepr ogrammerwi ll
thencompar etheout putproducedwi t
ht hepr edicted(act
ual)output.
Not
es.
Wher epossibl
e,t
hepr ogr
am shouldbetestedusi
ngt
hesametestdatat
hatwas
usedf ordeskchecking.Morestr
ict/r
igi
dtest
sshoul
dbeappl
i
edont heprogr
am i
n
ordertotesttheprogram t
oit
slimits.
Onl
yLogicaler
ror
s&Semant
icer
ror
scanbecor
rect
edbyt
hepr
ogr
ammerusi
ng
t
estdat
a.
Agoodpr ogram shouldnotcrashduetoincor
rectdataentr
ybutshouldi
nfor
mthe
userabouttheir
regulari
tyandrequestf
orthecorrectdat
atobeentered.
Useofdebuggi ngutili
ti
es.
Afterthepr ogr
am hasbeenenteredint
hepr ogr
am edi
tor,debuggi
ngutil
i
tieswhichar
e
builtinthecomputercanber unduri
ngtransl
ati
ontodetectanysyntaxerr
orsinthe
program.
Theer rorsarecor
rectedandthedebuggingprocessi
srepeatedagaintofindoutmore
errors,beforet
heprogram i
sexecuted.
Diagnost i
cpr ocedures.
Forcompl expr ogr
ams, di
agnost
icprocedur
es,suchasTracer
out
ines,
maybeusedto
fi
ndl ogi
cal er
rors.
ATr aceprintsouttheresul
tsateachprocessi
ngsteptoenabl
eer
rorst
obedet
ected
quickly.
Sy
stem Testwi
thact
ualdat
a.
Thisiswherebythenewprogr
am isr
uninpar
all
elwit
htheexi
sti
ngsystem f
orashor
t
ti
mesot hatresul
tscanbecomparedandadj
ustmentsmade.Insuchcases,t
he
system t
estismadeusingact
ualdat
a.
Rev
iewQuest
ions.
1.Di
ff
erent
iatebetweenTest
ingandDebuggi
ng.
2.WhatisDryrunni
ng?
-24-
I
mpl
ement
ati
onandMai
ntenance.
I
MPLEMENTATI
ON
I
mplementat
ionr
efer
stot
heact
ual
del
i
ver
y,i
nst
all
ati
onandput
ti
ngoft
henewpr
ogr
am
i
ntouse.
Theprogr
am i
sputintouseaf
teri
tisful
l
ytest
ed,wel
ldocument
ed,
andaf
tert
rai
ning
thest
affwhowil
lbeinvol
vedi
ntherunni
ngofthenewprogram.
St
ruct
uredWal
kThr
ough:
I
tisanor
ganizedst
yleofeval
uati
ng/
revi
ewingapr
ogram byat
eam ofot
her
pr
ogrammers,whi
chthenreport
stotheprogr
ammingteam.
REVI
EW ANDMAI
NTENANCE.
Oncetheprogram becomesoperat
ional
,i
tshouldbemaint
ainedthr
oughouti
tsli
fe,
i.
e.,
newrouti
nesshouldbeadded,obsolet
erouti
nesremoved,
&t heexi
stingr
out
ines
adj
ustedsothattheprogr
am mayadaptt oenhancedf
unct
ionalenv
ironment
s.
Themainobj
ecti
veofmaintenancei
stokeept
hesy
stem f
unct
ioni
ngatanaccept
abl
e
l
evel.
Progr
am mai
ntenancemainl
yinvol
ves:
-
√ Correct
ingerr
orsthatmaybeencount er
edaft
ert
heprogram hasbeeni
mplement
ed
orexposedtoextensiveuse.
√ Changingprocedures.
√ Hardwareandsof t
waremai nt
enance.
√ Changingparametersandalgori
thmsusedt odev
eloptheori
ginal
progr
ams.
√ Makinganyadjustmentsasnewt echnol
ogycomes.
Note.Progr
am maintenancerunsparal
l
eltothemaint
enanceofthepr
ogram
documentat
ion,
i.
e.,anytimemaint
enanceiscarr
iedoutonthepr
ogram,t
he
documentat
ionshouldalsobeupdatedtoconveyther
ighti
mageofthesyst
em.
Pr
ogr
am document
ati
on.
Afterwri
ting,
test
ing,
anddebuggi
ngaprogram,
itmustbedocumented.I
not herwor
ds,
theprogrammershoulddescr
ibeal
lwhathewasdoi
ngduringthepr
ogram devel
opment
stages.
Pr
ogr
am documentat
ioni
sthewri
ti
ngofsupport
ivemater
ial
sexplai
ninghowt
he
pr
ogr
am canbeusedbyuser
s,i
nst
all
edbyoperator
s,ormodif
iedbyother
pr
ogr
ammer s.
Note.Allt
heprogr
am devel
opmentacti
vi
ti
es( i
.e.
,fr
om theinit
ial
stageupt othe
complet
eprogr
am)shouldbedocumented/recordedinordertoassistinthe
devel
opmentofthepr
ogram,fut
uremodifi
cati
onoft heprogram,gener almai
ntenance,
machine&soft
wareconversi
onatalat
erdate,andprogram changeov er.
Documentat
ioncaneit
herbe;
Int
ernalorExt
er nal.
I
nter
naldocumentat
ionist
hewri
tingofnon-executabl
eli
nes(comments)i
nthesour
ce
pr
ogram t
hathelpot
herprogr
ammer stounder st
andthecodestat
ements.
-25-
Ext
ernaldocument
ati
onr
efer
stor
efer
encemat
eri
alssuchasusermanual
spr
int
edas
bookl
ets.
Ty
pesofpr
ogr
am document
ati
on.
Ther
ear
e3t
argetgr
oupsf
oranyt
ypeofdocument
ati
on:
1.User-
ori
enteddocumentati
on.
Thi
senablestheusert
olear
nhowt ousethepr
ogr
am asqui
ckl
yaspossi
ble,
and
wit
hli
ttl
ehelpfr
om thepr
ogram dev
eloper
.
2.Oper
ator
-or
ient
eddocument
ati
on:
Thisi
smeantforcomputeroper
ator
ssuchast
het
echni
cal
staf
f.I
tisusedt
ohel
p
them i
nst
all
&maintai
ntheprogram.
3.Pr
ogr
ammer
-or
ient
eddocument
ati
on:
Thisisadetai
l
eddocument
ati
onwri
tt
enforski
ll
edpr
ogrammers.Itpr
ovi
dest
he
necessar
ytechni
cal
inf
ormat
iont
ohelpi
nfutur
emodifi
cat
ionoft
heprogr
am.
Somedocument
susedi
npr
ogr
am document
ati
on.
(
1). Userguide/manual .
Thisi
samanual prov
idedforanend-usert
oenabl
ehim/
heruseoroper
atet
he
progr
am wit
hmi ni
mal ornogui
dance.
AUserguideisusedinuser-
ori
ent
eddocumentati
on.
(
2). Ref erenceguide.
I
tisusedbysomeonewhoal r
eadyknowshowt ouset
heprogram butneedst
obe
remindedaboutapar t
icul
arpointorobt
ainmor
edetai
l
edinfor
mationabouta
part
icularfeat
ure.
(
3). Qui ckRefer
encegui
de.
Thiscouldbeasingl
esheetorcar
dsmallenoughtofi
tintoapocket.Iti
susedby
theusertogethel
pfort
hecommont askscarri
edoutwithi
ntheprogram.
(
4). Technicalmanuals.
Theyareint
endedforSyst
em anal
yst
s&Progr
ammer
s.Theyassi
sti
nmai
ntai
ning&
modif
yingtheprogr
am desi
gnandcode.
Cont
ent
sinapr
ogr
am document
.
Document ationi ncl udes:
1.Ti t
leoft hepr ogr am.
2.Funct ionoft hepr ogram.
3.Languageused.
4.Har dwar e&Sof twar erequir
edt osuppor tthepr ocessi
ngofthesy st
em.
5.Fi l
especi fi
cat ions( detail
soft hedatast r
uct uresused,&detail
sofhowdat
af i
l
esare
tobeor gani zed, accessed,andkeptsecur e) .
6.Li mitati
onsoft hepr ogram.
7.For matoft hei nput&t heout putexpected.
8.Desi gnoft hepr ogram usingt hedesigntool s(i.
e.,
detai
ledalgor
it
hms&procedur
es
used) .
9.Al isti
ngoft heSour ceprogram andthepr ogr am fl
owcharts.
10.Acar eful l
ydev isedsetofTestdat a,andat ableofexpectedresul
ts.
11.Detailedi nstruct ionsonhowt orunthepr ogram.
-26-
Rev
iewQuest
ions.
1.Whati sprogram desi gning?
2.(a).Defi
neprogr am document at
ion.
(b).Whatdoesapr ogram document ati
oncont
ain?
3.Bri
ef l
yexplai
nhoweachoft hefoll
owingdocumentsar euseful
inprogr
ammi
ng?
(a).Usermanual /guide.
(b).Refer
encegui de.
(c).Quickref
er encegui de.
4.Program document ati
oni sdif
ferentfr
om I
mplement at
ion.Expl
ain.
5.Out l
i
neandbr ieflyexplainthestagesinvol
vedi
npr ogram devel
opment.
DEVELOPI
NGOFALGORI
THMS
Aft
ercar
eful
l
yanalyzi
ngt
her
equi
rement
sspeci
fi
cat
ion,
thepr
ogr
ammerusual
l
ycomes
upwit
hthealgor
it
hm.
Def
ini
ti
onofanAl
gor
it
hm:
AnAlgor
it
hm i
sali
mit
ednumberofl
ogi
cal
stepst
hatapr
ogr
am f
oll
owsi
nor
dert
o
sol
veaprobl
em.
Ast
ep-by
-step(asetof
)inst
ruct
ionswhi
chwhenf
oll
owedwi
l
lpr
oduceasol
uti
ont
o
agi
venproblem.
√ Algor
it
hmstakeli
tt
leornoaccountoftheprogr
ammi nglanguage.
√ Theymustbepreci
se/accur
ate,unambiguous/
clearandshouldguar
ant
eeasol
uti
on.
Pr
ogr
am desi
gnTool
s.
Al
gor
ithmscanbei l
l
ust
rat
edusi
ngt
hef
oll
owi
ngt
ool
s:
1)
.Pseudocodes.
2)
.Fl
owcharts.
3)
.Decisi
onTables.
4)
.Decisi
onTrees.
Not
e.Foranygiv
enprobl
em,
thepr
ogr
ammermustchoosewhi
chal
gor
it
hm (
met
hod)
i
sbestsui
tedt
osolv
eit.
PSEUDOCODES.
Apseudocodeisamet hodofdocumentingaprogram l
ogi
cinwhichEngli
sh-
li
ke
statement
sareusedt
odescr i
betheprocessi
ngsteps.
Thesearestr
uctur
edEngli
sh-l
i
kephrasesthati
ndicat
etheprogr
am stepstobe
foll
owedtosolv
eagivenproblem.
√ Thet
erm“
Code”usual
l
yref
erst
oacomput
erpr
ogr
am.Thi
simpl
i
est
hat
,someof
thewordsusedinapseudocodemaybedr awnfrom acertai
nprogr
ammi ng
l
anguageandt henmixedwithEngli
shtoform st
ructur
edstatement
st hatareeasi
ly
under
stoodbynon- pr
ogrammers,andalsomakeal otofsensetoprogrammer s.
However,pseudocodesarenotexecut
ablebyacomput er
.
Gui
del
inesf
ordesi
gni
ngagoodpseudocode.
1.Thestatement
smustbeshort
,cl
earandreadabl
e.
2.Thestatement
smustnothavemorethanonemeaning(i.
e.,
shoul
dnotbe
ambiguous)
.
3.Thepseudocodel
i
nesshouldbecl
earl
youtli
nedandindent
ed.
4.ApseudocodemusthaveaBeginandanend.
-27-
i
.e.
,apseudocodeshoul dshowcl
earl
ythestar
tandst
opofexecut
abl
estat
ement
s
andthecontrolst
ruct
ures.
5.Theinput,
outputandprocessi
ngstat
ementsshoul
dbecl
earl
ystat
edusi
ng
keywordssuchasPRI NT,READ,I
NPUT,etc.
Exampl
e1:
Wri
teapseudocodet
hatcanbeusedt
opr
ompttheusertoent
ertwonumbers,
cal
cul
atet
hesum andaverageoft
het
wonumber
sandt hendi
spl
aytheout
putont
he
scr
een.
START
PRINT“Ent
ertwonumber
s”
I
NPUTX, Y
Sum =X+Y
Average=Sum/2
PRINTSum
PRINTAverage
STOP
Exampl
e2:
Wri
teastructuredalgori
thm t
hatwoul
dpromptt
heusertoent
ert
heLengt
handWi
dth
ofarect
angle,calcul
atetheAreaandPer
imet
er,
thendi
splayt
her
esul
t.
Solut
ion
Step1:Drawt
her
ect
angl
eofLengt
h(L)andWi
dth(
W).
St
ep2:Wr
it
edownt
hePseudocode.
START
PRINT“EnterLengt
handWi
dth”
READL, W
Area=L*W
Peri
meter=2( L+W)
PRINTArea
PRINTPerimeter
STOP
Exampl
e3:
Wri
teapseudocodet
hatcanbeusedtocal
cul
atetheDi
amet
er,
Cir
cumf
erenceandAr
ea
ofaci
rcl
eandthendi
splayt
heoutputont
hescreen.
START
Setπt o3.14
Promptt heuserfort
heRadius(
R)
Stor
et heradiusi
nav ar
iabl
e(R)
SetDiamet erto2*Radi
us
-28-
SetCi
rcumf er
encetoπ*2*Radi
us
SetAreatoπ*Sqr( Radi
us)
PRINTDiamet er
PRINTCircumf er
ence
PRINTArea
STOP
Exampl
e4:
Wr
it
eapseudocodef
orapr
ogr
am t
hatwoul
dbeusedt
osol
veequat
ion:E=MC2.
START
Enterval
uesf
rom M t
oC
E=M *C*C
Displ
ayE
STOP
√ I
tisi
mpor
tantt
ousepr
ogr
am cont
rol
str
uct
ureswhenwr
it
ingPseudocodes.The
mostcommonconst r
uctsare:
(
i)
. Loopi ng(Repeti
ti
on/I t
erat
ion)–usedwhereinst
ruct
ionsaretoberepeated
undercertai
ncondit
ions.
(
ii
). Select
ion–usedwhenchoosi ngaspeci
fi
edgroupofinst
ruct
ionsforexecut
ion.
Thegr oupchosendependsoncertai
ncondit
ionsbei
ngsatisf
ied.
Exampl
e5:
Wri
teapseudocodeforapr
ogram thatcanbeusedtocl
assi
fypeopl
eaccor
dingt
oage.
I
fapersoni
smor ethan20years;
output“Adul
t”el
seout
put“
Youngperson”
.
START
PRINT“Ent
ertheAge”
I
NPUTAge
I
FAge>20THEN
PRINT“Adult
”
ELSE
PRINT“Youngper
son”
STOP
Not e.Pseudocodesmakeanalgor
it
hm easi
ertounder
stand.Thisi
sbecause;
the
algori
thm canbereadf
rom t
optobottom wit
houtt
heneedf orj
umpi
ngbackwardsor
forwardstofol
lowthel
ogicoft
healgori
thm asi
nfl
owcharts.
FLOWCHARTS.
AFl
owchar
tisadi
agr
ammat
icorpi
ctor
ial
repr
esent
ati
onofapr
ogr
am’
sal
gor
it
hm.
I
tisachar
tthatdemonst
rat
est
hel
ogi
cal
sequenceofev
ent
sthatmustbe
per
for
medtosolveaprobl
em.
Ty
pesofFl
owchar
ts.
-29-
Ther
ear
e2commont
ypesofFl
owchar
ts:
1)
.System flowchart.
ASy stem fl
owchar ti
sagraphi
calmodelthatil
l
ustr
ateseachbasi
cst
epofadat
a
processingsystem.
Iti
ll
ustrates(i
nsummar y)thesequenceofeventsi
nasy st
em,showi
ngt
he
departmentorf uncti
onr
esponsibl
eforeachevent.
2)
.Program f
lowchart.
Thisisadiagr
am thatdescribes,i
nsequence,al
ltheoper
ati
onsrequi
redtoprocess
datainacomput erprogr
am.
Apr ogr
am fl
owchartgraphicall
yrepr
esentsthety
pesofinst
ruct
ionscont
ainedina
computerprogram aswellast hei
rsequence&logic.
PROGRAM FLOWCHARTS.
AFl owcharti
sconst r
uctedusingasetofspecialshapes(orsy
mbols)t
hathavespecifi
c
meani ng.Symbolsar eusedtorepr
esentoperati
ons,ordatafl
owonaf l
owchart
.
Eachsy mbolcontainsinformati
on(
shortt
ext)thatdescri
beswhatmustbedoneatt hat
point.
Thesy mbolsarejoi
nedbyar r
owstoobtai
nacompl eteFl
owchart
.Thearrowsshowt he
orderinwhichtheinstructi
onmustbeexecuted.
SYMBOLSUSEDI
NPROGRAM FLOWCHARTS.
Bel
owisastandardsetofsymbolsusedtodr
awpr
ogr
am f
lowchar
tsascr
eat
edby
Ameri
canNationalSt
andardInst
it
ute(ANSI
).
1.Ter
minalsy
mbol .
El
l
ipse(
Oval
inshape)
I
tisusedt
oindi
cat
ethepoi
ntatwhi
chaf
lowchar
t,apr
ocessoranal
gor
it
hm begi
ns
&ends.
√ AllFl
owchartsmusthaveaSTART&STOPsy mbol.TheSTART/ BEGI
Nsymbolis
thefi
rstsymbolofafl
owchart
,&identi
fi
est
hepointatwhichtheanal
ysi
softhe
fl
owchartshoul
dbegin.TheSTOP/ ENDsymboli
sthelastsymbolofafl
owchart
,
&indicat
estheendofthefl
owchart.
√ ThewordsBegi
n&End(
orSt
art&St
op)shoul
dbei
nser
tedi
ntheTer
minal
symbol
.
2.I
nputorOut
putsy
mbol
.
Par
( all
elogr
am)
-I
tisusedt
oident
if
y/speci
fyani
nputoper
ati
onorout
putoper
ati
on.
Forexampl
e;
READEmpl oyee PRINTEmpl oyee
Name Name
Inputoper
ati
on Outputoperat
ion
Note.Thewor dsmost l
yassoci
atedwit
hI/Ooper
ationsareREAD&PRI NT.READ
descr
ibestheentryofcomputerdat
a,whil
ePRINTrelat
estothepr i
ntedout
putof
i
nformati
on.
3.Pr
ocesssy
mbol
.
-30-
Rect
( angl
e)
-Processsymbolisusedt oindicat
ethataprocessi
ngordat
atransformati
oni
s
t
aki
ngpl ace.
Theinf or
mat
ionplacedwithi
nt heprocesssy
mbol maybeanalgebraicfor
mulaora
sentencetodescr
ibeprocessing.
NO Rhombus)
(
YES
-I
tisusedtoi ndi
cate/speci
fyacondit
ionortoshowt hedeci
siontobemade.
Therear
e2mai ncomponent sofaDecisi
onsymbol :
(i
). Aquestionaskedwi t
hintheDeci
sionsymbol,t
hatindi
cat
est hecompari
son/
l
ogicaloperat
ion.
(i
i)
.Ther esultsofthecompar i
son(
whicharegiveninter
msofYESorNO) .
Thearr
owslabel
edYESorNOl
eadt
other
equi
redact
ioncor
respondi
ngt
othe
answertot
hequest
ion.
5.Fl
owl
ines.
Fl
owl i
neswitharrowheadsareusedtoindi
catethedirecti
onofprocessi
ngofthe
pr
ogram logi
c,i
.e.
,theyshowtheorderinwhichtheinstr
ucti
onsaretobeexecuted.
Thenormal f
lowofaf l
owcharti
sfrom ToptoBot t
om, andLeftt
oRight.
Note.Flowli
nesshouldnevercrosseachother.
6.Connect
orsy
mbol
.
-31-
1.Af l
owchar tshoul dhav eonl yoneent ry/
st artingpointandoneexi tpoi
nt(i
.e.
, ensur
e
thatthef l
owchar thasal ogical star
tandf inish).
2.Thef l
owchar tshoul dbecl ear,neatandeasyt ofoll
ow.
3.Uset hecor r
ectsy mbol ateachst ageint hef l
owchar t
.
4.Thef l
owchar tshoul dnotbeopent omor et hanonei nter
pret
ation.
5.Avoidov erl
appi ngt hel i
nesusedt oshowt hef l
owofl ogi
cast hiscancreate
confusionint hef l
owchar t
.
6.Makecompar isoni nstructionssi mpl e,i
.e.,capabl eofYES/NOanswer s.
7.Thelogical f
lowshoul dbecl earlyshownusi ngarrows.
Note.Af l
owchar tshoul df lowf rom theTopt oBot t
om ofapage, andfrom theLeft
totheRi ght.
8.Wher enecessar y
, useConnect orstor educet henumberoff lowl i
nes.
Connect orsar ehel pfulwhenaf lowchar tissev er
alpageslong,andwher esev er
al
l
oopsar eneededi nt hel ogicoft heflowchar t.
9.Checkt oensur ethatt hef lowchar tislogical l
ycor r
ect&compl ete.
Exampl
e1:
Drawaf l
owchartf
oraprogram t
hatcanbeusedt
opromptt
heusertoentert
wo
numbers,fi
ndthesum andaver
ageofthetwonumber
sandthendi
splayt
heoutputon
thescr
een.
Star
X,
Y
Sum =X+Y
Aver
age=
Sum/2
PRINTSum,
Average
St
o
Exampl
e2:
Dr
awaflowchartf
oraprogram t
hatwoul
dpromptt
heusert
oentert
heLengt
hand
Wi
dthofarect
angle,
cal
culatet
heAreaandPeri
met
er,
thendi
spl
ayther
esult
.
St
ar
L,
W
Area=LxW
Peri
meter=2(
Lx
W)
PRI
NTArea,
Per
imet
er
-32-
St
o
Exampl
e3:
Dr
awaf l
owchartf
orapr
ogr
am thatcanbeusedt
ocalcul
atet
heDi
ameter,
Ci
rcumf
erenceandAr
eaofaci
rcleandthendi
spl
ayt
heoutputont
hescreen.
St
ar
SET
π=
Radi
us
Di
amet
er=2xRadius
Cir
cumf
erence=πx2x
Radi
us
Area=πxSqr(Radi
us)
Di
ameter,
Ci
rcumfer
ence,
Area
St
o
Exampl
e4:
Desi
gnaflowchar
tforapr
ogram t
hatcanbeusedtoclassi
fypeopleaccordi
ngt
oage.
I
fapersonismorethan20year
s;out
put“Adul
t”el
seoutput“Youngperson”.
St
ar
Age
-33-
Age> Young
No
Yes
Exampl
e5:
Drawaf lowchartforaprogram t
hatwouldbeusedt
oclassi
fyani
malsaccor
dingtosex.
I
fal et
terM isinput,t
heprogram shoul
ddispl
ay‘
Mal
e’other
wisei
tshoul
ddisplay
“Female”.
St
ar
Ent
erLet
ter
Yes
Let
ter= Mal
e
No
Femal
e
St
o
Exampl
e6:
Writ
eapr ogr
am usi
ngafl
owchar
ttoconv
ertt
emper
atur
efom 0Ct
r o0F.
Fahrenhei
t=32+(9oxC/
5).
Star
READTemp.
noC
i
St
oreval
ue
in
memory
Cal
cul
ateFahr
enhei
t
F=32+( 5*oC
9/ )
-34-
Not
es.
AflowchartmusthaveaStartandanend.
Aflowcharti
suseful
whent healgori
thm isshort&thef
lowchar
tcanfi
tconv
eni
ent
ly
onasinglepage.Ift
hefl
owchar ti
st oolarge,
itisr
ecommendedtouse
Pseudocodesforl
ong&compl icatedprograms.
Adv
ant
agesofusi
ngFl
owchar
ts.
Thef
oll
owi
ngar
ethebenef
it
sofFl
owchar
ts:
(
i)
. Quickerunderst
andi
ngofrelat
ionships.
Theyassistpr
ogrammerstounderstandproceduresmorequickl
y.
Apr ogr
ammercanr epr
esentalengthyprocedur
emor eeasi
l
ywi t
hthehel
pofa
fl
owchartthandescr
ibi
ngitbymeansofwr i
tt
ennotes.
(
ii
). Ef
fect
ivesynt
hesi
s.
Fl
owchart
smaybeusedaswor
kingmodel
sint
hedesi
gnofnewpr
ogr
amsand
sy
stems.
(
ii
i)
.Pr operprogr am document at
ion.
Program fl
owchar tsser v
easgoodpr ogram document
ati
on,whichi sneededf
or
thefoll
owingr easons:
(
a).Ifprogramsar emodi fi
edinfutur
e,thefl
owchartswil
ldi
recttheprogrammer
onwhatwasor iginal
lydone.
(
b).Whenst affchangesoccur ,thefl
owchartsmayhelpnewempl oyees
understandt heexi st
ingprograms.
(
c).Flowchar tsassistinprogram conversi
onwhennewhar dware/ sof
twarear
e
acquired.
(
iv)
.Ef fecti
vecodi
ng.
Program fl
owchar
tsactasagui deduri
ngtheprogr
am pr
epar
ati
onst
age.
I
nst r
ucti
onscodedinapr ogr
ammi ngl
anguagemaybecheckedagai
nstt
he
fl
owchar tt
oensur
et hatnostepsareomit
ted.
(
v). Order
lydebuggi
ngandtesti
ngofprograms.
Fl
owchartshel
pindet
ecti
ng,l
ocati
ngandr emovi
ngmistakes.
Theprogr
ammercanr ef
ertothefl
owchartashe/sher
e-checkst
hecodi
ngst
eps,
&thelogi
cofthewri
tt
eninstr
ucti
ons.
(
vi)
.Effici
entprogram mai
ntenance.
Fl
owchartsfacil
i
tat
ethemaintenanceofoperati
ngprograms.Theyhel
pthe
pr
ogrammert oconcent
rateonthepartoftheinfor
mationfl
owwhichistobe
-35-
modi
fi
ed.
Li
mit
ati
onsofusi
ngFl
owchar
ts.
(
i)
. Fl owchar tsar ecomplex,clumsy&becomeuncl ear,especi
all
ywhent heprogram
l
ogici scompl ex.
(
ii
).I fchangesar etobemade, thefl
owchar tmayr equirecompletere-drawing.
(
ii
i)
.Repr oduct ionoff l
owchar t
sisusuallyapr oblem, sincethefl
owchar tsymbols
cannotbet yped.
(
iv)
.Nouni f
orm pr act
icei
sf oll
owedf ordrawingf lowchar t
sasitisusedasanai dt o
thepr ogram.
(
v). Somet i
mes, i
tbecomesdi ffi
cul
ttoest abl
isht helinkbetweenv ar
iouscondi t
ions,
andt heact ionstobet akenuponapar ti
cularcondi ti
on.
Rev
isi
onExer
cise.
1.Defi
net hefol
lowing:
(a)
.Al gori
thm.
(b)
.Pseudocode.
(c)
.Flowchar t.
2.(a)
.Statethev ar
iousty
pesoffl
owchar
ts.
(b)
.Discusstheadv ant
agesanddi
sadvant
agesoff
lowchar
ts.
PROBLEM SOLVI
NG
1.Pr
obl
em I
dent
if
icat
ion(
probl
em r
ecogni
ti
on)
.
Wri
teapr ogram which:
(
a).Requeststheusert oenteratemperatureinoC.
(
b).Cal
cul at
esthecor r
espondingtemperatureinoF.
(
c).Outputsthegiventemper at
ureandtheconv ertedv
alue.
2.Pr
obl
em def
ini
ti
on&Pr
obl
em Anal
ysi
s.
-Det erminet hegeneral
r equi
rements,
i.
e.,
themaininputst
otheprogr
am,themai
n
out
put sfrom theprogram, &alsot
hekindoffi
l
eswhi chmaybeneeded.
Fi
ndouthowt oconv
ertthegiventemperat
ure.I
fthegivent
emperatur
ei noC,
si t
hen
o
convertitto F.
F=32+( 9oC/ 5)
-TheKeyboar
dwil
lbeusedt
oent
ert
heCent
igr
adet
emper
atur
e,anddi
spl
ayt
he
out
putontheScr
een.
3.Desi
gnt
hepr
ogr
am.
Dev
elopanAl
gor
it
hm (
amet
hod)f
orsol
vi
ngt
hepr
obl
em.
AnAlgor
it
hm i
sasetofi
nst
ruct
ionswhi
chwhenf
oll
owedwi
l
lpr
oduceasol
uti
ont
o
agi
venprobl
em.
-Wri
tet
heinst
ruct
ionsinsuchawayt
hatt
heycanbeeasi
l
yconv
ert
edi
ntoaf
orm
whi
chthecomputercanfol
low.
Comput erinstruct
ionsfalli
nto3mai ncategori
es:
(a)
.Inputinst r
ucti
ons–usedf orsuppl
yingdat atoapr ogram insidethecomputer
.
Thedat asuppl iedisstor
edinthememor yoft hecomput er.
(b)
.Processi nginstructi
ons–usedf ormani pulat
ingdatainsidet hecomputer
.
Thesei nstruct
ionsallowust oAdd,Subtract,Mult
iply,
&Di vide.Theyalsoall
ow
-36-
ust
ocomparetwoval
ues,andactaccordi
ngtotheresul
toft
hecompar
ison.
(
c).Out
puti
nst
ruct
ions–usedtogetinfor
mationoutofthecomput
er.
Note.Theprogr
ammermustchoosewhi
chal
gori
thm (
method)i
sthebestsui
tedt
o
sol
veit
.Thismayinv
olv
edrawingaFl
owchar
torwri
ti
ngPseudocode.
Al
gor
it
hm.
STEP1: [
Promptt
heusert
oent
ert
emper
atur
einoC]
STEP2: [
Stor
ethev
aluei
nmemor
y]
STEP3: [
Cal
cul
atet
hecor
respondi
ngt
emper
atur
einoF]
STEP4: [
Stor
ether
esul
tinmemor
ylocat
ion]
STEP5: [
Out
putt
hev
al noC&oF]
uesi
STEP6: [
Stop]
Afterthealgor
ithm isdev eloped,i
tmustbecheckedbyuseofappr opr
iat
edata
valuestomakesur eitisdoi ngit
sjobcorr
ect l
y.Thisprocessiscall
edDryrunningor
Deskchecki ngtheal gori
thm, &isusedtopi n-
pointanyerror
sinlogicbef
orethe
program isactuall
ywr it
ten.
Not e.Youshoul dnev erstartwrit
ingpr
ogrammi ngcodesunl essyouareabsolutel
y
surethatthealgorit
hm i scor r
ect.
-Al gori
thmsdonotdependonanypar ti
cularlanguage.
Fl
owchar
t.
Flowchartsareusefulf
orspecif
yingsmal
lal
gori
thms.
Af l
owchartconsist
sofasetof‘ f
lowchar
tsymbols’
connectedbyarr
ows.Each
symbol contai
nsinf
ormationaboutwhatmustbedoneatt hatpoi
nt&thear
rows
showt heorderinwhichtheinst
ructi
onsmustbeexecut
ed.
BEGI
N
Promptt
heuser
toent
erTemp.
noC
i
St
oret
heval
ue
in
memory
Cal
cul
ateFahr
enhei
t
F=32+( 5*oC)
9/
Out
putthe
t
emp.inoC&oF
END
4.Wr
it
eacomput
erpr
ogr
am cor
respondi
ngt
otheal
gor
it
hm.
-37-
-Chooseasui
tablelanguage&wri
tethecomputerprogram usi
ngt
heal
gor
it
hm.
Usetheappr
opriat
elanguagest
atements&control
structur
eswhi
char
efoundi
n
thatpr
ogr
ammi nglanguage.
5.Testt
hepr
ogr
am t
ofi
ndoutwhet
heri
tisdoi
ngi
tsi
ntendedj
ob.
-Testi
nginvol
vessupply
ingdataval
ues(
Testdata)tothepr
ogr
am f
orwhi
chthe
answerisknown.Somev alueswecanuseare0,10,100.
Ift
heprogram doesnotgi
veusthecorr
ectanswers,t
henweknowthepr
ogr
am
contai
nssomeer ror
s.
6.Debugt
hepr
ogr
am.
I
.e.
,fi
nd&cor
rectanyer
ror
sint
hepr
ogr
am.
7.Documentt
hepr
ogr
am.
Wr i
teoutanexpl anationofhowt heprogram wor
ks,andhowtouseit.Thisi
ncludes:
(i
). Thest atementoft hepr obl
em.
(i
i). TheAlgori
thm f orsol v
ingtheproblem.
(i
ii
).Thepr ogram listing.
(i
v).Testdat a,andt her esult
sproducedbytheprogram.
Not e.Document ati
onshoul dbedoneatt hesametimeasallt
heotheracti
vi
ties.
Eachactivi
typroducesi tsowni t
emsofdocument at
ionwhichwil
lbepartofthefi
nal
program document ation.
.Us
1) erdocument ati
on( Usergui de)
Itenabl
esanon- t
echnicalper
sontouset heprogr
am withoutt
heneedtoknow
abouttheint
ernalworkingsoftheprogr
am.
Userguidesareintendedtohelptheusertousetheprogram (
tooper
atei
t)wi
th
mi ni
malornoguidance.
Exampl
e1:
Assumet
hatt
hepr
ogr
am i
scal
l
edTEMPCONV,
andr
esi
desonadi
sknamed
CONVERT.
Pr
ogr
am Name:
TempConv
Machi
ne:
Thi
spr
ogr
am i
sdesi
gnedf
oruseonI
BM Mi
crocomput
ers.
Pur
pose:
o
Thispr
ogr am si
mulatestheconversionoft emperatur
ei n Ctothecorr
esponding
o
temp.i
n F.( Att
achadescr i
pti
onofhowt hegamei splayed)
.
Thestrat
egyusedi sfortheprogr
am t orequesttheusert oenteratemperat
urein
o
C.Theuserspecifiesthi
stemperature,andt heprogram cal
culat
esthe
o
corr
espondingtemper at
urein F,
printi
ngt hegiventemper at
ureandthe
conver
tedv al
ue.
Locat
ion:
Ont
hedi
sknamedCONVERT
Howt
oUse:
St
artPASCALindri
veC:
I
nsertt
hediskCONVERTint
othedi
skdr
ive.
-38-
Pr
essALT+‘F’
toopentheFi
lemenu,
thenchooseOpen.
Ty
peA:TEMPCONV,thenpr
essENTER
I
nput
:
Theprogram r
equest
stheusertoenteratemp.inoC.Whenthemessage
“ENTERTEMP.I NDEGREESCENTI GRADE”appearsonthescr
een, y
oumustt y
pe
theval
ueofthetemperat
ureyouwantconv er
ted.
Onlydigi
ts0to9,apossi
blesi
gn,andapossibledeci
malpoi
ntmustbet yped.
E.g.
,25,-
20,or30.
5.Typi
ng25C, f
orinst
ance,isi
nval
idandwil
lresul
tinanerr
or.
Out
put
:
Thepr
ogram wi
ll
printaheadingf
oll
owedbythetwot
emperat
ures.Forexampl
e,
i
f40isenter
edastheCentigr
adetemp.
,theout
putwi
l
lbe;
Centi
grade Fahrenhei
t
40 104
Exampl
e2:
Pr
ogr
am Name:
OneZer
o
Machi
ne:
Thi
spr
ogr
am i
sdesi
gnedf
oruseonanI
BM Mi
crocomput
er.
Pur
pose:
Thisprogram simulat
estheplayi
ngoft heOnez er
ogame.( Adescript
ionofhow
thegamei splay
edshouldbeat t
ached).
Thest rat
egyusedistoli
mitat urntoamax imum numberoft hr
ows.Theuser
specifi
est hi
smaximum (aswel lasthenumberoft ur
nstoplay),
andtheprogr
am
simulatesagame, pri
nti
ngthescor eobtained.
Thepr ogram canbeusedtoenabl etheusert osel
ectthemax imum numberof
throwst omakeoneacht urn,
inor dertoincreasethechancesofwinningan
actualgame.
Locat
ion:
Ont
hedi
sknamedGAMES
Howt
oUse:
St
ar tPASCALinyourcomput er
.
I
nser tt
hediskGAMESi ntothediskdr
ive.
ChooseOpenf rom theFil
emenu.
TypeA: OneZer
o,thenpressENTER
PressALT+‘ R’
Whent heprompt
: ‘
ENTERNUMBEROFTURNSPERGAME’ appear
s,t
ypet
he
numberoft ur
nsforwhi
chyouwantagamet
olast
.Thus,
ifyouwantt
hegame
toconsistof20t
urns,t
ype20.
Whent heprompt:‘
ENTERMAXIMUM THROWSPERTURN’ appears,
typea
number ,
say,5.
Theprogram wil
lsi
mulat
eonegameof20t ur
ns;eachtur
nwi l
lconsi
stofa
max i
mum of5t hr
ows.Wheniti
sfi
nished,
itwil
lpri
ntt
heresult
sasi nthe
fol
l
owi ngsample:
-39-
Thegameconsistedof20t
urns
Eachtur
nconsist
edofamaximum of5t
hrows
Thescoreobt
ainedfort
hegamewas156
Itwi
llthendispl
aytheprompt:ENTERMAXI MUM THROWSPERTURN.
Again,youcanenteranothernumber(ort
hesameone, ifyouwish)
,andthe
comput erwil
lsi
mulateanothergameof20t ur
ns,usi
ngthenewmax i
mum that
youhav egiv
en.Ifyoudon’twishtoconti
nue,youcanenter0atthi
sstageand
theprogram wil
lend.
Rest
ri
cti
ons:
I
fyouwishtovar
ythenumberofturnsinagame,theprogr
am hastoberer
un.
Youcantype:
ALT+‘R’
,andent
erther equi
rednumberwhentheprompt
:‘ENTER
NUMBEROFTURNSPERGAME’ appears.
.Te
2) chni
caldocument
ati
on(
Techni
calmanual
s)
Techni
calmanual
sarei
ntendedforthesyst
emsanaly
stsorprogr
ammers.
Thi
sdocumentati
oni
susefultoaprogrammer&canhelpthem i
nmai
ntenance&
modifi
cat
ionoft
hepr
ogram desi
gnandcodeatalaterst
age.
Exampl
e
Pr
ogr
am Name:
DiceGame
Pur
pose:
Thispr
ogram si
mul at
estheplayi
ngofadicegame.(Attachadescr
ipt
ionofhow
thegameisplayed).
Thestr
ategyusedistoli
mitat urnt
oamax imum numberofthr
ows.Theuser
speci
fi
esthi
smaxi mum (aswel l
asthenumberoftur
nst opl
ay)
,andtheprogr
am
simul
atesagame, pri
nti
ngthescoreobtai
ned.
Pr
ogr
am St
ruct
ure:
Theprogram consi
stsoft
hreemodul
es;
Main
Si
mulateOneGame
Si
mulateOneTurn
Thef
oll
owi
ngdi
agr
am showst
her
elat
ionshi
pbet
weent
hesemodul
es:
MAI
N
Si
mul
ateOneGam -40-
e
(Note.Heregiv
ethedocument
ationf
oreachindivi
dualmodule.The
documentati
ongiv
enisonl
yforthemoduleSi
mul at
eOneTurn.Therefor
e,
developt
hedocumentat
ionf
ortheothermodulesalongsi
milarli
nes)
.
Modul
eName: Si
mul
ateOneTur
n
Par
amet
ers: MaxThr
owsPer
Tur
n,Scor
eThi
sTur
n
Pur
pose:
GivenMaxThrowsPer
Tur
n,t
hismodulesi
mulat
esonet
urnandr
etur
ns(
in
ScoreThi
sTur
n),t
hescor
eobtai
nedfort
hatt
urn.
Var
iabl
esused:
MaxThr
owsPerTur
n -apar
ameterr
epr
esenti
ngt
hemaxi
mum numberof
t
hrowspert
urn.Thisv
aluei
ssuppl
iedt
othemodule.
Scor
eThi
sTur
n -apar
amet
erusedt
oret
urnt
hescor
efort
het
urnt
othe
cal
li
ngmodul
e.
Number OfThr
ows -usedtocountt
henumberoft
hrowsmade.I
fthe
countreachesthemaxi
mum,
thetur
nends.
ModulesCal l
ed:
Onl
ythest
andardmoduleRANDOM iscal
l
edtosimulat
ethet
hrowi
ngofthedi
ce.
RANDOM (
1,6)producesar
andom numberi
ntherange1to6,i
ncl
usi
ve.
TheAl
gor
it
hm:
MODULESi mul at
eOneTurn(MaxThr owsPerTur
n,ScoreThisTur
n)
SetScoreThisTurnt o0
SetNumber Of Throwsto0
WHILENumber OfThrows<Max ThrowsPerTurnDO
SetThrowVal uetoRANDOM ( 1,6)
Add1t oNumber OfThrows
I
FThr owValue=1THEN
SetScoreThisValuet o0
SetNumber Of ThrowstoMaxThr owsPerTurn { forceloopexit
}
ELSE
AddThr owVal uetoScoreThi
sTurn
ENDIF
ENDWHI LE
ENDMODULE
Expl
anat
ionNot
es:
I
fa1isthr
own,thet
urnends.Int
hiscase,afor
cedexi
toftheWHI
LEl
oopi
s
made.Thi
sisdonebysett
ingNumberOfThrowstoMaxThr
owsPer
Tur
n.
Not
e.I
fthemodul
ewast
est
edi
ndi
vi
dual
l
y;apr
ogr
am l
i
sti
ng,
thet
estdat
aused,
-41-
andtheresul
tsobtainedshouldbeincl
udedint
hedocumentat
ionoft
hemodul
e.
Aft
ereachmodul ehasbeendocument ed,
sampl
erunsoft
heentir
eprogr
am
shoul
dbeadded.Thi sshoul
di ncl
ude:
•Thecompl eteprogram l
ist
ing.
•Testdataused.
•Result
sobt ai
ned.
Rev
iewExer
cise
1.Whyisdocument
ati
onanessentialpar
toftheprogr
am developmentpr
ocess?
2.Namefourit
emswhichtheuserdocumentati
onofaprogram mustcontai
n.
3.Whatit
emsmakeupt hetechni
caldocumentat
ionofaprogram?
PROGRAM CONTROLSTRUCTURES
Cont
rolst
ruct
uresarebl
ocksofstat
ementst
hatdeter
minehowprogram st
atements
aret
obeexecuted.
Cont
rolst
atementsdeal
withsit
uati
onswher
eprocessesar
etoberepeatedsever
al
numberofti
mesorwheredecisi
onshavetobemade.
Ther
ear
e3cont
rol
str
uct
uresusedi
nmostoft
hest
ruct
uredpr
ogr
ammi
ngl
anguages:
1)
.Sequence.
2)
.Select
ion.
3)
.It
erati
on(l
oopi
ng)
.
SEQUENCECONTROLSTRUCTURES
InSequencecont
rol,
thecomputerreadsinst
ruct
ionsf
rom apr
ogram fi
l
eline-
by-
li
ne
star
ti
ngfrom t
hefir
stli
nesequent
iall
ytowardstheendoft
hefi
le.Thisiscall
ed
Sequenti
alpr
ogram execut
ion.
St
art St
atement St
atement …t
St
a ement End
Note.Sequent
ialpr
ogram execut
ionenabl
esthecomput
ert
oper
for
mtaskst
hatar
e
ar
rangedconsecuti
vel
yoneaf t
eranotheri
nthecode.
SELECTI
ON(
DECI
SION)CONTROLSTRUCTURES
Sel
ect
ioni
nvol
veschoosi
ngaspeci
fi
edgr
oupofi
nst
ruct
ions/
stat
ement
sforexecut
ion.
I
nSel
ect
ioncont
rol
,oneormor
est
atement
sar
eusual
l
ysel
ect
edf
orexecut
ion
-42-
dependi
ngonwhetherthecondit
iongivenisTrueorFalse.
Thecondit
ionmustbeaBoolean( l
ogical
)expressi
on,e.g.
,X>=20
I
nt hi
scase,t
hecondit
ioni
strueifxisequaltoorgreaterthan20.Anyv
aluet
hati
sless
than20,wi
llmakethecondi
ti
onfalse.
Gener
all
y,t
hereare4typesofsel
ect
ioncont
rol
str
uct
uresusedi
nmosthi
gh-
lev
el
pr
ogramminglanguages:
1)
.IF–THEN
2)
.IF–THEN–ELSE
3)
.NestedIF
4)
.CASE–OF
Note.Thesecont r
ol str
ucturesareusedinapr ogram basedonasequenceof
i
nstruct
ions,whi
chr equir
et hatachoice(deci
sion)bemadebet weentwoormore
alt
ernat
ives.
Insuchasi t
uati
on,thecomput ermustbeprogrammedt ocomparedata,
andt
ake
acti
ondependingont heoutcomeoft hecompar i
son.
I
F–THEN
IF–THENstruct
urei
susedifonl
yoneoptioni
savail
able,i
.e.
,i
tisusedtoper
for
ma
cer
tai
nact
ionift
hecondi
ti
onistr
ue,butdoesnot
hingifthecondit
ioni
sfal
se.
Thegener
alf
ormatoft
heI
F-THENst
ruct
urei
s:
F<Condi
I ti
on>THEN
Pr
ogram st
atementt
obeexecut
edi
fcondi
ti
oni
str
ue;
ENDI
F
Ift
hecondit
ioni
sTRUE, t
heprogram execut
esthepartfoll
owi
ngthekeyword‘
THEN’.I
f
thecondi
ti
onisFALSE,t
hestatementpartofthest
ructureisi
gnor
ed,andthepr
ogr
am
conti
nueswitht
hestat
ementsbelowt heENDIF.
Thedi
agr
ammat
icexpr
essi
onoft
heI
F-THENst
ruct
urei
s:
FALSE
Condi
ti
o
TRUE
Executest
atement
s
betweenTHEN&ENDIF
Cont
inuat
ionofpr
ogr
am
Exampl
e1;
I
naschool ,
theadmi
nist
rat
ionmaydeci
detorewardonl
ythosest
udent
swhoat
tai
na
meanmar kof80%andabov e.
Flowchart
Pseudocode
I
FMar
k>80THEN
Pr
int“Gi
ver
ewar
d” Mar Yes
ENDI
F k>
-43-
Pr
intr
ewar
d
No
Exampl
e2;
Auserisaskedtoent
erasetofposit
ivenumber
s,oneatati
me.Sheent
ersa0(
zer
o)
t
oindicat
ethatshehasnomor enumberstoent
er.
Devel
opanal gor
it
hm t
opri
ntthelar
gestnumberent
ered.
Pseudocode
START
Prompttheuserf
oranumber,Lar
gest
Prompttheuserf
oranot
hernumber,NewNumber
FNewNumber>Lar
I gestTHEN
SetLar
gestt
oNewNumber
ENDIF
Prompttheuserforanumber ,
NewNumber
Output(
‘Thel
argestnumberent
eredi
s’
,Lar
gest
)
STOP
Aseachnumberi senter
ed,theal
gorit
hm checksi
fthenumberent
eredislargerthan
thepr
ev i
ousones.Ifi
tislar
ger,i
tissavedasthel
argest
.Ifi
tissmall
er,i
tisignored,
andholdsontothelar
gestnumbersof ar.
Exampl
e3;
PROGRAM AgeTal
k(I
nput
,Out
put
);
VARAge:
INTEGER;
BEGI
N{pr
ogr
am}
Wr
it
eln(
‘Howoldareyou?’
);Readl
n(Age)
;
IFAge>=18THEN
Writ
eln(
‘Youareoldenought
ojoint
hear
my.
’
);
END.{
progr
am}
Not
e.Compoundst
atement
scanal
sobeusedwi
tht
heI
F–THENst
ruct
ure.
Exampl
e4;
PROGRAM Ser
vice; {*
Thi
sprogr
am di
spl
aysamessagedependi
ngonthe
numberofy
ear
syouhaveworkedf
oracompany*}
VARYear
s:I
NTEGER;
BEGI
N
CLRSCR
Wr
it
eln(
‘Howl
onghav
eyoubeenwi
tht
hecompany
?’)
;Readl
n(Year
s);
I
FYears>20THEN
Wri
tel
n(‘
GetaGol
dwat
ch’
);
I
F(Years>10)AND( Years<=20)THEN
Wri
tel
n(‘GetaPaperweight
’)
;
-44-
I
FYears<=10THEN
Wri
tel
n(‘
Getapatont
heback’
);
END.
I
F–THEN-
ELSE
TheI
F-THEN-
ELSEst
ruct
urei
ssui
tabl
ewhent
her
ear
e2av
ail
abl
eopt
ionst
osel
ectf
rom.
Thegener
alf
ormatoft
heI
F-THEN-
ELSEst
ruct
urei
s:
F<Condi
I ti
on>THEN
St
atement1; (
cal
l
edt
heTHENpar
t)
ELSE
St
atement2; (
cal
l
edt
heELSEpar
t)
ENDI
F (
indi
cat
est
heendoft
hecont
rol
str
uct
ure)
Thedi
agr
ammat
icexpr
essi
onoft
heI
F-THEN-
ELSEst
ruct
urei
s:
TRUE FALSE
Condi
ti
o
Executest
atement
s Executest
atement
s
betweenTHEN&ELSE betweenELSE&ENDIF
Execut
est
atement
s
aft
erENDI
F
Cont
inuat
ionofpr
ogr
am
Whent
heI
F-THEN-
ELSEst
ruct
urei
sencount
ered:
1)
.TheConditi
onistest
ed.
2)
.Ift
heConditi
onisTRUE,thest
atement
sbet
weenTHEN&ELSE(
i.
e.,
theTHENpar
t)
areexecut
ed.
TheELSEpar
tisski
pped,
andexecut
ioncont
inueswi
tht
hest
atementf
oll
owi
ng
ENDI
F.
3).Ift
heCondi t
ionisFALSE,theTHENpar ti
sskipped.Thestat
ementsbetweenELSE
&ENDI F(i.
e.,t
heELSEpar toft
hestruct
ure)areexecut
ed,
andexecuti
oncontinues
withthestatementfoll
owingENDI
F.
Aftereit
hergroupofstatementshasbeenex ecuted,
thepr
ogram wi
l
lthenconti
nue
executi
ngt heprogram st
atementsaf
terthelastENDIF.
Not
e.Usi
ngI
F-THEN-
ELSE,
foranygi
vent
estoft
hecondi
ti
on,
onl
yonesetof
-45-
st
atement
sissel
ect
edf
orexecut
ion(
notbot
hst
atement
s).
Exampl
e1;
I
naf oot
bal
lmatch,i
faplay
ermakesami
stakewhichisconsi
deredser
iousbyt
her
ules
oft
hegame, he/
sheisgi
venaRedcar
d.Ot
herwise,he/
sheisgiv
enaYel l
owcar
d.
Fl
owchar
t
Pseudocode
I
FFaul
t=Ser
iousTHEN
Pr
int“Gi
ver
edcar
d” NoFaul
t Yes
ELSE =
Pr
int“Gi
veYel
l
owcar
d”
Pri
ntYel
l
ow Pri
ntRed
ENDI
F car
d car
d
Exampl
e2;
Wri
teanalgor
it
hm whi
chasksauserf
ort
wonumber
s;AandB,andcal
culat
estheval
ue
ofAdiv
idedbyB.However
,ifBi
s0,amessagei
spr
int
edwhi
chsaysthatdi
visi
onby0
i
snotall
owed.
Pseudocode
START
Prompttheuserf
ort
het
wonumber
s,AandB
FB=0THEN
I
Wri
tel
n(‘
Div
isi
onby0i
snotal
l
owed’
)ELSE
SetCtoA/B
ENDIF
Out
putA,B,
andC
STOP
Expl
anat
ion.
Supposetheuserent er
s1f orAand0forBinresponsetothepr
ompt .
Thealgor
ithm wil
ltesti
fB=0.SinceBis0,thecondit
ioni
sTrue.Theref
ore,t
he
THENparti sexecutedpri
nti
ngthemessage:‘
Divi
sionby0isnotal
lowed’.
Supposetheuserenter
s20forAand5forBinresponsetotheprompt.
Thealgor
ithm wi
l
ltesti
fB=0.Si
nceBisnot0,thecondi
tionisFal
se.Ther
efore,t
he
st
atementsbetweenELSE&ENDIFareexecut
ed( i
.e.
,Aisdiv
idedbyB,andtheresul
t
i
sstoredinC).
NESTEDI
F
NestedIFst
ruct
urei
susedwher
e2ormor
eopt
ionshav
etobeconsi
der
edt
omakea
sel
ecti
on.
Thegener
alf
ormatoft
heNest
edI
Fst
ruct
urei
s:
F<Condi
I ti
on1>THEN
St
atement1
-46-
ELSE
F<Condi
I ti
on2>THEN
St
atement2
ELSE
F<Condi
I ti
on3>THEN
St
atement3
ELSE
St
atement4;
ENDI
F
ENDIF
ENDI
F
Exampl
e;
I
nanOl
ympi
cst
rackev
ent
,medal
sar
eawar
dedonl
ytot
hef
ir
stt
hreeat
hlet
esas
f
oll
ows:
a)
.Posi
ti
on1:
Goldmedal
b)
.Posi
ti
on2:
Sil
vermedal
c)
.Posi
ti
on3:
Bronzemedal
Thepseudocodeandf
lowchar
tbel
owcanbeusedt
oshowt
hest
ruct
ureoft
heNest
ed
I
Fselect
ion.
Pseudocode
I
FPosi
ti
on=1THEN
Medal
=“Gol
d”
ELSE
I
FPosi
ti
on=2THEN
Medal
=“Si
l
ver
”
ELSE
I
FPosi
ti
on=3THEN
Medal
=“Br
onze”
ELSE
Medal
=“ni
l
”
ENDI
F
ENDI
F
ENDI
F
Fl
owchar
t
No No No
Posi
ti Posi
ti Posi
ti
on1 on2 -47- on3
Yes Yes Yes
WhenI Fst
atementsar
eembeddedwit
hinoneanother
,theyaresai
dtobeNested.
Note.EachIF-
THENorIF-THEN-
ELSEi
ster
minatedwit
ht hecomment{ENDI
F}.The
numberof{EndIf
’s}mustbeequal
tot
henumberofELSE’ s.
TheCASEst
ruct
ure
CASE- OFallowsapar t
icul
argr
oupofstat
ementstobechosenf
rom sev
eral
avai
l
abl
e
groups.
Iti
st her
eforeusedwher ether
esponsetoaquest
ioni
nvol
vesmorethantwo
choices/al
ternat
ives.
Thegener
alf
ormatoft
heCASEst
ruct
urei
s:
CASEExpr
essi
onOF
Label1:st
atement1
Label2:st
atement2
Label3:st
atement3
.
.
.
Labeln:st
atementn
ELSE
St
atementm
ENDCASE
√ TheBooleanexpr
essionf
ortheCASEstr
uct
urecanonl
ybeexpr ngI
essedusi nteger
s
oral
phabeti
ccharact
ersonl
y.Hence;
CASEInt
egerOF orCASECharOF
√ Astat
ementisexecutedonl
yifoneofi
tscorrespondi
nglabel
smat chesthecur
rent
val
ueoftheexpressi
on.Thi
simpli
esthat,
thecurrentval
ueoftheexpressi
on
det
ermineswhichofthest
atementswi
llbeexecuted.
Exampl
e1;
Wri
teapseudocodeofapr
ogr
am thatrequest
stheusert
otypeanumberf
rom 1t
o7.
Thepr
ogram t
henpri
ntst
hecor
respondingdayoftheweek.
Pseudocode
START
Pr
ompttheuserf
oranumberf
rom 1t
o7,
Day
CASEDayOF
-48-
1:
Wri
tel
n(‘
Sunday
’);
2:
Wri
tel
n(‘
Monday
’);
3:
Wri
tel
n(‘
Tuesday
’);
4:
Wri
tel
n(‘
Wednesday
’);
5:
Wri
tel
n(‘
Thur
sday
’);
6:
Wri
tel
n(‘
Fri
day
’);
7:
Wri
tel
n(‘
Sat
urday
’);
ENDCASE
STOP
TheCASEst
ruct
ureconsi
stsof
:
ThewordCASE.
AControl
vari
able(e.
g. ,
Day).
ThewordOF.
Agroupofoneormor est at
ements,
eachgroupl
abeledbyoneormor
epossi
ble
val
uesofthecontr
olv ari
abl
e.
ThewordENDCASE, indicat
ingt
heendoftheconst
ruct.
WhenaCASEst atementisencount
er ed,
thevalueofthecont
rolvari
abl
eisusedt o
determi
newhichgroupofstat
ement sisexecuted,
e.g.
,i
ftheval
ueofDayi s5,t
hent he
groupofst
atementslabel
ed5isselectedforexecuti
on,andt
hest at
ement;‘
Thursday’i
s
pri
nted.
Aft
erexecut
ingt
hisgr
oupofst
atement
s,execut
ioncont
inuesatt
hest
atement
fol
l
owingENDCASE.
NOTES:
i
). Theprogrammershouldensur
ethattheval
ueofthecontr
olv
ari
abl
eappear
sasa
l
abel.E.g.
,supposet
hevalueent
eredforDaywas9.Since9doesnotl
abelany
st
atementwithi
ntheCASEconstruct
,anerr
orwil
lresul
t.
i
i)
. Agiv
enlabelcanbeusedononlyonegroupofstat
ement
s.E.g.
,5can’
tbeusedto
l
abelt
wogr oupsofst
atement
s.Ifthi
sisdone,
thecomputerwi
l
lnotknowwhich
gr
ouptoselect&unpr
edict
abl
eresult
scanoccur.
Exampl
e2;
Writ
eapseudocodeofaprogram t
hatrequest
stheusert
oty
peanumberfr
om 1to7.
Dependi
ngonthenumberentered,
pri
ntthemessage,‘
Iti
saSchool
day
’or‘
Iti
sona
Weekend’
.
Pseudocode
Prompttheuserf
oranumberf
rom 1t
o7,
Day
I
F( Day<1)OR(Day>7)THEN
Pri
nt(
‘I
nval
i
dnumberenter
ed-
--
’,
Day
)
ELSE
CASEDayOF
2,
3,4,
5,6:
Wri
tel
n(‘
I
tisaSchool
day
’);
-49-
1,
7:Wr
it
eln(
‘I
tisonaWeekend’
);
ENDCASE
ENDI
F
STOP
Inthi
spseudocode, t
heIFstatementhasbeenusedtovali
dat
et heval
ueofDay.Thi
s
ensurest hat,onlyval
iddatagetsprocessedbyt
heCASEst at
ement.
Other
wi se,iftheELSEpar tisexecut
ed,wearesuret
hatthevalueofDaywil
ll
iebet
ween
1and7i nclusive.
Exampl
e3;
Pseudocode
CASEAv erageOF
80.
.100:
Grade=‘
A’
70.
.79:
Grade=‘
B’
60.
.69:
Grade=‘
C’
50.
.59:
Grade=‘
D’
40.
.49:
Grade=‘
E’
ELSE
Gr
ade=‘
F’
ENDCASE
Fl
owchar
t
Aver
a No AVG No AVG No AVG No
ge 70- 60- 50-
Yes Yes Yes Yes
Gr
ade=A Gr
ade=B Gr
ade=C Gr
ade=D Gr
ade=E
PRI
NTGr
ade
Exampl
e4;
PROGRAM CaseSampl
e(I
nput
,Out
put
);
VARGr
ade:
CHAR;
BEGI
N{Pr
ogr
am}
-50-
Wr
it
eln(
‘Whatgr
adedi
dyouget
?’)
;Readl
n(Gr
ade)
;
CASEGr
adeOF
‘
A’,
‘B’ :
Wri
tel
n(‘
Ver
yGood’
);
‘
C’:
Wri
tel
n(‘
Pass’
);
‘
D’,
‘F’ :
Wri
tel
n(‘
Wakeup’
);
End; {
Case}
Readl
n;
End. {
Progr
am}
I
TERATI
ON(
LOOPI
NG/REPETI
TION)CONTROLSTRUCTURES
Loopingr
eferstotherepeat
edexecut
ionofthesamesequenceofst
atementsto
processi
ndiv
idualdata.Thi
sisnormall
ycr
eatedbyanuncondi
ti
onalbranchbackt
oa
previ
ous/
earl
ieroperati
on.
Theloopi
sdesi
gnedtoexecut
ethesamegr
oupofst
atement
srepeat
edl
yunt
ilacer
tai
n
condi
ti
oni
ssati
sfi
ed.
Not
e.It
erat
ionisi
mpor
tanti
nsi
tuat
ionswher
ethesameoper
ati
onhast
obecar
ri
edout
onasetofdatamanyt
imes.
Theloopstr
uctur
econsi stsof2parts:
1).Loopbody,whichrepresentst
hestatement
stober
epeated.
2).Loopcontr
ol,
whi chspecifi
esthenumberofti
mest
heloopbodyi
stober
epeat
ed.
Typesofl
oops:
( .Condi
a) ti
onall
oop:-Thi
siswher
ether
equi
rednumberofr
epet
it
ionsi
snotknowni
n
adv
ance.
Pseudocode
STEP1: [
Promptt
heuserf
ort
emper
atur
einoC]
STEP2: [
Stor
ethev
aluei
nmemor
y]
STEP3: I
FC=0THENSt
op
o
STEP4: [
Calcul
atetemperat
urei
n F]
o
F:=32+(C*9/ 5)
o o
STEP5: [
Out
putt
emper
atur
ein C& F]
STEP6: [
GOTOSt
ep1]
Fl
owchar
t
START
Promptt
heuser
toent
erTemp.
noC
i
St
oret
heval
ue
Loop
in
memory
-51-
I
sC= STO
YES
NO
Thisal
gori
thm i
ll
ust
rat
esCondi ti
onalexecut
ion.Condi
ti
onalexecut
ionisa
si
tuati
onthatr
equir
esthatalogicalt
estbecarri
edout,
andthenapar t
icul
aract
ion
betakendependi
ngont heoutcomeoft hatt
est.
I
nthiscase,
goingtoStep4willdependonwhet hertheconditi
onisTrueorFal
se.
E.
g.,
IfC=10t henthecondi
ti
on‘ C=0’isFalse,
andt heprogram goestoStep4.
Buti
fC=0, thent
heconditi
onisTrue,andtheprogram stops.
(
b).Uncondi
ti
onalloop:-Thi
siswher
etheex
ecut
ionoft
hei
nst
ruct
ionsi
srepeat
ed
somespecif
iednumberoftimes.
(
c).Cont
inuous(
inf
ini
te/
unendi
ng)loop:-Thi
siswher
ethecomput
err
epeat
sa
pr
ocessagai
nandagain,wi
thoutendi
ng.
Exampl
e:
STEP1: [
Promptt
heuserf
ort
emper
atur
einoC]
STEP2: [
Stor
ethev
aluei
nmemor
y]
STEP3: [
Calcul
atetemperat
ur noF]
ei
o
F:=32+(C*9/ 5)
STEP4: [
Out
putt
emper
atur
einoC&oF]
STEP5: [
GOTOSt
ep1]
o
Aslongasanumberi senteredforC, t
healgori
thm doesnotstopwhenitr
eaches
STEP5butr at
hertransferscontr
oltoSTEP1, causi
ngt healgor
it
hm/pr
ocesstobe
repeated.
Howev er,
azero(0)canbeusedt ostoptheprogram because;t
hepr
ogram cannot
o
givetheFahr
enheitequi
v al
entto0 C.
Requi
rementsforloops:
1.Contr
olvar
iabl
e( Count
er)
:-i
ttel
l
s/i
nst
ruct
sthepr
ogr
am t
oexecut
easetof
stat
ementsanumberoft i
mes.
2.Ini
ti
ali
zat
ion:-al
locati
ngmemor yspace,
whichwi
l
lbeoccupi
edbytheoutput.
3.Incr
ementing:-i
ncreasi
ngthecontr
olvari
abl
ebyacer
tai
nnumberbefor
et henext
l
oop.
Gener
all
y,t
hereare3mai
nloopi
ngcont
rol
s:
1.TheWHI LEloop
2.TheREPEAT…UNTILloop.
3.TheFORl oop.
-52-
TheFORl
oop
TheFORloopisusedi
nsit
uat
ionswher
eexecut
ionoft
hechosenst
atement
shast
obe
repeat
edapredet
ermi
nednumberoft
imes.
Thegener
alf
ormatoft
heFORl
oopi
s:
FORl
oopv
ari
abl
e=Lowerl
i
mitTOUpperl
i
mitDO
St
atement
s;
ENDFOR
Thef
lowchar
text
ractf
oraFORl
oopt
hatcount
supwar
dsi
s:
Lowerl
i
mit=Loopv
ari
abl
e+
Loopv
ari
abl
e=Lowerl
i
mit St
atement
s
NO
Condi
ti
o
YES
Exampl
e;
Consideraprogr
am thatcanbeusedt
ocalcul
atethesum oft
ennumber sprov
idedby
theuser.The‘FOR’
loopcanbeusedtoprompttheusertoentert
het ennumber sat
most10t i
mes.Oncet henumbershav
ebeenent er
ed,t
heprogram calcul
atesand
displ
aystheaccumulatedsum.
Pseudocode Fl
owchar
t
FORcount=1TO10DO
PRI
NT“ Ent
eranumber(
N)”
Lowerl
i
mit=Count+1
Sum =Sum +N
ENDFOR
Count=Lowerl
i
mit Sum =Sum +
Di
spl
aySUM
Cou YES
nt<=
NO
Sum
Expl
anat
ion
-53-
1. Thel
oopv
ari
abl
e(Count
)isf
ir
sti
nit
ial
i
zed/
sett
otheLowerl
i
mitwhosev
aluei
s1.
2. Thelowerl
imiti
sthentestedagai
nstt
heUpperlimitwhosev al
ueissetat10.
3. Ift
helowerl
imiti
slessthanorequalt
o10,theprogram wil
lprompttheusert
o
enteranumberN,other
wisethecomputerwil
lexi
ttheloop.
4. Aft
erthel
astst
atementi
nthel
oophasbeenexecut
ed,thel
oopvar
iabl
e(count
)is
i
ncrement
edbya1andst or
edi
nthelowerl
i
mit,i
.e.
,Lowerl
imi
t=Count+1.
5. Thelowerl
imitisagai
ntest
ed,andifi
tislesst
hanorequalto10,
thel
oopi
s
repeat
edunti
ltheti
met hel
owerli
mitwill
equaltheupperl
i
mi t
.
NOTE:
TheFORl
oopcanal
sobeusedt
ocountdownwar
dsf
rom t
heupperl
i
mitt
othel
ower
l
imit
.
E.
g.,
FORcount=10DOWNTO1DO
I
nthi
scase,
theupperl
i
mit10i
stest
edagai
nstt
hel
owerl
i
mit1.
Pseudocodef
ora‘
FOR’
loopt
hatcount
sfr
om upperl
imi
tdownt
othel
owerl
imi
t:
FORl
oopv
ari
abl
e=Upperl
i
mitDOWNTOLowerl
i
mitDO
St
atement
s;
ENDFOR
Thef
lowchar
text
ractf
oraFORl
oopt
hatcount
sdownwar
dsi
s:
Upperl
i
mit=Loopv
ari
abl
e-1
Loopv
ari
abl
e=Upperl
i
mit St
atement
s
NO
Condi
ti
o
YES
TheWHI
LEl
oop
The‘WHILE’loopisusedi
facondi
ti
onhastobemetbef
orethest
atementswi
thi
nthe
l
oopar eexecuted.
E.g.
,towit
hdrawalmoneyusi
nganATM, acust
omermusthaveabalancei
nhi
s/her
account.
Ther
efor
e,i
tal
l
owst
hest
atement
st edzer
obeexecut oormanyt
imes.
Pseudocode Fl
owchar
t
WHI
LEBal
ance>0DO
Wit
hdrawcash
Updat
eaccount Withdr
aw
ENDWHI
LE cash
Update
YES
Bal
ance>
NO
-54-
Exi
tloop
Expl
anat
ion
1. Thecondi
tonbal
i ance>0i sfi
rstt
est
ed.
2. I
fiti
sTRUE,theaccountholderi
sall
owedtowit
hdr
awcash.
3. Theprogr
am exi
tsthelooponcethebal
ancefal
l
stozer
o.
Thegener
alr
epr
esent
ati
onoft
heWHI
LEl
oopi
s:
Pseudocodesegment Fl
owchar
text
ract
LECondi
WHI ti
onDO
St
atement
s;
ENDWHI
LE St
atement
s
TRUE
Condi
ti
o
FALSE
Exi
tloop
TheREPEAT…UNTI
Lloop
InREPEAT…UNTI
L,thecondi
ti
onist
est
edattheendoft
hel
oop.Ther
efor
e,i
tal
l
ows
stat
ementswi
thi
nittobeexecut
edatl
eastonce.
E.g.
,i
fREPEAT…UNTI
LisusedincaseoftheATM cashwithdr
awal,t
hecustomerwil
lbe
abletowithdr
awt
hecashatl
eastoncesi
nceavai
l
abili
tyofbalancei
stest
edattheend
oftheloop.
Pseudocode Fl
owchar
t
REPEAT
Wit
hdrawcash
Updat
eaccount Withdr
aw
cash
UNTI
Lbal
ance<=0; Update
Yes
Bal
ance>
No
Exi
tloop
Thegener
alf
ormatoft
heREPEAT…UNTI
Lloopi
s:
-55-
Pseudocodesegment Fl
owchar
text
ract
REPEAT
St
atement
s; Repe
LCondi
UNTI ti
on;
St
atement
s
Tr
ue
Condi
ti
o
False
Exi
tloop
DEVELOPI
NGCOMPLEXALGORI
THMS
Exampl
e1:
Wi
thai
dofapseudocodeandaf
lowchar
t,desi
gnanal
gor
it
hm t
hat
:
a)
.Pr
omptt
heusert
oent
ert
wonumber
sXandY.
b)
.Di
vi
deXbyY.Howev er
,ift
hev
alueofYi
s0,
thepr
ogr
am shoul
ddi
spl
ayaner
ror
message“
Err
or:
Div
isi
onbyzer
o”.
Pseudocode
START
PRINT“Ent
ert
wonumbersXandY”
I
NPUTX, Y
I
FY=0THEN
PRI
NT“Err
or:
Div
isi
onbyz
ero”
ELSE
-56-
Quot
ient=X/
Y
PRI
NTX,
Y,Quot
ient
ENDI
F
STOP
Fl
owchar
t
St
ar
X,
Yes
I
sY= Er
ror
:Di
vi
sionby
No 0
Quot
ient=X/
Y
X,Y,
Quot
ient
St
o
Exampl
e2:
I
nanat
hlet
icscompet
it
ion,
anat
hlet
eisr
ewar
dedasf
oll
ows:
1stposi
ti
on:Gold
2ndposit
ion:
Silv
er
3rdposi
ti
on:Bronze
Drawapseudocodeandafl
owchar
tforaprogram t
hatwoul
dbeusedt
odet
ermi
net
he
ty
peofmedal t
oberewar
dedtoeachathl
ete.
Pseudocode
START
PRI
NT“Ent
erat
hlet
eNameandPosi
ti
on”
I
NPUTName,Posit
ion
I
FPosi
ti
on=1THEN
Medal
=“Gol
d”
ELSE
I
FPosi
ti
on=2THEN
Medal
=“Si
l
ver
”
ELSE
I
FPosi
ti
on=3THEN
Medal
=“Br
onze”
ELSE
-57-
Medal
=“None”
ENDI
F
ENDI
F
ENDI
F
Fl
owchar
t
St
ar
Name,
Posi
ti
on
Repeat
Posi
ti No Posi
ti No Posi
ti No
on1? on2? on3?
Yes Yes Yes
Medal
= Medal
=Si
l
ver Medal
= Medal
=“None”
Gol
d Br
onze
PRI
NTName,
Posi
ti
on,
Medal
No
Exi
t
Yes
St
o
Exampl
e3:
Theclasst eacherofForm 3Si nasecondar yschool r
equestedaprogr
ammert odesi
gn
forherasi mpleprogram thatwouldhelpherdot hefoll
owing:
(a)Enterthenamesofst udentsandmar ksobt ai
nedin8subj ect
s–Mat hemati
cs,
English,Ki
swahil
i,Bi
ology ,Chemist
ry,Businessstudies,Comput
erstudi
es,
and
History.
(b)Afterenteri
ngthemar kf oreachsubject,t
hepr ogram shoul
dcal
cul
atethetot
aland
averagemar ksforeachst udent.
(c)Dependi ngontheAv eragemar kobtained,theprogram shoul
dassi
gngradeas
f
oll
ows:
(
i) Bet
ween80and100–A
(
ii
) Bet
ween70and79 –B
(
ii
i)Bet
ween60and69 –C
(
iv)Bet
ween50and59 –D
(
v) Bel
ow50 –E
(
d)Thepr
ogr
am shoul
dthendi
spl
ayeachst
udent
’sName,
Tot
almar
ksandt
heAv
erage
-58-
gr
ade.
Usi
ngapseudocodeandaf
lowchar
t,wr
it
eanal
gor
it
hm t
hatshowst
hedesi
gnoft
he
pr
ogram.
Pseudocode
START
REPEAT
PRI
NT“Enterst
udentNameandsubjectmarks”
I
NPUTStudentname,Maths,
Eng,
Kisw, Bi
o,Chem,
Busi
ness,
Comput
er,
Hist
ory
SUM =Mat
hs+Eng+Ki
sw+Bi
o+Chem +Busi
ness+Comput
er+Hi
stor
y
AVG=SUM/
8
I
F(AVG=>80)AND(AVG<=100)THEN
Grade=“A”
ELSE
I
F(AVG=>70)AND(AVG<=79)THEN
Grade=“B”
ELSE
I
F(AVG=>60)AND(AVG<=69)THEN
Grade=“C”
ELSE
I
F(AVG=>50)AND(AVG<=59)THEN
Grade=“D”
ELSE
Gr
ade=“
E”
ENDI
F
ENDIF
ENDIF
ENDI
F
PRI
NTSt
udentname,
Sum,
AVG,
Grade
UNTI
LCount=Numberofst
udent
s
STOP
Fl
owchar
t
St
ar
ENTERSt udent
name, Maths,Eng,
Kisw,Bio,
Chem,
Business,
Comput er
,Histor
y
Repeat
SUM =Maths+Eng+Kisw
+Bio+Chem +Busi
ness+
Comput
er+Hist
ory -59-
No No No No
No
Yes
Exampl
e4:
Thegr osssalar
yofemployeesi
nZAGBOOKSENTERPRI SEisbasedonbasicsal
aryand
addit
ionalbenefi
tsasfol
l
ows:
(a)Empl oyeeswhohaveworkedfort
hecompanyformorethan10year
srecei
vean
additi
onalpayof10%tothei
rbasi
csal
ary.
(b)Mont hl
ysalar
ybonusbasedonmonthlysal
esofbooksasfol
lows:
Mont
hlysal
es BonusRate
(%)
Abov e500,
000 15
Between250,000and 10
500,000
Bel
ow250, 000 5
Drawaf l
owchar
tforaprogr
am thatwouldbeusedtocal
cul
atethegr
osssal
aryt
hen
outputeachempl
oyee’
sbasicsalar
y,gr
osssal
aryandall
benefi
ts.
St
ar
ENTERName,
Basi
c,Sal
es,
Year
s
Exper
ien Yes
Benef
it=Basi
cx0.
1
ce>10
-60-
Yes
No
Repeat
Yes No
Exampl
e5:
Aladydeposi
ts2,
000shi l
l
ingsinaMi cr
ofinancecompanyatani
nterestr
ateof20%per
annum.Attheendofeachy ear,t
heinterestearnedi
saddedtot
hedepositandthenew
amountbecomest hedepositforthatyear.
Writ
eapseudocodef oraprogram thatwoul dtr
ackthegr
owthofthedeposit
sovera
peri
odofsevenyears.
START
I
NPUTInit
ialDeposit
I
NPUTInterestRate
SETDeposittoIni
ti
aldeposi
t(i
.e.
,2000)
SETYeart
o0
WHI LEYear<=7DO
I
nterest=DepositxInt
erestr
ate
Total=Deposit+Int
erest
Deposit=Total {t
henewdeposi
t}
Year=Year+1
-61-
ENDWHI
LE
PRINTDeposi
t,Year
STOP
Exampl
e6:
Drawaflowchar
tforapr
ogram t
hatist
opromptforNnumbers,accumulat
et hesum
andthenfi
ndtheaver
age.Theoutputi
stheaccumul
atedt
otal
sandt heaverage.
BEGI
N
I
nit
ial
i
zeSum =0
Count=0
Ent
eranumberN
Sum =Sum +N
Count=Count+1
Av er
age=Sum /
Count
NO
N
YES
PRI
NTSum,
Aver
age
EN
Exampl
e7:
MutukutookaloanofKsh.400,
000fr
om alocal
bankatanint
erestr
ateof10%payabl
e
i
nfouryears.Assumingyouwisht
odevel
opacomput erpr
ogram t
hatwil
lkeept
rackof
monthl
yrepayments:
(
a)I
denti
fyt
heinput,pr
ocessingandout
putrequi
rement
sforsuchaprogram.
(
b)Desi
gnthealgor
it
hm fortheprogr
am usi
ngasimplefl
owchartandpseudocode.
(
a).Requi
rement
s:
I
nput -I
nit
ialamountborr
owed
-I
nterestr
ate
-Numberofy ear
s
Pr
ocessi
ng -equat
iont
ocal
cul
ateYear
lyr
epay
ment
sandMont
hlyr
epay
ment
s.
Out
put -Mont
hlyr
epay
ment
scal
cul
atedbyt
hepr
ocess
(
b).Pseudocode:
START
-62-
I
NPUTI
nit
ialamountborr
owed
I
NPUTI
nterestr
ate
I
NPUTNumberofy ear
s
Cal
cul
ateYear
lyr
epay
ment
s
Mont
hlyr
epay
ment
s=(
Year
lyr
epay
ment
s/12)
OUTPUTMont
hlyr
epay
ment
s
STOP
Fl
owchar
t:
BEGI
N
ENTERIni
ti
alamount,I
nter
est
rat
e,numberofYear
s
Calcul
ateYear
ly
r
epayments&Mont hl
y
r
epayments
PRI
NTMont
hlyr
epay
ment
s
EN
-63-