Uploaded by Emmanuel Nyirenda

Programming concepts

advertisement
Introduction to Computer
Programming
Outline of Topics
•
•
•
•
•
•
Hardware
Software
Computer Languages
Syntax, Semantics, Grammars
Compilers
Compilation vs. Interpretation
Hardware
• The physical devices that a computer is
made of are referred to as the computer’s
hardware.
• Hardware is physical, and software
is virtual.
Software
• A program is a set of instructions that a
computer follows to perform a task.
• Programs that run on a computer are
referred to as software.
Software Categories
• System Software
– Programs written for computer systems
• Compilers, operating systems, …
• Application Software
– Programs written for computer users
• Word-processors, spreadsheets, & other
application packages
A Layered View of the Computer
Application Programs
Word-Processors, Spreadsheets,
Database Software, IDEs,
etc…
System Software
Compilers, Interpreters,Preprocessors,
etc.
Operating System, Device Drivers
Machine with all its hardware
Operating System (OS)
 An OS is the most fundamental set of programs
on a computer.
 OS provides several essential services:
– Loading & running application programs
– Allocating memory & processor time
– Providing input & output facilities
– Managing files of information
Components of Computer
•
•
•
•
•
CPU Central Processing Unit
RAM (Random Access Memory)
Mass storage devices
Input devices
Output Devices
Programs
• Programs are written in programming languages
– PL = programming language
• A PL is
– A special purpose and limited language
– A set of rules and symbols used to construct a
computer program
– A language used to interact with the computer
Computer Languages
• Programming languages allow
programmers to code software.
• The three major families of languages are:
– Machine languages
– Assembly languages
– High-Level languages
Computer Languages Cnt’d
– Machine Language
• Uses binary code
• Machine-dependent
• Not portable
• Assembly Language
– Uses mnemonics
– Machine-dependent
– Not usually portable
• High-Level Language (HLL)
–
–
–
–
Uses English-like language
Machine independent
Portable (but must be compiled for different platforms)
Examples: Pascal, C, C++, Java, Fortran, . . .
Machine Language
• The representation of a computer program which is
actually read and understood by the computer.
• Comprised of 1s and 0s
• The “native” language of a computer
• Difficult to program – one misplaced 1 or 0 will cause the
program to fail.
Example:
Operation
Address
0010
0000 0000 0100
0100
0000 0000 0101
0011
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 errorprone 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
High-level language
• Historically, we divide HL languages into
two groups:
– Procedural languages
– Object-Oriented languages (OOP)
Procedural languages
• Early high-level languages are typically called procedural
languages.
• Procedural languages are characterized by sequential
sets of linear commands. The focus of such languages is
on structure.
• Examples include C, COBOL, Fortran, LISP, Perl,
HTML, VBScript
Object-Oriented Languages
• Most object-oriented languages are high-level
languages.
• The focus of OOP languages is not on structure, but on
modeling data.
• Programmers code using “blueprints” of data models
called classes.
• Examples of OOP languages include C++, Visual
Basic.NET and Java.
Syntax & Semantics
• Syntax:
– The structure of strings in some language. A
language's syntax is described by a grammar.
– Examples:
• Binary number
<binary_number>
<bit>
= <bit> | <bit> <binary_number>
=0|1
• Identifier
<identifier> = <letter> {<letter> | <digit> }
<letter>
=a|b|...|z
<digit
=0|1|...|9
• Semantics:
– The meaning of the language
Syntax & Grammars
• Syntax descriptions for a PL are
themselves written in a formal language.
– E.g. Backus-Naur Form (BNF)
• The formal language is not a PL but it can
be implemented by a compiler to enforce
grammar restrictions.
• Some PLs look more like grammar
descriptions than like instructions.
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
Assembly
Language
Compiler
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’
Compilers & Programs
• Object program
– Output from the compiler
– Equivalent machine language translation of the
source program
– Files usually have extension ‘.obj’
• Executable program
– Output from linker/loader
– Machine language program linked with necessary
libraries & other files
– Files usually have extension ‘.exe’
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
CPU
Computed results
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
Program Errors
• Syntax Errors:
– Errors in grammar of the language
• Runtime error:
– When there are no syntax errors, but the program
can’t 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
Output
Input
• The interpreter stays around during execution
• It reads and executes statements one at a time
Compilation vs. Interpretation
• Compilation:
– Syntax errors caught before running the program
– Better performance
– Decisions made once, at compile time
• Interpretation:
– Better diagnostics (error messages)
– More flexibility
– Supports late binding (delaying decisions about
program implementation until runtime)
• Can better cope with PLs where type and size of
variables depend on input
– Supports creation/modification of program code on
the fly (e.g. Lisp, Prolog)
Mixture of C & I
Source
Program
Translator
Intermediate
Program
Intermediate
Program
VM
Output
Input
• Many programming languages implement this
• Interpreter implements a Virtual Machine (VM).
JAVA
For portability:
Java
Compiler
bytecode
ML
Interpreter
For flexibility: Just In Time (JIT) compiler translates
bytecode into ML just before execution
The Program Development
Cycle
• The process containing the five phases of
program development:
1.Analyzing
2.Designing
3.Coding
4.Debugging and testing
5.Implementing and maintaining application
software
Flowchart
• A program design tool that graphically
shows step-by-step the actions a
computer program will take.
Programming as Problem
Solving
• Problem solving principles:
1.
2.
3.
4.
Completely understand the problem
Devise a plan to solve it
Carry out the plan
Review the results
• Developing a Program:
1. Analyze the problem
2. Design the program
3. Code the program
4. Test the program
An example of programming as problem solving, Brewster’s
Thousands follows …
Analyze the Problem
• Brewster’s Thousands
– The problem: Brewster wants to invest money at a
local bank. There are many options such as interest
rates, terms of deposit, compounding frequencies.
He needs a program to compute, for any given initial
investment, the final maturity (value) of the deposit.
• What are the inputs? (given data)
• What are the outputs? (required data)
• How will we calculate the required outputs from the
given inputs?
Design the Program
• Create an outline of the program
• An algorithm – a step by step procedure
that will provide the required results from
the given inputs.
• Algorithm Examples: Instructions on how
to make a cake, use the bank’s ATM, etc.
Code the Program
• Once the design is completed, write the
program code.
• Code is written in some programming
language such as BASIC, Pascal, C++,
Java, etc.
In this course we write code in pseudocode, developing the skills to be used
when studying the specific languages.
Testing the program
• Locate any errors (bugs)
• Testing is done throughout the development
cycle
• Desk-checking, or code walkthrough is
performed to locate errors in the code.
– Pretend you are the computer and execute your own code.
• Ultimate test is to run the program to see if the
outputs are correct for the given inputs.
Modular Programming
• Determine the major tasks that the
program must accomplish. Each of these
tasks will be a module.
• Some modules will be complex
themselves, and they will be broken into
sub-modules, and those sub-modules may
also be broken into even smaller modules.
• This is called top-down design
Code Modules
• A module is an independent, selfcontained section of code that performs a
single task.
• The main module is the module that drives
the application. It “controls” all other
modules. Typically, the main module calls
other modules in order to have them
perform certain tasks.
Program Control & Modules
• When the main module calls another
module, program control transfers to the
called module. Program control cedes
back to the main module when the called
module finishes.
Structured Programming
• A method for designing and coding
programs in a systematic, organized
manner.
• It combines the principles of top-down
design, modularity and the use of the three
accepted control structures of sequence,
repetition and selection.
Questions
Resources
• Venit, Stewart. Extended Prelude to
Programming: Concepts and Design.
Scott/Jones, Inc., 2002.
Download