g g g
,CS201g–
gIntroductiongtogProgramming
Lectureg No.g 1 .................................................................................................................... 3
Lectureg No.g 2 .................................................................................................................... 9
Lectureg No.g 3 .................................................................................................................. 15
Lectureg No.g 4 .................................................................................................................. 24
Lectureg No.g 5 .................................................................................................................. 33
Lectureg No.g 6 .................................................................................................................. 45
Lectureg No.g 7 .................................................................................................................. 55
Lectureg No.g 8 .................................................................................................................. 66
Lectureg No.g 9 .................................................................................................................. 77
Lectureg No.g 10 ................................................................................................................ 88
Lectureg No.g 11 ................................................................................................................ 99
Lectureg No.g 12 .............................................................................................................. 112
Lectureg No.g 13 .............................................................................................................. 124
Lectureg No.g 14 .............................................................................................................. 143
Lectureg No.g 15 .............................................................................................................. 158
Lectureg No.g 16 .............................................................................................................. 171
Lectureg No.g 17 .............................................................................................................. 186
Lectureg No.g 18 .............................................................................................................. 198
Lectureg No.g 19 .............................................................................................................. 211
Lectureg No.g 20 .............................................................................................................. 228
Lectureg No.g 21 .............................................................................................................. 245
Lectureg No.g 22 .............................................................................................................. 255
Lectureg No.g 23 .............................................................................................................. 269
Lectureg No.g 24 .............................................................................................................. 279
Lectureg No.g 25 .............................................................................................................. 292
Lectureg No.g 26 .............................................................................................................. 305
Lectureg No.g 27 .............................................................................................................. 319
Lectureg No.g 28 .............................................................................................................. 329
Lectureg No.g 29 .............................................................................................................. 345
Lectureg No.g 30 .............................................................................................................. 359
Lectureg No.g 31 .............................................................................................................. 369
Lectureg No.g 32 .............................................................................................................. 382
Lectureg No.g 33 .............................................................................................................. 395
Lectureg No.g 34 .............................................................................................................. 408
Lectureg No.g 35 .............................................................................................................. 422
Lectureg No.g 36 .............................................................................................................. 433
Lectureg No.g 37 .............................................................................................................. 444
Lectureg No.g 38 .............................................................................................................. 454
Lectureg No.g 39 .............................................................................................................. 469
Lectureg No.g 40 .............................................................................................................. 482
Lectureg No.g 41 .............................................................................................................. 497
Lectureg No.g 42 .............................................................................................................. 510
Lectureg No.g 43 .............................................................................................................. 520
Lectureg No.g 44 .............................................................................................................. 529
Lectureg No.g 45 .............................................................................................................. 553
2
©g Copyrightg Virtualg Universityg ofg Pakistan
,CS201g–
gIntroductiongtogProgramming
LecturegNo.g1
Summary
o Whatgisgprogramming
o Whyg programmingg isg important
o Whatg skillsg areg needed
o Developg ag basicg recipeg forg writingg programs
o Pointsgtogremember
Whatgisgprogramming
Asg thisg courseg isg titledg “Introductiong tog programming”,g thereforeg itg isg mostg essenti
algandg appropriateg tog understandg whatg programmingg reallyg means.g Letg usg firstg see
g agwidelygknowngdefinitiongofgprogramming.
Definition:g "Ag programg isg ag preciseg sequenceg ofg stepsg tog solveg ag particularg problem.”
Itgmeansgthatgwhengwegsaygthatgweghavegagprogram,gitgactuallygmeansgthatgwegknowg a
boutgagcompletegsetgactivitiesgtogbegperformedgingagparticulargorder.gThegpurposegofg th
esegactivitiesgisgtogsolvegaggivengproblem.
Alang Perlis,g ag professorg atg Yaleg University,g says:
"Itg goesg againstg theg graing ofg moderng educationg tog teachg childreng tog program.g Whatg f
ungisg thereg ing makingg plans,g acquiringg disciplineg ing organizingg thoughts,g devotingg
attentiongtogdetailgandglearninggtogbegself-critical?g"
Itgisgagsarcasticgstatementgaboutgmoderngeducation,gandgitgmeansgthatgthegmoderngeduca
tiong isg notg developingg criticalg skillsg likeg planning,g organizingg andg payinggattention
gtogdetail.gPractically,gingourgdaygtogdayglivesgwegaregconstantlygplanning,gorganizingg a
ndg payingg attentiong tog fineg detailsg (ifg weg wantg ourg plansg tog succeed).g Andgitgisgalsog
fungtogdogthesegactivities.gForgexample,gforgagpicnicgtripgwegplangwheregtoggo,gwhatg to
g wear,g whatg tog takeg forg lunch,g organizeg travelg detailsg andg haveg ag goodg timegwhil
egdoinggso.
Wheng weg talkg aboutg computerg programmingg theng asg Mr.g Steveg Summitg putsg it
“Atgitsgmostgbasicglevel,gprogramminggagcomputergsimplygmeansgtellinggitgwhatgtogdo,g
andgthisgvapid-
soundinggdefinitiongisgnotgevengagjoke.gTheregaregnogothergtrulygfundamentalgaspectsgof
gcomputergprogramming;geverythinggelsegwegtalkgaboutgwillgsimplyg beg theg detailsg ofg
ag particular,g usuallyg artificial,g mechanismg forg tellingg agcomputerg whatg tog do.g Som
etimesg theseg mechanismsg areg choseng becauseg theyg havegbeengfoundgtogbegconvenien
tgforgprogrammersg(people)gtoguse;gothergtimesgtheyghavegbeengchosengbecausegthey'reg
easygforgthegcomputergtogunderstand.gThegfirstghardgthinggaboutgprogramminggisgtoglear
n,gbecomegcomfortablegwith,gandgacceptgthesegartificialgmechanisms,gwhethergtheygmak
eg``sense''gtogyougorgnot.g“
g3
©g Copyrightg Virtualg Universityg ofg Pakistan
, CS201g–
gIntroductiongtogProgramming
WhygProgrammingg isgimportant
Theg questiong mostg ofg theg peopleg askg isg whyg shouldg weg learng tog programg wheng th
eregaregsogmanygapplicationgsoftwaregandgcodeggeneratorsgavailablegtogdogthegtaskgforg
us.gWellgtheganswergisgasggivegbygthegMatthiasgFelleisengingthegbookg‘Howgtogdesigngp
rograms’
“Theganswergconsistsgofgtwogparts.gFirst,gitgisgindeedgtruegthatgtraditionalgformsgofgprog
ramminggaregusefulgforgjustgagfewgpeople.gBut,gprogramminggasgwegthegauthorsgunders
tandg itg isg usefulg forg everyone:g theg administrativeg secretaryg whog usesgspreadsheetsg
asgwellgasgtheghigh-
techgprogrammer.gIngothergwords,gweghavegagbroadergnotiongofgprogramminggingmindgt
hangthegtraditionalgone.gWegexplaingourgnotiongingagmoment.g Second,g weg teachg ourg id
eag ofg programmingg withg ag technologyg thatg isg basedgongthegprinciplegofgminimalgintr
usion.gHence,gourgnotiongofgprogramminggteachesgproblem-analysisgandgproblem-
solvinggskillsgwithoutgimposinggthegoverheadgofgtraditionalgprogramminggnotationsgand
gtools.”
Henceglearninggtogprogramgisgimportantgbecausegitgdevelopsganalyticalgandgproblemgs
olvinggabilities.gItgisgagcreativegactivitygandgprovidesgusgagmeangtogexpressgabstractg ide
as.g Thusg programmingg isg fung andg isg muchg moreg thang ag vocationalg skill.g Bygdesig
ninggprograms,gweglearngmanygskillsgthatgaregimportantgforgallgprofessions.gThesegskills
gcangbegsummarizedgas:
o Criticalg reading
o Analyticalg thinking
o Creativeg synthesis
Whatgskillsgaregneeded
Programmingg isg ang importantg activityg asg peopleg lifeg andg livingg dependsg ong thegpr
ogramsgonegmake.gHencegwhilegprogramminggonegshould
o Payingg attentiong tog detail
o Thinkg aboutg theg reusability.
o Thinkg aboutg userg interface
o Understandg theg factg theg computersg areg stupid
o Commentg theg codeg liberally
Payingg attentiong tog detail
Ingprogramming,gthegdetailsgmatter.gThisgisgagverygimportantgskill.gAggoodgprogrammer
galwaysganalyzesgthegproblemgstatementgverygcarefullygandgingdetail.gYougshouldgpayga
ttentiong tog allg theg aspectsg ofg theg problem.g Youg can'tg beg vague.g Youg can'tg describ
egyourgprogramg3/4thgofgthegway,gthengsay,g"YougknowgwhatgIgmean?'',gandghavegthegc
ompilergfiguregoutgthegrest.
Furthermoregyougshouldgpaygattentiongtogthegcalculationsginvolvedgingthegprogram,gitsgf
low,gandgmostgimportantly,gtheglogicgofgthegprogram.gSometimes,gaggrammaticallygcorr
ectgsentencegdoesgnotgmakeganygsense.gForgexample,gheregisgagversegfromgpoemg"Throu
ghgthegLookinggGlass"gwrittengbygLewisgCarol:
4
©g Copyrightg Virtualg Universityg ofg Pakistan