notes on Programming fundamentals

9,319 views 11 slides Jul 05, 2021
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

things to know before you start programming


Slide Content

Noteson
ConceptsofProgramming
Instructor:
ArghodeepPaul
FirmwareEngineeratBitBibleTechnologiesPvt.Ltd.
ContentLicenseUnder:OpenSource
Date:05July2021

Computerprogrammingistheactofwritingcomputerprograms,whicharea
sequenceofinstructionswrittenusingaComputerProgrammingLanguageto
performaspecifiedtaskbythecomputer.
IntroductiontoComputerProgram
Beforegettingintocomputerprogramming,letusfirstunderstandcomputer
programsandwhattheydo.
AcomputerprogramisasequenceofinstructionswrittenusingaComputer
ProgrammingLanguagetoperformaspecifiedtaskbythecomputer.
Thetwoimportanttermsthatwehaveusedintheabovedefinitionare−
Sequenceofinstructions
ComputerProgrammingLanguage
Tounderstandtheseterms,considerasituationwhensomeoneasksyouabouthow
togotoanearbyKFC.WhatexactlydoyoudototellhimthewaytogotoKFC?
YouwilluseHumanLanguagetotellthewaytogotoKFC,somethingasfollows−
Firstgostraight,afterhalfkilometer,takeleftfromtheredlightand
thendrivearoundonekilometerandyouwillfindKFCattheright.
Here,youhaveusedEnglishLanguagetogiveseveralstepstobetakentoreachKFC.
Iftheyarefollowedinthefollowingsequence,thenyouwillreachKFC−
1.Gostraight
2.Drivehalfkilometer
3.Takeleft
4.Drivearoundonekilometer
5.SearchforKFCatyourrightside

Now,trytomapthesituationwithacomputerprogram.Theabovesequenceof
instructionsisactuallyaHumanProgramwritteninEnglishLanguage,which
instructsonhowtoreachKFCfromagivenstartingpoint.Thissamesequencecould
havebeengiveninSpanish,Hindi,Arabic,oranyotherhumanlanguage,provided
thepersonseekingdirectionknowsanyoftheselanguages.
Now,let'sgobackandtrytounderstandacomputerprogram,whichisasequence
ofinstructionswritteninaComputerLanguagetoperformaspecifiedtaskbythe
computer.FollowingisasimpleprogramwritteninPythonprogrammingLanguage

print"Hello,World!"
Theabovecomputerprograminstructsthecomputertoprint"Hello,World!"on
thecomputerscreen.
Acomputerprogramisalsocalledacomputersoftware,whichcanrange
fromtwolinestomillionsoflinesofinstructions.
Computerprograminstructionsarealsocalledprogramsourcecode
andcomputerprogrammingisalsocalledprogramcoding.
Acomputerwithoutacomputerprogramisjustadumpbox;itisprograms
thatmakecomputersactive.
Aswehavedevelopedsomanylanguagestocommunicateamongourselves,
computerscientistshavedevelopedseveralcomputer-programminglanguagesto
provideinstructionstothecomputer(i.e.,towritecomputerprograms).Wewillsee
severalcomputerprogramminglanguagesinthesubsequentchapters.

IntroductiontoComputer
Programming
Ifyouunderstoodwhatacomputerprogramis,thenwewillsay:theactofwriting
computerprogramsiscalledcomputerprogramming.
Aswementionedearlier,therearehundredsofprogramminglanguages,whichcan
beusedtowritecomputerprogramsandfollowingareafewofthem−
Java
C
C++
Python
PHP
Perl
Ruby
UsesofComputerPrograms
Todaycomputerprogramsarebeingusedinalmosteveryfield,household,
agriculture,medical,entertainment,defense,communication,etc.Listedbeloware
afewapplicationsofcomputerprograms−
MSWord,MSExcel,AdobePhotoshop,InternetExplorer,Chrome,etc.,are
examplesofcomputerprograms.
Computerprogramsarebeingusedtodevelopgraphicsandspecialeffectsin
moviemaking.
ComputerprogramsarebeingusedtoperformUltrasounds,X-Rays,andother
medicalexaminations.
ComputerprogramsarebeingusedinourmobilephonesforSMS,Chat,and
voicecommunication.

