Sei sulla pagina 1di 24

CHAPTER 1: 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)

Computer and Software


Computerscandoalotofexcitingtasks.
However,computersmustbegiveninstructionsintheformof
computerprogramsbeforetheycanperformactionsandmake
decisions.

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

sscanf( "%s", astring );


for ( i = 0; i < 10; ++i )
{
if ( astring[i] == 'a' )
{
printf( "You entered an a!\n" );
}

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)

Why Should I Know Programming?


Everybodyinthiscountryshouldlearnhow
toprogramacomputerbecauseitteaches
to program a computer because it teaches
youhowtothink SteveJobs

W t hM
WatchMe

Source: http://www
http://www.youtube.com/watch?v=sGyH9tBie4M
youtube com/watch?v=sGyH9tBie4M

BS(May2013)

Why Take Programming Course?


Programmingisnotonlyforcomputersciencepeopleonly.
Writingcomputerprogramstosolvecomplicatedengineering
problemsandtocontrolmechanicaldevicesisabasicskillall
engineersmustmaster.
Introductoryprogrammingcourseisn
Introductory programming course isn'tttryingtoteachyouto
trying to teach you to
programsothatyoucanbeaprogrammer.
Instead,suchcoursejustwantyoutothinklikeprogrammers
j
y
p g
becauseinthefutureyouwillrunintoproblemsthataretoo
longandcomplextodobyhand.
In
Inthosecases,you
those cases you'llllhavetouseMATLABorMappleormathworksorsomeother
have to use MATLAB or Mapple or mathworks or some other
mathrelatedapplicationthatallowsyoutosolvetheseproblems.
Unfortunately,ifyoucan'tcodeyourproblemintotheapplication,itwon'tdoyou
anygood.

BS(May2013)

Topic 2
PROGRAMMING LANGUAGES
BS(May2013)

10

What is a Programming Language?1


Technically,itisanartificiallanguage designedto
,
communicateinstructionsto,ortocontrolthe
behaviorofamachine,particularlyacomputer.
Itisdifferentfromoureverydaylanguage(natural
languageNL)
NL:Dependsoncircumstances;thecontext oneword
canhavemanymeaningdependingonthesituation(e.g.,
h
i d
di
h i
i (
OPERATE)

PL: Veryspecific(onewordmeansonething
Very specific (one word means one thing context
context
free)sinceto'talk'toacomputer;toinstructacomputer;
ourcommandsmustbe100%clearandcorrect.
BS(May2013)

11

What is a Programming Language?2


Conceptually,programminglanguageisa
frameworkwithinwhichweorganizeourideas
framework within which we organize our ideas
aboutdataandprocedures.
Data: 'objects'
objects or
or things
things wewanttomanipulate
we want to manipulate
Procedures:'descriptions'or'rules'or
processes
processes thatdefinehowtomanipulatedata
that define how to manipulate data
Example:
Asimpleprogramthatdeterminesumoftwonumbers
A simple program that determine sum of two numbers
Data?
Procedures?

BS(May2013)

12

Types of Programming Languages


printf (Hello);
total = quiz + assignment;

HighLevel
Language
L

printf(Total = %d, total);

Assembly
y
Language

LOAD A, 9999
LOAD B, 8282
SUB B
MOV C, A

Machine
M
hi
Language

1101 0001 1000 0101 1000 0110


0100 1100 0101 1110 0101 1010

BS(May2013)

13

1. Machine Language
Theonlylanguagethatisdirectlyunderstandablebya
computersprocessor.
Certainlydifficultforhumanstounderstand
Certainly difficult for humans to understand

Consistsofbinarycodes:0and1.
Example: 1101 0001 1000 0101 1000

0110 1111 0001


0100 1100 0101 1110 0101 1010 0001 1100

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

Your First C Program


ACprogram(alsocalledCsourcecode):
/
/**********************************************************
Author: Badariah Solemon
Date: 01/01/2013
Description: A program that prints the famous
Hello World message on the computer screen
**********************************************************/
#include <stdio.h>
<stdio h>
void main (void)
{
printf(****************\n);
printf( Hello World\n);
printf(****************\n);
}

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

Visual Studio C++ Express

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

Potrebbero piacerti anche