Imperative Programming: Introduction To Computers and Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

COS132

Imperative Programming
Lecture 1.2
(Week 1, Lecture 2)

Chapter 1:
Introduction to Computers
and Programming
Tuesday 7 Feb & Wednesday 8 Feb2016
Group 5: 14:30 – 15:20 (EMS 2-150), Tues
Group 4: 12:30 – 13:20 (Centenary 3), Wed
Admin (questions that students have asked)
1) Where is the Informatorium?
IT building, street level. The entrance is next to the main
entrance to the IT building.

2) How do I get access to the Informatorium?


If you are going to the Informatorium for the first time, report to
the reception desk at the entrance. The reception staff will
activate your card so that you can swipe to get in.
3) Where do I go for login credentials for CS website?
Check the announcements on the COS132 website.
4) What Laptop should I buy?
5) How can I install Linux on my MS Windows laptop?
Information on questions 4 and 5 will be available on the
COS132 website by Friday the week.
2
RECAP: What is Imperative Programming?
• What does ‘Imperative’ mean?
– synonyms: words that mean the same thing: (MSWord Thesaurus)
• very important vital crucial
• essential necessary

• What does ‘Programming’ mean?


– construction of a computer program
– Program = a set of instructions for performing specified
computation tasks.
– Computation = processing of numeric, text data (values)

• So, Imperative computer programming covers


– the fundamental building blocks of computer programming.

3
Course objectives & outcomes
(1) COS132 covers the activities of constructing a
program for solving a given problem:

– edit: type program instructions (statements)


– compile: check that the grammar for the instructions is correct
– build: create the executable program file (computer code)
– run: test that the program does what it is supposed to do
– debug: fix problems if the program gives incorrect results

(2) The second objective - enable the student to:


– master the elements of a programming language (C++)
• data types (int, float, text)
• control structures (if, loop)
• Arrays (e.g. list of names)
• functions (units of code used many times)
• libraries (standard & commonly use functions) 4
Chapter 1:
Introduction
to
Computers
and
Programming
1.1 Why Program?
Computer – programmable machine designed to follow
instructions

Program – instructions in computer memory to perform a


task

Programmer – person who writes instructions (programs) to


make computer perform a task

SO, without programmers, no programs; without programs,


a computer cannot do anything
1.2
Computer Systems: Hardware & Software

Main Hardware
Component categories:

1. Central Processing
Unit (CPU)

2. Main Memory (RAM)


3. Secondary Memory /
Storage (DISK)

4. Input Devices

5. Output Devices Mother board


Figure 1-2
(Devices are interconnected)
Central Processing Unit (CPU)
Comprised of:
Control Unit
Retrieves and decodes program instructions
Coordinates activities of all other parts of computer
Arithmetic & Logic Unit
Hardware optimized for high-speed numeric calculation
Hardware designed for true/false, yes/no decisions

CPU
Organization
Main Memory
• It is volatile.
– Main memory is erased
when program terminates
or computer is turned off

• Also called Random Access


Memory (RAM)

• Organized as follows:
– bit: smallest piece of memory.
Has values 0 (off, false) or 1
Mother board
(on, true)

– byte: 8 consecutive bits.


Bytes have addresses.
Main Memory
Addresses – Each byte in memory is identified by a unique
number known as an address.

• In Figure 1-4, the number 149 is stored in the byte with


the address 16, and the number 72 is stored at address
23.
Secondary Storage
• Non-volatile: data retained when program is not running
or computer is turned off
• Comes in a variety of media:
– magnetic: floppy disk, hard drive
– optical: CD-ROM, DVD
– Flash drives, connected to the USB port

• What is missing?
Input Devices
• Devices that send information to the computer from
outside
• Many devices can provide input:
– Keyboard, mouse, scanner,
digital camera, microphone
– Disk drives, CD drives,
and DVD drives

– What is missing?
Software-Programs That Run on a
Computer
• Categories of software:

System software:
– programs that manage the computer hardware and the
programs that run on them.

• Examples: operating systems, utility programs, software


development tools

– Application software:
– programs that provide services to the user.
– Examples : word processing, games, programs to solve
specific problems
Programs and Programming Languages
• A program is a set of instructions that the computer
follows to perform a task
• We start with an algorithm, which is a set of well-defined
steps.

• E.g. Algorithm to read values into x and y and


compute & display the sum z = x + y
1. Prompt user to enter x value
2. Read x value
3. Prompt user to enter y value
4. Read y value
5. Compute z = x + y
6. ???
Example Algorithm for Calculating Gross
Pay
Machine Language
• Although the previous algorithm defines the steps for
calculating the gross pay, it is not ready to be executed
on the computer.
• The computer only executes machine language
instructions

• Machine language instructions are binary numbers,


such as

1011010000000101

• Rather than writing programs in machine language,


programmers use programming languages.
Programs and Programming Languages

Types of languages:

Low-level: used for communication


with computer hardware directly.
Often written in binary machine
code (0’s/1’s) directly.

High-level: closer to human


language
Some Well-Known Programming Languages
(Table 1-1 on Page 10)

C++
BASIC Ruby
FORTRAN
Java
Visual Basic
COBOL
C#
JavaScript
C Python
From a High-Level Program to an
Executable File
a) Create file containing the
program with a text editor.
b) Run preprocessor to convert
source file directives to source
code program statements.
c) Run compiler to convert source
program into machine
instructions.
d) Run linker to connect
hardware-specific code to
machine instructions,
producing an executable file.
• Steps b–d are often performed
by a single command or button
click.
• Errors detected at any step will
prevent execution of following
steps.
Integrated Development Environments
(IDEs)
• An integrated development environment, or IDE,
combine all the tools needed to write, compile, and
debug a program into a single software application.

• Examples are Microsoft Visual C++, Turbo C++ Explorer,


CodeWarrior, DevC++, etc.
Integrated Development Environments
(IDEs)
1.7 Procedural and Object-Oriented
Programming

• Procedural programming: focus is on the process.


Procedures/functions are written to process data.

• Object-Oriented programming: focus is on objects,


which contain data and the means to manipulate the
data. Messages sent to objects to perform operations.

You might also like