ComputerProgrammer
Someonewhocanwritecomputerprogramsorinotherwords,someonewhocan
docomputerprogrammingiscalledaComputerProgrammer.
Basedoncomputerprogramminglanguageexpertise,wecannameacomputer
programmersasfollows−
CProgrammer
C++Programmer
JavaProgrammer
PythonProgrammer
PHPProgrammer
PerlProgrammer
RubyProgrammer
Algorithm,PseudocodeandProgram
Algorithm:Systematiclogicalapproachwhichisawell-defined,step-by-step
procedurethatallowsacomputertosolveaproblem.
Pseudocode:ItisasimplerversionofaprogrammingcodeinplainEnglishwhich
usesshortphrasestowritecodeforaprogrambeforeitisimplementedinaspecific
programminglanguage.
Program:Itisexactcodewrittenforproblemfollowingalltherulesofthe
programminglanguage.

Algorithm
Analgorithmisusedtoprovideasolutiontoaparticularprobleminformofwell-
definedsteps.Wheneveryouuseacomputertosolveaparticularproblem,the
stepswhichleadtothesolutionshouldbeproperlycommunicatedtothecomputer.
Whileexecutinganalgorithmonacomputer,severaloperationssuchasadditions
andsubtractionsarecombinedtoperformmorecomplexmathematicaloperations.
Algorithmscanbeexpressedusingnaturallanguage,flowcharts,etc.
Let’stakealookatanexampleforabetterunderstanding.Asaprogrammer,weare
allawareoftheLinearSearchprogram.
Algorithmoflinearsearch:
1.Startfromtheleftmostelementofarr[]and
onebyonecomparexwitheachelementofarr[].
2.Ifxmatcheswithanelement,returntheindex.
3.Ifxdoesn’tmatchwithanyofelements,return-1.
Here,wecanseehowthestepsofalinearsearchprogramareexplainedinasimple,
Englishlanguage.

Pseudocode
Itisoneofthemethodswhichcanbeusedtorepresentanalgorithmforaprogram.
Itdoesnothaveaspecificsyntaxlikeanyoftheprogramminglanguagesandthus
cannotbeexecutedonacomputer.Thereareseveralformatswhichareusedto
writepseudo-codesandmostofthemtakedownthestructuresfromlanguagessuch
asC,Lisp,FORTRAN,etc.
Manytimealgorithmsarepresentedusingpseudocodesincetheycanbereadand
understoodbyprogrammerswhoarefamiliarwithdifferentprogramminglanguages.
PseudocodeallowsyoutoincludeseveralcontrolstructuressuchasWhile,If-then-
else,Repeat-until,forandcase,whichispresentinmanyhigh-levellanguages.
Note:Pseudocodeisnotanactualprogramminglanguage.
PeudocodeforLinearSearch:
FUNCTIONlinearSearch(list,searchTerm):
FORindexFROM0->length(list):
IFlist[index]==searchTermTHEN
RETURNindex
ENDIF
ENDLOOP
RETURN-1
ENDFUNCTION

