CIT 04206 ProgrammingConcepts Topics: 1. Introduction 2. Creating a Program Plan & Flow 3. Program Development (Writing Codes) 4. Program Structure 5. Program Testing and Debugging 6. PHP Basics
2.
Chapter 1 Intro toProgramming and Program Design • Why Program? • Computer Systems : Hardware and Software • Programs and Programming Languages • What is a Program Made of? • Input, Processing and Output • The Programming Process • Procedural and Object-Oriented Programming
3.
Concept - Computerscan do many different jobs because they are programmable. Why Program? • Computers perform a wide variety of tasks. • Computer programmers provide the software for computers. • Software transforms a general purpose computer into a specialized tool.
4.
Concept - Aprogram is a set of instructions that a computer follows in order to perform a task. 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.
5.
What a programmerdoes • 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.
6.
Computer Computer Systems :Hardware and Software • Central Processing Unit • Main Memory • Secondary Storage • Input Devices • Output Devices • Software : – loaded from a secondary storage device into main memory Input Device Main Memory CPU Output Device Secondary Storage
7.
Software • The instructionsthat tell the computer and all of its devices what to do. – When and what data to get – What and how to manipulate the data – When and how to provide it to the user Concept – Software connects the user to the machine
8.
Concept - Aprogram language is a special language used to write computer instructions. 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
9.
CS331 • Introduction Evolutionof Programming Languages 1. Machine language 2. Symbolic assembly language • “mnemonics” : names for memory locations instead of addresses 3. Assembler macros • One statement for many 4. High-level languages • Machine independent • Natural notation • Instruction explosion
10.
CS331 • Introduction SourceCode • Optimized for human readability – expressive: matches human notions of grammar – redundant to help avoid programming errors int expr(int n) { int d; d = 4 * n * n * (n + 1) * (n + 1); return d; }
11.
CS331 • Introduction Machinecode • Optimized for hardware • Redundancy, ambiguity reduced • Information about intent lost • Assembly code ≈ machine code lda $30,-32($30) stq $26,0($30) stq $15,8($30) bis $30,$30,$15 bis $16,$16,$1 stl $1,16($15) lds $f1,16($15) sts $f1,24($15) ldl $5,24($15) bis $5,$5,$2 s4addq $2,0,$3 ldl $4,16($15) mull $4,$3,$2 ldl $3,16($15) addq $3,1,$4 mull $2,$4,$2 ldl $3,16($15) addq $3,1,$4 mull $2,$4,$2 stl $2,20($15) ldl $0,20($15) br $31,$33 $33: bis $15,$15,$30 ldq $26,0($30) ldq $15,8($30) addq $30,32,$30 ret $31,($26),1
12.
CS331 • Introduction Low-LevelLanguages • Machine Language (Binary) Machine friendly / user hostile – Tightly coupled to The Machine – Very terse • Assembly Language – Mnemonic version of machine language • Access to all supported instructions and formats – Features • Registers • Labels • Mnemonics • Storage control • Potential for highly efficient use of hardware – Liabilities • Little program structure – highly error prone • No reusability to other instruction sets • Terribly expensive to program this way
13.
CS331 • Introduction Higher-LevelLanguages • Goals of high level language – Notational convenience with appropriate “expressibility” – Machine independence (reuse, portability) – Human friendly – Easy maintenance – Machine translation to target environment • Appropriate granularity of operators and objects – May support an abstract programming environment • distributed? concurrent? secure? • Multiple families of higher-level languages – Imperative – Object-Oriented – Functional – Logical
14.
What is a(programming) language? • A program needs to be written in a language • There are many programming languages – Low-level, understandable by a computer – High-level, needs a translator! • C++ is a high level programming language A sequence of instructions A program (in computer language) An algorthm (in human language)
15.
• Machine binarylanguage: unintelligible • Low-level assembly language – Mnemonic names for machine operations – Explicit manipulation of memory addresses – Machine-dependent • High-level language – Readable – Machine-independent Levels of programming language
Some Popular Programming Languages BASICBeginners All-purpose Symbolic Instruction Code. A general programming language originally designed to be simple enough for beginners to learn. FORTRAN Formula Translator. A language designed for programming complex mathematical algorithms. COBOL Common Business-Oriented Language. A language designed for business applications. Pascal A structured, general purpose language designed primarily for teaching programming. C A structured, general purpose language developed at Bell Labs. C offers both high- level and low-level features. C++ Based on the C language, C++ offers object-oriented features not found in C. Also invented at Bell Laboratories. Java An object-oriented language invented at Sun Microsystems. Java may be used to develop programs that run over the Internet, in a web browser.
18.
Code • Source Code –Text written by the programmer. • Modified Source code – Expanded text produced by the Pre-processor. • Object Code – Machine level code generated by the compiler. • Executable Code – Machine level code generated by the linker. Source Code Pre-processor Modified Source Code Compiler Object Code Linker Executable Code
19.
What is aProgram Made of? 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.
20.
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.
21.
Variables • Symbolic namesthat represent locations in the computer’s 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.
22.
Variable Declarations • Variablesare 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.
23.
Input, Process, andOutput • 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.
24.
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. Concept - There are a number of steps involved in successfully creating a program. 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.