You are on page 1of 29

Introduction to Computer Programming

What is Programming?
Computers are designed to execute a set of instructions. Programming is the act of creating a set of instructions for a computer to execute. A set of instructions that performs a specific task is called an algorithm.
Concept - A program is a set of instructions that a computer follows in order to perform a task.

What a programmer does


A programmer creates the following things:
The logical flow of the instructions The mathematical procedures The appearance of the screens
The way information is presented to the user The program's user friendliness

Manuals and other forms of written documentation


Concept Make a general purpose machine perform a specific task.

Programming Languages
Computers use machine language consisting of numbers only. Humans have difficulty communicating purely in numbers. A programming language provides a way for humans to communicate with a computer
Concept - A program language is a special language used to write computer instructions.

Types of Programming languages


First Generation : Machine languages
Strings of numbers giving machine specific instructions Example:
+1300042774 +1400593419 +1200274027

Second Generation : Assembly languages


English-like abbreviations representing elementary computer operations (translated via assemblers)
Example: LOAD BASEPAY ADD OVERPAY STORE GROSSPAY

Third Generation : High-level languages


Codes similar to everyday English Use mathematical notations (translated via compilers) Example: grossPay = basePay + overTimePay
Introduction to Computers and Programming - Class 1 5

Machine Language
The representation of a computer program which is actually read and understood by the computer.
A program in machine code consists of a sequence of machine instructions.

Instructions:
Machine instructions are in binary code Instructions specify operations and memory cells involved in the operation

Example:

Operation

Address

0010 0100 0011

0000 0000 0100 0000 0000 0101 0000 0000 0110

Assembly Language
A symbolic representation of the machine language of a specific processor. Is converted to machine code by an assembler. Usually, each line of assembly code produces one machine instruction (One-to-one correspondence). Programming in assembly language is slow and error-prone but is more efficient in terms of hardware performance. Mnemonic representation of the instructions and data Example:
Load Add Store Price Tax Cost

High-level language
A programming language which use statements consisting of English-like keywords such as "FOR", "PRINT" or IF, ... etc. Each statement corresponds to several machine language instructions (one-to-many correspondence). Much easier to program than in assembly language. Data are referenced using descriptive names Operations can be described using familiar symbols Example: Cost := Price + Tax

Some Popular Programming Languages


BASIC
Beginners All-purpose Symbolic Instruction Code. A general programming language originally designed to be simple enough for beginners to learn. mathematical algorithms.

FORTRAN Formula Translator. A language designed for programming complex


COBOL Pascal C C++ Java
Common Business-Oriented Language. A language designed for business applications. A structured, general purpose language designed primarily for teaching programming. A structured, general purpose language developed at Bell Labs. C offers both high-level and low-level features. Based on the C language, C++ offers object-oriented features not found in C. Also invented at Bell Laboratories. An object-oriented language invented at Sun Microsystems. Java may be used to develop programs that run over the Internet, in a web browser.

PL hierarchy

Source Code
Text written by the programmer.

Code
Source Code Pre-processor

Modified Source code


Expanded text produced by the Pre-processor.

Modified Source Code


Compiler Object Code Linker Executable Code

Object Code
Machine level code generated by the compiler.

Executable Code
Machine level code generated by the linker.

Compilers & Programs


Compiler
A program that converts another program from some source language (or high-level programming language / HLL) to machine language (object code). Some compilers output assembly language which is then converted to machine language by a separate assembler. Is distinguished from an assembler by the fact that each input statement, in general, correspond to more than one machine instruction.

Compilation into Assembly L


Source Program Compiler Assembly Language

Assembly Language

Assembler

Machine Language

Compilers & Programs


Source program
The form in which a computer program, written in some formal programming language, is written by the programmer. Can be compiled automatically into object code or machine code or executed by an interpreter. Pascal source programs have extension .pas

What is a Linker?
A program that pulls other programs together so that they can run. Most programs are very large and consist of several modules. Even small programs use existing code provided by the programming environment called libraries. The linker pulls everything together, makes sure that references to other parts of the program (code) are resolved.