Inhere,wehaven’tusedanyspecificprogramminglanguagebutwrotethesteps
ofalinearsearchinasimplerformwhichcanbefurthermodifiedintoaproper
program.
Program
Aprogramisasetofinstructionsforthecomputertofollow.Themachinecan’tread
aprogramdirectly,becauseitonlyunderstandsmachinecode.Butyoucanwrite
stuffinacomputerlanguage,andthenacompilerorinterpretercanmakeit
understandabletothecomputer.
ProgramforLinearSearch:
//C++codeforlinearlysearchxinarr[].Ifx
//ispresentthenreturnitslocation,otherwise
//return-1
intsearch(intarr[],intn,intx)
{
inti;
for(i=0;i<n;i++)
if(arr[i]==x)
returni;
return-1;
}
AlgorithmvsPsuedocodevsProgram
1.Analgorithmisdefinedasawell-definedsequenceofstepsthatprovides
asolutionforagivenproblem,whereasapseudocodeisoneofthemethods
thatcanbeusedtorepresentanalgorithm.
2.Whilealgorithmsaregenerallywritteninanaturallanguageorplain
Englishlanguage,pseudocodeiswritteninaformatthatissimilartothe
structureofahigh-levelprogramminglanguage.Programontheotherhand
allowsustowriteacodeinaparticularprogramminglanguage.
So,asdepictedaboveyoucanclearlyseehowthealgorithmisusedtogenerate
thepseudocodewhichisfurtherexpandedbyfollowingaparticularsyntaxofa
programminglanguagetocreatethecodeoftheprogram.

ElementsofPLs
WeassumeyouarewellawareofEnglishLanguage,whichisawell-knownHuman
InterfaceLanguage.Englishhasapredefinedgrammar,whichneedstobefollowed
towriteEnglishstatementsinacorrectway.Likewise,mostoftheHumanInterface
Languages(Hindi,English,Spanish,French,etc.)aremadeofseveralelementslike
verbs,nouns,adjectives,adverbs,propositions,andconjunctions,etc.
SimilartoHumanInterfaceLanguages,ComputerProgrammingLanguagesarealso
madeofseveralelements.Wewilltakeyouthroughthebasicsofthoseelements
andmakeyoucomfortabletousetheminvariousprogramminglanguages.These
basicelementsinclude−
ProgrammingEnvironment
BasicSyntax
DataTypes
Variables
Keywords
BasicOperators
DecisionMaking
Loops
Numbers
Characters
Arrays
Strings
Functions
FileI/O
Wewillexplainalltheseelementsinsubsequentchapterswithexamplesusing
differentprogramminglanguages.First,wewilltrytounderstandthemeaningofall
thesetermsingeneralandthen,wewillseehowthesetermscanbeusedin
differentprogramminglanguages.
Thistutorialhasbeendesignedtogiveyouanideaaboutthefollowingmost
popularprogramminglanguages−

CProgramming
JavaProgramming
PythonProgramming
AmajorpartofthetutorialhasbeenexplainedbytakingCasprogramming
languageandthenwehaveshownhowsimilarconceptsworkinJavaandPython.
Soaftercompletionofthistutorial,youwillbequitefamiliarwiththesepopular
programminglanguages.
ProgrammingEnvironment
EnvironmentSetupisnotanelementofanyProgrammingLanguage,itisthefirst
steptobefollowedbeforesettingontowriteaprogram.
WhenwesayEnvironmentSetup,itsimplyimpliesabaseontopofwhichwecan
doourprogramming.Thus,weneedtohavetherequiredsoftwaresetup,i.e.,
installationonourPCwhichwillbeusedtowritecomputerprograms,compile,and
executethem.
Aprogrammingenvironmentsisthecollectionoftoolsusedinthedevelopmentof
software.
Thiscollectionmayconsist:-
 Afilesystem,
 Atexteditor,
 Alinker,
 Acompiler,
 Integratedtools
Thesetoolsmaybeaccessthroughauniforminterface(GUI).
Someoftheexamplesofprogrammingenvironmentsare-
1)MicrosoftVisualStudio.NET,whichisalargecollectionofsoftwaredevelopment
tools,usedthroughawindowsinterface.
Itisusedtodevelopsoftwareinfollowinglanguages-
 C#,
 VisualBasic.NET,
 JScript(MSJavaScriptversion),
 J#(MSJavaversion),

 managedC++.
2)NetBeans
3)TurboC,C++
4)Dreamweaver
5)Arduino,etc.