Documente Academic
Documente Profesional
Documente Cultură
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.
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.
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
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
PL hierarchy
Source Code
Text written by the programmer.
Code
Source Code Pre-processor
Object Code
Machine level code generated by the compiler.
Executable Code
Machine level code generated by the linker.
Assembly Language
Assembler
Machine Language
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
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
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.
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)
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
Default type, if type not defined Can be any type and type can vary as code is executing Not to be used