Introduction to
Computer Programming 1
1
Objectives
‣ Identify the different components of a computer
‣ Know about programming languages and their
categories
‣ Understand syntax and semantics
‣ Understand Software Development Method and
apply it in problem solving
2
Computer
‣ an electronic, programmable device that can store, retrieve, and
manipulate data
Major Components
1. Hardware
‣ tangible part of the computer
‣ composed of electronic and mechanical parts
2. Software
‣ intangible part of the computer
‣ consists data and computer programs
3
1. Hardware
- Main Memory
- Secondary Memory
- Central Processing Unit
- Input Devices
- Output Devices
4
Memory
‣ where data and instructions needed by the CPU to
perform are stored
Main Memory
‣ used to hold programs and data that the processor
is actively working on
Secondary Memory
‣ used to hold programs and data for long term use
5
Memory
6
Anatomy of Memory
Memory Cell
‣ an individual storage location in the
memory
Address of a memory cell
‣ the relative position of a memory cell in the
computer’s main memory
Contents of a memory cell
‣ the information stored in a memory cell,
either a program instruction or data
7
How is data stored in the memory?
Bit
‣ short for Binary Digit
‣ either a 0 or a 1
‣ smallest element a computer can deal with
‣ by grouping them together large range of values can be
represented
8
How is data stored in the memory?
Byte
‣ the amount of storage required to store a single character
‣ there are 8 bits to a byte
‣ capable of representing 28 = 256 different values
9
Central Processing Unit (CPU)
‣ coordinate all computer operations
‣ perform arithmetic and logical operations on data
retrieve instruction from the main memory
fetch
execute decode
carry out the instruction determine what the instruction is
10
Central Processing Unit (CPU)
3.6 GHz AMD A8-7670K Quad-Core Processor
‣ can execute approximately
3.6 billions of instructions
per second
4 GHz Intel® Core™ i7-6700K Quad-Core Processor
‣ able to produce complex
behavior at a speed
almost instantaneous to
human user
11
I/O Devices
‣ allows a computer system to interact with the outside world by
moving data in and out of the system
Input Device
‣ peripheral devices used to enter information for computation
e.g keyboard, mice, microphone
Output Device
‣ peripheral devices that receives data from the computer used for
observing the results of computation
e.g. monitor, printer, speaker
12
2. Software
‣ programs used by a computer to operate
Program
‣ collection of instructions that makes a computer
perform a specific task when executed
‣ list of instructions that tell a computer what to do
13
Software Categories
Applications Software
‣ programs that assist a computer user in accomplishing specific tasks
e.g. Microsoft Word, Google Chrome, Steam
Systems Software
‣ programs that are needed to keep all the hardware and applications software running
together smoothly
‣ interface between the hardware and the user applications
e.g. Operating systems like Windows, Linux, MacOS
Compiler
‣ translates a high-level computer program into machine language
e.g. GNU Compiler Collection (GCC)
14
Computer Languages
Machine Language
‣ language directly understood by a computer
‣ collection of binary numbers understood by a specific CPU
Programming Language
‣ standardized communication technique for expressing
instructions to a computer
‣ like human languages each has it’s own syntax and grammar
15
Programming Language Categories
Assembly Language
‣ mnemonic codes that correspond to machine language instructions
‣ computer operations are represented by mnemonic codes rather than
binary numbers and variables can be given names rather than binary
memory addresses
High-level Language
‣ used to write programs that are independent of the CPU on which they will
be executed
‣ combine algebraic expressions and English symbols
‣ is not directly understood by a computer
16
Syntax and Semantics
Syntax
‣ rules of a specific programming language
‣ it defines how we can put together symbols, reserved words
and identifiers to make a valid program
Semantics
‣ refers to the meaning of a program statement and it’s
purpose and role in a program
17
Syntax and Semantics
Keep in mind,
✓ A program that is syntactically correct is NOT
necessarily semantically correct
✓ A program will always do what we tell it to do,
NOT what we meant to tell it to do
18
Entering,
Translating
and Running
a High-level
Language
Program
19
Software Development Method
‣ also referred to as the Program Development Lifecycle
‣ basic steps in trying to solve a problem on a computer
Steps,
1. Problem Definition
2. Problem Analysis
3. Algorithm Design and Representation
4. Coding and Debugging
20
1. Problem Definition
‣ a clearly defined problem is already half the
solution
‣ computer programming requires us to define the
problem first before we even try to create a solution
e.g
“Create a program that will determine the number of
times a name occurs in a list.”
21
2. Problem Analysis
‣ involves breaking up the problem into smaller and simpler
sub-problems
‣ read the problem statement carefully to obtain a clear idea of
the problem and to determine the inputs and outputs
1. identify the data you have to work with - inputs
2. identify the desired results - outputs
3. develop a list of formulas to specify the relationships between
the inputs and outputs
22
2. Problem Analysis
“Create a program that will determine the number of times a
name occurs in a list.”
1. Input to the program
‣ list of names
‣ name to look for
2. Output of the program
‣ the number of times the name occurs in a list
23
3. Algorithm Design and Representation
Algorithm
‣ list of steps for solving a problem
‣ a clear unambiguous specification of the steps needed to solve a problem
‣ is a well-ordered collection of unambiguous and effectively computable
operations that, when executed, produces a result and halts in a finite amount of
time.
‣ it may be expressed using either of the following:
‣ Human Language (English, Tagalog, etc.)
‣ Graphical Representations (flowchart)
‣ Pseudocode which is a cross between human language and a programming
language
24
3. Algorithm Design and Representation
Algorithm using a Human Language:
1. Get the list of names, let's call this nameList
2. Get the name to look for, let's call this the keyName
3. Compare the keyName to each of the names in nameList
4. If the keyName is the same with a name in the list, add 1 to
the count
5. If all the names have been compared, output result
25
3. Algorithm Design and Representation
Algorithm using a Graphical Representation:
26
3. Algorithm Design and Representation
Algorithm using a Pseudocode:
Let nameList = List of Names
Let keyName = the name to be sought
Let Count = 0
For each name in NameList do the following
if name == keyName
Count = Count + 1 Display Count
27
4. Coding and Debugging
‣ After constructing the algorithm, it is now possible
to create the source code. Using the algorithm as
basis, the source code can now be written using
the chosen programming language.
Debugging
‣ The process of fixing errors (bugs) in your program
28
4. Coding and Debugging
Types of Errors
Syntax Error / Compile-time Error
‣ occur if there is syntax error in the code
‣ the compiler will detect the error and the program won’t compile
‣ the programmer won’t be able to form an executable program that a user can’t run until
the error is fixed
Runtime Errors
‣ displayed during the execution of the program
‣ occurs when the program directs the computer to perform an illegal operation e.g
division by zero
Logic Error
‣ caused by following an incorrect/faulty algorithm
29
Warning!
‣ Learning how to program takes time
‣ It requires you to THINK
‣ It requires a lot of PRACTICE
‣ It requires a lot of PATIENCE
‣ One cannot learn to program just by reading a
textbook.
30
The programmers of tomorrow are the wizards of the
future. You’re going to look like you have magic powers
compared to everybody else.
Gabe Newell
Founder and President, Valve
31
Questions?
32