1) Introduction To Computer Programming: Dr. E. Lang
1) Introduction To Computer Programming: Dr. E. Lang
1
Table of Contents
1.1 Introduction
1.2 Data
1.2.1 Primitive Data Types
1.2.2 Derived Data Types
1.3 Data—Operations
1.4 Program Structures
1.4.1 Sequence Structures
1.4.2 Control Structures
1.4.2.1 Decision Structures
1.4.2.2 Repetition Structures
1.4.3 Module Structures
1.5 Computer Architecture
1.6 Summary
2
1.1) Introduction
Note: The following will be a general introduction; therefore the
details and examples do not refer to any specific programming
language. You should keep this in mind as we go through the slides.
3
1.1) Introduction
A computer program consists of two basic components:
• Data
• What is manipulated in the computer and the operations
associated with the data type
• Programming Structures
• How the computer manipulates the data
4
1.2) Data
The first basic component of computer programming is data.
There are two classes of data:
5
1.2.1) Primitive Data Types
Primitive (basic) data are the fundamental data types that the
computer will operate on.
The following are the five basic data types of any computer:
• integers (eg. -1, 0, 1, 10)
• floating-point (real) numbers (eg. 1.0, 1.0E00, 1.234638)
• logical data (only two: TRUE (1) or FALSE (0))
• character data (eg. ˈaˈ, ˈ1ˈ, ˈ+ˈ, ˈ=ˈ )
• addresses (variables that contain addresses are called pointers)
6
1.2.1) Primitive Data Types
Note that “1” is represented in all the data types; however, to the
computer each of these is completely different; therefore, the way the
computer processes each is different. It is important to understand this
distinction to avoid mistakes.
For example, the character data ˈ1ˈ is just a symbol to the computer
and has no relation to the number “1”.
The character data ˈ+ˈ is also just a symbol with no relation to the plus
sign (“+”) used for the operation of addition. However “+” applied to
integer data represents the operation of addition. Whether “+” is used for
the operation of addition or is an element of the character data set is
determined by the computer from the context in which it is used.
7
1.2.1) Primitive Data Types
Examples: To the computer
ˈ1+1=3ˈ
{1, -20, 3, 3}
Note: even though the Social Insurance Number (SIN) is a string of numbers, they
are not considered to be numeric data but rather character data.
10
1.3) Data—Operations
Each data type has operations that can be applied to the data. There are clear
rules as to how the operations are performed, and associated with each operation
is an operator represented by a symbol.
One of the most important thing to understand about operations is that they
are usually restricted to data of the same type. So for example, operations on
integers are only done between integers. The computer cannot add an integer and
a real number. Neither can it add an integer and character data, such as ˈ3ˈ; at
least not in the sense you would expect (more later).
But the computer may do “behind-the-scene manipulations” to perform these
operations that can lead to unexpected results.
11
1.3) Data—Operations
Fundamental to the concept of operations is the following form:
12
1.3) Data—Operations
Example 1: Arithmetic Addition
The operation of addition can be performed on integers and the symbol
commonly used is “+”:
5 + 6
The operands are the integers “5” and “6” and the operator is addition
represented by the “+”.
As we will discover during the course, this is not always as simple as the
above seems. You must be aware of the data type of the operands and the
meaning of the operand on those data types.
13
1.3) Data—Operations
Example 2: Logical Operators
The operator “+” is not defined for logical data. The following has no
meaning and will result in an error:
TRUE + FALSE
TRUE or FALSE
14
1.3) Data—Operations
Example 3: Character Concatenation
The same operator can have different meanings for different data. For character data there
is a concatenation operator, which mean “put together”. The symbol used is language
dependent, but “+” is sometimes used:
Here ˈabcˈ and ˈ167ˈ are strings of characters which are arrays of characters (a data
structure); ˈ167ˈ is not an integer. Note that ˈ ˈ is the blank character, which is blank to you
ab a character to the computer; so 1000 blanks will take up 1000 memory spaces in the
computer.
So “+” can mean one thing for integers and another for character data.
15
1.3) Data—Operations
When working with data and operations, it is important to
understand that the concept of the data and the operation are
universal to computer programming. However, the way they are
implemented in any language is language dependent. In this case, you
must learn the syntax of the language to implement the concepts.
16
1.3) Data—Operations
Example 4: For example, to concatenate the strings "Hello" and "world!", in C++,
we could use the syntax is:
But the syntax of MATLAB requires the use of the strcat function:
These are the syntactic differences between the languages: the concept is the same,
the syntax is different. Note that C++ also has a strcat function. Also note that C++
used the double quotes ("), but MATLAB uses the single quote (ˈ)—syntactic differences.
17
1.3) Data—Operations
Example 5: You cannot use operators with different data. So the following
are invalid:
ˈ5ˈ + 5
5 + 3.0
The first case tried to apply the “+” operator to the character ˈaˈ and the
integer 5. In fact, the computer treats character data as integers, but you will
not get the result you expect!
The second case tries to add an integer and a floating-point number. This
is an example of a mixed-mode expression: the combination of different data
types in a single expression. The computer has rules for mixed-mode
operations that we will consider later.
18
1.4) Program Structures
The second basic component of computer programming is program structures.
Program structures define the way program statements are arranged and controlled,
that is, they define the flow of the program execution.
There are three classes of program structures:
1) Sequence structures
2) Control Structures
1. Decision Structures
2. Repetition Structures
3) Module structures
19
1.4) Program Structures
Structured programming is the use of the above structures to write
computer code. You should note that these structures are all that you
have to write a program. When writing a program you will only have
the data types and these structures.
These three program structures are common to most programming
languages; for example, C, C++, Java and Python. The differences from
one language to another is in the syntax (the “grammar” used for the
structure). If you master the use of these structures in one, you will be
able to adapt and learn other language with much greater ease.
20
1.4) Program Structures
Pseudocode is a generalized method of writing computer code
without reference to a specific programming language. The following
programs will be written in pseudocode, and more details are provided
in the Pseudocode Primer on Blackboard.
21
1.4.1) Sequence Structures
A sequence structure is simply a series of ordered program
statement which are executed in order without any change in the flow
of the program.
22
1.4.1) Sequence Structures
Example (in pseudocode):
Note that these are executed in order and the order cannot change
23
1.4.2) Control Structures
Control structures alter the flow of the program; therefore,
statements may not be executed sequentially. There are two types:
24
1.4.2.1) Decision Structures
The decision structure, or selection structure, is used to change the
flow of the program depending on the result of a question.
25
1.4.2.1) Decision Structures
Example (in pseudocode):
TestNumber:
1. read M
2. if M == 5 then # This is True or False
3. display "Right!“ #Indented program block
4. else then
5. display "Wrong!"
26
1.4.2.2) Repetition Structures
In programming it is often the case that a section of code has to be
repeated several times. If a pure sequence structure was used the code
would get very long. It is not uncommon for a section of code to be
repeated 100 times or 1,000 times or 1,000,000 times.
The solution to this problem is to use a repetition structure (aka loop
structure).
27
1.4.2.1) Decision Structures
Example (in pseudocode):
IncrementNumber:
1. N ← 0
2. for i ← 1 to 5 do
3. N ← N + i
4. display N
IncrementNumber:
1. N ← 0
2. while N < 5 do
3. N ← N + i
4. display N
Note that the sequence strcture, lines 3 and 4, are repeated as long as
the logical expression N < 5 is true, when false the repetition structure
is terminated. 29
1.4.3) Module Structures
Modules are independent program units that alter the program flow
as follows:
30
1.4.3) Module Structures
Example (in pseudocode):
IncrementNumber(N):
1. for i ← 1 to 5 do
2. M ← N + i
3. return M
ProgramNumber:
1. N ← 0
2. M ← IncrementNumber(N)
3. display M
33
1.4.3) Module Structures
All programming languages have modular structures. They are just
given different names:
• Modules
• Procedures
• Subroutines
• Methods
• Functions
• Since modules are independent, each modules can be written and tested
separately.
• Once a module is written and tested it does not have to be touched again.
• Program modification can be done by adding new modules or modifying a
specific existing one.
• When an exiting module is modified, other modules usually do not have to be
modified.
• A large programming project will consist of a set of modules which can be
divided into groups and assigned to different programmers. And since the
modules are independent this reduces the interaction needed between the
programmers.
• A user of a module only needs to know the input and output of the module. 35
1.4.3) Module Structures
Disadvantages:
• Data has to flow into and out of the modules and this can be a bit
difficult to master.
• Different modules may effect data that is common to the entire
programming system in inconsistent ways.
• Modifying a module may create errors in other parts of the
programming system, especially with respect to the access and
modification of common data.
36
1.4.3) Module Structures
A module once written acts as a black box. The user of the module
has no knowledge of the internal operations. To the user all that is
relevant is the input and output data and what the modules does to the
data. The programmer will write and test the module and provide the
users with a description of what it does and the required input/output.
37
1.4.3) Module Structures
History:
40
1.5) Computer Architecture
The architecture of a computer defines the way the physical
components of a computer are put together. For our purpose is will be
sufficient to consider that computer architecture consists of two basic
components:
41
1.5) Computer Architecture
CPU
ALU
Control
Unit
Memory
42
1.5) Computer Architecture
• The CPU consists of two basic units: the Control Unit and the Arithmetic Logical
Unit (ALU).
• The ALU performs various arithmetic operations like addition, subtraction, multiplication,
division and logical operations.
• The Control Unit controls the communication between the ALU the Memory Unit.
• The Memory Unit is a random access memory (RAM) and contains the program
instructions and program data.
The Control Unit will fetch program instructions and program data from RAM
and load it into the ALU. The ALU will perform the instructions on the data and
the Control Unit will transfer the results back to RAM.
43
1.5) Computer Architecture
The CPU has registers used for fast storage and to manipulate data.
There are two main types of registers that are of interest to this course:
Recall that in the computer integer “1” and floating-point “1.0f” are
different to the computer. The differences are based on standards that are
hardwired into the computer’s processor. So integer “1” is processed by
the integer register and floating-point “1.0f” by the floating-point register.
44
1.5) Computer Architecture
Computer data consists of bits which are represented by values of
0’s and 1’s. One important property of the CPU is the maximum
number of bits the registers operate on.
Typically these would be:
• 32-bit processor
• 64-bit processor
45
1.5) Computer Architecture
To synchronize the operations of the CPU, the computer needs a
clock. Processor are rated by clock speed (cycles/second) and every
computer operation will take a certain number of clock cycles.
Examples of processor clock speeds:
46
1.5) Computer Architecture
RAM (Random Access Memory) consists of a memory address and
program data or program instructions at that memory location.
Example:
Memory Address Data or Instruction
0001 Load data at memory 004 into Integer Register
0002 Add data at memory 005 to data in Integer Register
0003 Move result to memory 005
0004 6
0005 7 (13 after instruction at 0003 is executed)
47
1.5) Computer Architecture
A computer program and the data on which it acts is nothing but a
linear sequence of zeros and ones (binary numbers) called bits. So, the
previous example may look as follows starting at memory address 001:
011101010011001010101010101010…
So for example, 011101, may represent the load instruction. The way
the processor responds to this bit pattern is hardwired into the
computer.
48
1.6) Summary
We have seen that the two basic components of all computer
programming are
• Data types
• Program structures:
• sequence,
• control (decision and repetition) and
• module
51
1.6) Summary
But you should be aware of the fact that MATLAB is not a
programming language. MATLAB is a mathematical package with some
programming features.
As a result, MATLAB simplifies programming elements, especially
how data is treated. The result is that many students can find it difficult
to adapt to a programming language like C++ after working with
MATLAB.
52