CHAPTER 1: Programming Fundamentals Fundamentals: Cseb113 Principles of Programming
CHAPTER 1: Programming Fundamentals Fundamentals: Cseb113 Principles of Programming
Fundamentals
CSEB113 PRINCIPLES of PROGRAMMING
BS(May2013)
Topics
1. ComputerProgramsandProgramming
Introduction
WhyDoEngineeringUndergraduatesNeedtoTake
ProgrammingCourse?
2. ProgrammingLanguages
MachineLanguage;AssemblyLanguage;Highlevel
Language
3. TheCLanguage
YourfirstCProgram
CProgrammingEnvironment
BS(May2013)
Topic 1
COMPUTER PROGRAMS AND
PROGRAMMING
BS(May2013)
Elements of a Computer
3
2
Application
software
1
system
software
Hardware
Software
BS(May2013)
Software/program asetofinstructionsstoredinsidea
computerthatallowstheusertodoaparticularthing.
Operating
O
ti
Systems
Windows8
Utilit P
UtilityPrograms
DiskCleanup
LLanguage
Translation
Compiler
SystemSoftware
y
*Complete this list
Mi
MicrosoftWord
ft W d
scanf( "%s", astring );
for ( i = 0; i < 10; ++i )
{
if ( astring[i] == 'a' )
{
printf( "You entered
an a!\n" );
}
}
ApplicationSoftware
pp
BS(May2013)
Embedded Systems
Computerprogramsthatareembeddedinto
p
hardwaretocontrolitsoperations.
Example:
Telecommunicationsystems
Consumerelectronics
Missilesandsatellite
Smartcards
Medicalequipment(e.g.,electronicstetescope)andmedical
imaging(e.g.MRI,CT)
Transportationsystems
Transportation systems
BS(May2013)
What is Programming?
Computerprogrammingis:
notonlyaboutwritingcomputerprograms
not only about writing computer programs
theiterativeprocessofdesigning,writing,testing,
debugging and maintaining thesourcecodeof
debugging,andmaintaining
the source code of
computerprograms
Thissourcecodeiswritteninoneormore
programminglanguages(suchasC,C++,C#,Java,
Python,Smalltalk,etc.)
oftenshortenedtoprogramming,
f
h
d
i
scripting,orcoding
BS(May2013)
W t hM
WatchMe
Source: http://www
http://www.youtube.com/watch?v=sGyH9tBie4M
youtube com/watch?v=sGyH9tBie4M
BS(May2013)
BS(May2013)
Topic 2
PROGRAMMING LANGUAGES
BS(May2013)
10
PL: Veryspecific(onewordmeansonething
Very specific (one word means one thing context
context
free)sinceto'talk'toacomputer;toinstructacomputer;
ourcommandsmustbe100%clearandcorrect.
BS(May2013)
11
BS(May2013)
12
HighLevel
Language
L
Assembly
y
Language
LOAD A, 9999
LOAD B, 8282
SUB B
MOV C, A
Machine
M
hi
Language
BS(May2013)
13
1. Machine Language
Theonlylanguagethatisdirectlyunderstandablebya
computersprocessor.
Certainlydifficultforhumanstounderstand
Certainly difficult for humans to understand
Consistsofbinarycodes:0and1.
Example: 1101 0001 1000 0101 1000
isthelanguageintowhichallprogramsmustbeconverted
beforetheycanberun
isgenerallymachinedependent,asitvariesfromprocessorto
i
ll
hi d
d
i
i f
processor(e.g.,Intel,AMDandARMprocessors)
isNOTportable
programmaysometimeshavetobecompletelyrewrittentoworkon
differenttypeofprocessors
Programminginmachinelanguageisveryslowandtedious
since it is hard to memorize all the instructions and mistakes
sinceitishardtomemorizealltheinstructionsandmistakes
canhappenveryeasily
BS(May2013)
14
2. Assembly Language
Usesamnemoniccode torepresenteachmachineoperation
inwordsandnumbers
LOADA,9999
LOADB,8282
SUBB
SUB B
MOVC,A
Anintermediarylanguagethatcanbeunderstoodbyhumans
butisstillquitedifficulttouse
q
LOAD A, 9999
LOAD B, 8282
SUB B
MOV C, A
Assembler
00000000
00010101
00010110
00110101
01110111
Cannotbeprocesseddirectlybyacomputer,mustbe
converted to machine language using assemblers
convertedtomachinelanguageusingassemblers
Althougheasiertousethanmachinelanguage,programmeris
requiredtohaveacompleteunderstandingofthecomputer
hardware in order to program it
hardwareinordertoprogramit.
Todosimpletaskssuchasprintingoutamessage,
programmerneedstowriteasubstantialamountofcode.
Processordependentandnotportable.
BS(May2013)
15
3. High-level Languages1
UsemoreEnglishwordssoeasiertoprograminthese
languages.Example:
printf (Hello);
total = quiz + test + assignment;
Theprogrammingstructureisproblemoriented
h
bl
i
d
doesnotneedtoknowhowthecomputeractuallyexecutes the
instructions.
Processorindependent thesamecodecanberunon
differentprocessors.
Programswrittenintheselanguagesmustbetranslatedinto
P
itt i th
l
tb t
l t di t
executablemachinelanguageusing
Compilers translateinstructionstocreateexecutableform
Interpreters translateandexecuteinstructionsoneafteranother
BS(May2013)
16
3. High-level Languages2
Example:
C
PHP
Ruby
Ada
Java
Visual Basic
Visual Basic.NET
MATLAB
ObjectiveC
Python
LISP
Lua
C++
Perl
Pascal
Bash
C#
Javascript
Delphi
FORTRAN
Allhavesetofrulescalledsyntax
mustbefollowed
describetheformofavalidprogramandtogetanaccuratetranslation
intomachinelanguage
BS(May2013)
17
Topic 3
THE C LANGUAGE
BS(May2013)
18
The C Language1
ItwasdevelopedbyDennisRitchieintheearly1970s
(
p
atBellLaboratories(nowapartofLucent
Technologies,Inc).
Ahighlevellanguagethatishighlyportable.
Ccombinesthepowerofhighlevellanguageswith
thepowerofassemblylanguages.
Ithasinfluencedthenewlycreatedhighlevel
languagenameObjectiveC
whichisthemainprogramminglanguageusedbyApple
fortheOSXandiOSoperatingsystemsandtheir
respective APIs Cocoa and Cocoa Touch frameworks
respectiveAPIs,CocoaandCocoaTouchframeworks.
BS(May2013)
19
The C Language2
Cisahighlyimperativeprogramminglanguage.This
means:
Itexpresseswhattheprogramshouldaccomplishby
prescribinghowtodoitintermsofsequencesofactions
tobetaken.
Itwasdesignedtobecompiledusingarelatively
straightforwardcompiler.
Th
Thelanguagehasbecomeavailableonaverywide
l
h b
il bl
id
rangeofplatforms,fromembeddedmicrocontrollers
tosupercomputers.
p
p
BS(May2013)
20
Itmustbetranslatedintomachinelanguage.Once
the machine language is executed the program
themachinelanguageisexecuted,theprogram
producesanoutput: ****************
Hello World
****************
BS(May2013)
21
C Programming Environment
AmodernCprogrammingoftenmadeeasybytheuseof
integrateddevelopmentenvironments(IDEs).
IDEsthatsupportClanguageincludeMicrosoftVisualStudio,
Eclipse,andetc.
MsVisualStudio2010ExpressorMsVisualStudioExpress
Ms Visual Studio 2010 Express or Ms Visual Studio Express
2012isfree!
Highlyrecommendedforyourpersonaluse
Youmustregistertoobtainafreeproductkeyforongoinguse
BS(May2013)
22
BS(May2013)
23
Summary
Computersmustbegiveninstructionsintheformofcomputerprograms
beforetheycanperformactionsandmakedecisions.
Programmingistheiterativeprocessofdesigning,writing,testing,
P
i i th it ti
fd i i
iti t ti
debugging,andmaintainingthesourcecodeofcomputerprograms.
Conceptually,programminglanguageisaframeworkwithinwhichwe
organize our ideas about data (object we want to manipulate) and
organizeourideasaboutdata(objectwewanttomanipulate)and
procedures(howtomanipulatethedata).
Ingeneral,programminglanguagesmaybecategorizedas1)machine
language (also called machine code) 2) assembly language and 3) high
language(alsocalledmachinecode),2)assemblylanguage,and3)high
levellanguages.
Cisahighlyimperativeprogramminglanguage.
AmodernCprogrammingoftenmadeeasybytheuseofintegrated
d
f
d
b h
f
d
developmentenvironments(IDEs).
BS(May2013)
24