Running Programs
Steps that the computer goes through to run a program:
Memory
Machine language program (executable file)

Input Data

Data entered during execution


Computed results

CPU

Program Output

Program Execution
Steps taken by the CPU to run a program (instructions are in machine language):
1. 2. 3. 4. 5. Fetch an instruction Decode (interpret) the instruction Retrieve data, if needed Execute (perform) actual processing Store the results, if needed

Syntax Errors:

Program Errors

Errors in grammar of the language

Runtime error:
When there are no syntax errors, but the program cant complete execution
Divide by zero Invalid input data

Logical errors:
The program completes execution, but delivers incorrect results Incorrect usage of parentheses

Compilation
Source Program Input Compiler Target Program Target Program Output

Compiler translates source into target (a machine language program) Compiler goes away at execution time Compiler is itself a machine language program, presumably created by compiling some other high-level program Machine language, when written in a format understood by the OS is object code

Interpretation
Source Program
Interpreter Input Output

The interpreter stays around during execution It reads and executes statements one at a time

Language Description Element Key Words Words that have a special meaning. Key words may only be used for their intended purpose. Programmer Words or names defined by the programmer. They are Defined symbolic names that refer to variables or programming Symbols routines. Operators Operators perform operations on one or more operands. An operand is usually a piece of data, like a number. Punctuation Punctuation characters mark the beginning or ending of a statement, or separate items in a list. Syntax Rules followed when constructing a program. Syntax dictates how key words and operators may be used, and where punctuation symbols must appear. Concept - There are certain elements common to all computer programming languages.

What is a Program Made of?

Lines and Statements


A line is one single line of program text A statement is:
A complete instruction that causes the computer to perform some action May consist of more than one line

A C++ statement must end in a semicolon (;)

Concept - Programs are made of a complete statements.

Variables
Symbolic names that represent locations in the computers Random Access M Used to reference information that may change throughout the execution of a program The name of a variable should reflect the purpose of the data it references
Concept - A program stores information in variables.

Variable Declarations
Variables are either numbers or characters A variable declaration statement informs the compiler:
the name that will be assigned to the variable how it should be stored in memory it's initial value (optional)

A variable definition statement causes a variable to be created in memory.


Concept - Variables must be defined before they are used.

Data Types
BOOLEAN (2 bytes)
Either True or False

INTEGER (2 bytes)
Whole numbers (no decimals)
-32,768 32,767

LONG (4 bytes)
Whole numbers (integers)
-2 trillion 2 trillion

Whole numbers require less memory and run faster than decimal numbers (or variant

Data Types
SINGLE (4 bytes)
Real numbers
10^30 with 7 significant digits of accuracy

DOUBLE (8 bytes)
Real numbers
10^300 with 15 significant digits of accuracy

CURRENCY (8 bytes)
Real numbers
4 digits to the right of the decimal point 15 digits to the left of the decimal point

Data Types
DATE (8 bytes)
Storage of specific dates
Jan 01, 100 to Dec 31, 9999

STRING (1 byte per character)

Up to 2 billion characters per string


Use the * symbol to denote size Some numbers should be defined as strings
Student number, SIN, telephone number We do not perform arithmetic calculations on them

VARIANT (sized by Visual Basic as required)

Default type, if type not defined Can be any type and type can vary as code is executing Not to be used

Input, Process, and Output


A program takes data as input, processes it and returns new data as output:
Input usually comes from some external source but can be the output of another process. Output is usually sent to an external device but could be input to another process.

A process determines the content of the output.


Concept - The three primary activities of a program are input, processing, and output.

The Programming Process


1. Clearly define what the program is to do. 2. Visualize the program running on the computer. 3. Design a flow or Hierarchy chart. 4. Check the chart for logical errors. 5. Write a pseudocode version of the program. 6. Check the pseudocode for errors. 7. Write the actual program on paper. 8. Desk check the program for syntax or logical errors. 9. Enter the code and compile it. 10 . Correct any errors found during compilation. 11. Run the test data for input . 12. Correct any logical errors found while running the program.

Concept - There are a number of steps involved in successfully creating a program.

You might also like