Вы находитесь на странице: 1из 24

Principles oI Programming - NI July 2005

1
Chapter 1: !ntroduction
n this chapter you will learn about:
Overview of PC components
The different types of language
Natural Language
Formal Language
Functional / mperative Language
Programming Languages
C as an imperative language
C program at a glance
Principles oI Programming - NI July 2005
2
Computer Hardware Components
Components of a PC
Principles oI Programming - NI July 2005
3
!nput / Output Devices
nput Devices
Accepts information from the user and transforms
it to digital codes that the computer can process
Example: keyboard, mouse, scanner
Output Devices
An interface by which the computer conveys the
output to the user
Example: monitor, printer
Principles oI Programming - NI July 2005
4
ain emory
A semiconductor device which stores the
information necessary for a program to run.
2 types
ROM (Read Only Memory)
Contains information that is necessary for the
computer to boot up
The information stays there permanently even when
the computer is turned off.
RAM (Random Access Memory)
Contains instruction or data needed for a program to
run
Got erased when the computer is turned off.
Principles oI Programming - NI July 2005
5
Central Processing Unit (CPU)
Does most of the work in executing a program
The CPU inside a PC is usually the microprocessor
3 main parts:
Control Unit
Fetch instructions from main memory and put them in
the instruction register
ALU (Arithmetic Logic Unit)
Execute arithmetic operations
Registers
Temporarily store instructions or data fetched from
memory
Principles oI Programming - NI July 2005
6
&torage Devices
A magnetic device used to store a large amount
of information.
Store the software components or data needed
for the computer to execute its tasks.
Could be "read only or "writable.
Example: Hard drive, CD ROM, floppy disks
Principles oI Programming - NI July 2005
7
!etwork Devices
Connect a computer to the other computers.
Enable the users to access data or execute
programs remotely.
Example: modem, Ethernet card
Principles oI Programming - NI July 2005
8
!atural language
Our everyday-language; spoken and written
Not 100% needed to understand:
"Do you want to buy this computer ? remains
comprehensible
Depends on circumstances; the context:
"Do you like one ? doesn't make sense on its own.
t needs a situation around it:
someone holding a bouquet of flowers: you might
take one
someone pointing to an expensive car: your opinion
is asked
someone 'offers' you an oily cloth to sneeze: you
don't take it
Principles oI Programming - NI July 2005
9
&emantics and &yntax
Semantics the meaning of the language within a
given context
Syntax - Syntax are the rules to join words
together in forming a correct expression or
phrase.
n natural languages it is often possible to
assemble a sentence in more than one correct
ways.
Principles oI Programming - NI July 2005
10
ormal Language
Language with limited, defined, words
Each concatenation of words ('phrase') has a
single, clearly defined meaning
no (miss-)interpretation possible
Sometimes called "Context Free Language
To 'talk' to a computer; to instruct a computer; our
commands must be 100% clear and correct.
Often there is only a singIe, correct syntax.
Principles oI Programming - NI July 2005
11
unctional / !mperative Language
Functional Language:
Tell at to do, but not how:
sum [1...10]
mperative Language:
Tell what to do, but mainly o:
"Take number 1 and add the next number to it;
then add the next number to the sum; and so on;
until you have reached 10 as number to be added.
Then print the sum of all numbers
Principles oI Programming - NI July 2005
12
hat is Programming?
!rogramming is instructing a computer to do something
for you with the help of a programming language
The two roles of a programming language:
%ecnicaI: t instructs the computer to perform tasks.
ConceptuaI: t is a framework within which we
organize our ideas about things and processes.
n programming, we deal with two kind of things:
Data - representing 'objects' we want to manipulate
Procedures -'/escriptions' or 'rules' that define how to
manipulate data.
Principles oI Programming - NI July 2005
13
Programming Language
ormaI Language used to communicate to a
computer.
A programming language contains instructions
for the computer to perform a specific action or a
specific task:
'Calculate the sum of the numbers from 1 to 10'
'Print " like programming'
'Output the current time'
Principles oI Programming - NI July 2005
14
Programming Language
Can be classified into as a special-purpose and
general-purpose programming languages.
Special-purpose : is design for a particular type of
application
Structured Query Language (SQL)
General-purpose : can be used to obtain solutions
for many types of problems
Machine Languages
Assembly Languages
High-Level Languages
Principles oI Programming - NI July 2005
15
achine Language
The only language that the processor actually
'understands'
Consists of binary codes: 0 and 1
Example: 00010101
11010001
01001100
Each of the lines above corresponds to a specific task
to be done by the processor.
Programming in machine code is difficult and slow
since it is difficult to memorize all the instructions.
Mistakes can happen very easily.
Processor and Architecture dependent
Principles oI Programming - NI July 2005
16
ssembly Language
Enables machine code to be represented in words and
numbers.
Example of a program in assembler language:
LOAD A, 9999
LOAD B, 8282
SUB B
MOV C, A
LOAD C, #0002
DV A, C
STORE A, 7002
Easier to understand and memorize (called Mnemonics),
compared to macine code but still quite difficult to use.
Processor and Architecture dependent
Principles oI Programming - NI July 2005
17
HighLevel Language
Use more English words. They try to resemble English
sentences. Therefore, it is easier to program in these
languages.
The programming structure is probIem oriented - does not
need to know how the computer actually executes the
instructions.
Processor independent - the same code can be run on
different processors.
Examples: asic, Fortran, !ascal, Cobol, C, C++, Java
A high level language needs to be analyzed by the compiIer
and then compiled into macine code so that it can be
executed by the processor.
Principles oI Programming - NI July 2005
18
C Programming Language
hy 'C' ?
Because based on ''; developed at ell Laboratories
Developed by Dennis Ritcie at Bell Laboratories in
the 1960s
n cooperation with Ken %omson it was used for
Unix systems
The C Language was only vaguely defined, not
standardized, so that almost everyone had his own
perception of it, to such an extend that an urgent
need for a standard code was creeping up
Principles oI Programming - NI July 2005
19
C Programming Language cont.
n 1983, the American National Standards nstitute
(ANS) set up X3J11, a Technical Committee to draft
a proposal for the ANS standard, which was
approved in 1989 and referred to as the ANS/SO
9899 : 1990 or simply the $ C, which is now the
global standard for C.
This standard was updated in 1999; but there is no
compiler yet
Principles oI Programming - NI July 2005
20
C - n !mperative Language
C is a highly imperative language
e must tell it exactIy o to do what;
the means and functions to use;
which libraries to use;
when to add a new line;
when an instruction is finished;
in short: everything and anything.
int: Observe the syntax in the next sli/e
Principles oI Programming - NI July 2005
21
&imple Program in C
#include <stdio.h>
main()
{
printf(" like programming in C.\n");
}
Principles oI Programming - NI July 2005
22
&imple Program in C explanation
#include <stdio.h>
main()
{
printf(" like programming in C.\n");
}
standard Library, input-output, eader-file
Begin of program
End of statement
End of Segment
Start of Segment
Function for printing text
nsert a new line
Principles oI Programming - NI July 2005
23
C Output
like programming in C.
Principles oI Programming - NI July 2005
24
&ummary
e have looked at some underlying hardware
e have seen some different types of languages;
the relevance of semantics and syntax.
e have observed the detail necessary in an
imperative Ianguage to instruct a computer
properly.
Finally, we examined the syntax to print a line of
text to the screen of our computer.

Вам также может понравиться