How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

46
How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5

Transcript of How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Page 1: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

How Are Computers Programmed?How Are Computers Programmed?

CPS120: Introduction to Computer ScienceLecture 5

Page 2: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Problem SolvingProblem Solving

Is an art, not a science It must be experienced, it can't be taught

Guidelines can be provided– Based on experience– Sound like platitudes– Are internalized as we learn to solve problems

It requires patience

Page 3: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Problem Solving TasksProblem Solving Tasks

Establish the context of the problemUnderstand the problemDetermine the primary goals of the solutionCreate a solution

Page 4: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Problem Solving GuidelinesProblem Solving Guidelines One big problem is unsolvable

Many small problems can be managed Understand the problem: What is needed?

Write on paper what results are expectedWhat functions (procedures) will be required to generate these resultsWhat information will need to be given to these functionsDescribe what the functions do before you write a single line of code

Page 5: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Programs

A program is a set of step-by-step instructions that directs the computer to do the tasks you want it to do and produce the results you want.

Page 6: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

The Program Development CycleThe Program Development Cycle

Get the program into machine-readable form

Test and debug the program

Translate the program

Understand the problem

Code the program

Plan the program's logic

Document the program

Page 7: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Understand the problemUnderstand the problem

Become familiar with what the program is trying to accomplish

Read the specificationsAsk questionsUnderstand the specifications

Eliminate extraneous information

Page 8: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Plan the Program’s LogicPlan the Program’s Logic Decide how to best meet the program’s

specifications Often uses a flowchart or pseudocode Divide the program into subroutines, functions or

modules Subroutines cost less and take less time to maintainCan be used in other programsSeveral programmers can work on the same project

Page 9: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Code the ProgramCode the Program

Put the solution, generally documented with pseudocode or a flowchart into a programming language

Page 10: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Machine-Readable FormMachine-Readable Form

Key the program into a computer

Page 11: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Translate the programTranslate the program

Translated into machine language using an assembler, a compiler or an interpreter

Includes the elimination of syntax errors Generate executable file

Page 12: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Test the ProgramTest the Program Logic errors are not listed during the translation

projects Only way to find logic errors is to do program

testingInvolves using sample data as inputExecuting the programChecking the results manually

Know as debugging

Page 13: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Document the ProgramDocument the Program Internal Documentation

Comments External Documentation

Program specificationLayout chartHierarchy chartProgram flowchartPseudo-codeData Dictionary

– Input, Output, Work Area– Name, description, type, initial value, calculation

Source listingTest Plan

Page 14: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Programming LanguagesProgramming Languages

A programming language is a set of rules that provides a way of telling a computer what operations to perform.

Page 15: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Levels of Programming LanguagesLevels of Programming Languages

Machine language Assembly Language High Level Languages Fourth Generation Languages (4GL)

Page 16: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Machine Languages

different for each computer processor

0100001101 100000 001101 11000100101 10001 1000001110111001. . .

Page 17: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Assembly Languages

different for each computer processor

main proc paymov ax, dsegmov ax, 0b00hadd ax, dxmov a1, b1mul b1, axmov b1, 04h

Page 18: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

High-Level LanguagesHigh-Level Languages

Higher Level LanguagesUse traditional programming logic where the programming instructions tell the computer what to do and how to perform the required operations.

4GLsUse high-level English-like instructions to specify what to do, not how to do it .

Page 19: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Interpreter vs. CompilerInterpreter vs. Compiler

InterpreterTranslates instructions to machine code line-by-line.

CompilerTranslates the entire program to machine code before running it.

Page 20: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Types of Programming LanguagesTypes of Programming Languages

Machine language Procedure-oriented languages Object-oriented languages Event-driven languages

Page 21: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Early Language HistoryEarly Language History FORTRAN (short for Formula Translator, developed in the 1950s by IBM In 1958, a language called ALGOL (Algorithm Language) was developed COBOL (Common Business Oriented Language) was created in 1960 to serve

as the primary language for large-scale programs In 1964, the BASIC language (Beginners All-Purpose Symbolic Instruction

Code) was first used In 1965, a language called PL/I was developed in hopes of being everything to

everyone. PL/I proved to be too complex. In the late 1960s, Niklaus Wirth developed a teaching language called Pascal.

Page 22: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Later Language HistoryLater Language History Ada, which was developed in 1983, is large and complex. Smalltalk is graphical and object-oriented. Concepts developed

with Smalltalk were important to the development and continued development of languages like C++ and Java

The C language was derived from ALGOL. C++ is C with the addition of object-oriented concepts.

Page 23: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Procedure-Oriented LanguagesProcedure-Oriented Languages

FORTRAN COBOL Pascal C Ada

Page 24: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

OOED LanguagesOOED Languages

Object-oriented languagesSmalltalkC++Ada 95

Event-driven languagesVisual Basicmost Visual languages

Page 25: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

What Can a Program Do?

A program can only instruct a computer to:Read InputSequenceCalculateStore dataCompare and branchIterate or LoopWrite Output

Page 26: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Fundamental Programming ConceptsFundamental Programming Concepts

Assignment of values to a variable Iteration (Looping)

Over a set of set of statementsWith respect to a logical expressions (conditions)

Delegation of sub-tasks to functions / procedures

Page 27: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

The Structure TheoremThe Structure Theorem

The Structure Theorem states that any algorithm can be built from three basic control structures.

One-after-another (Sequence) Decision-making (Selection)

Making choices between 2 or more alternatives Repetition (Iteration)

Concerned with repetitive tasks (and the termination conditions of loops)

Page 28: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

C++ Control StructuresC++ Control Structures

1. "Sequence statements" are imperatives2. "Selection" is the "if then else" statement

– AND, OR, NOT and parentheses ( ) can be used for compound conditions

3. "Iteration" is satisfied by a number of statements– "while" – " do " – "for"

4. The case-type statement is satisfied by the "switch" statement.

– CASE statements are used for most non-trivial selection decisions

Page 29: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Sequence Control Structures Sequence control structures direct the order of

program instructions. The fact that one instruction follows another—in

sequence—establishes the control and order of operations.

Page 30: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Calculate

A program can instruct a computer to perform mathematical operations.

Add 1 to

Counter

Page 31: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Store

A program will often instruct a computer to store intermediate results.

Place 1 in

Counter

Page 32: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Compare and Branch

A program can instruct a computer to compare two items and do something based on a match or mismatch which, in turn, redirect the sequence of programming instructions.

There are two forms:IF-THENIF-THEN-ELSE

Page 33: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

IF-THEN

Test Test condition pcondition p

falsefalse truetrue

EntryEntry

ExitExitTrue True

statement astatement a

Page 34: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

IF-THEN-ELSE

falsefalse truetrue

EntryEntry

ExitExit

Test Test condition pcondition p

““true” true” statement astatement a

““false” false” statement astatement a

Page 35: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Iterate

A program loop is a form of iteration. A computer can be instructed to repeat instructions under certain conditions.

No

Page 36: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Iteration Control Structures

Iteration control structures are looping mechanisms.

Loops repeat an activity until stopped. The location of the stopping mechanism determines how the loop will work:

Leading decisions Trailing decisions

Page 37: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Leading Decisions

If the stop is at the beginning of the iteration, then the control is called a leading decision.

The command DO WHILE performs the iteration and places the stop at the beginning.

Page 38: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

DO WHILE Loop

NoNo

YesYes

EntryEntry

ExitExit

Test Test condition pcondition p

Loop Loop statement astatement a

Page 39: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Trailing Decisions

If the stop is at the end of the iteration, the control mechanism is called a trailing decision.

The command DO UNTIL performs the iteration and puts the stop at the end of the loop.

Page 40: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

DO UNTIL Loop

Loop Loop statement astatement a

NoNo YesYes

EntryEntry

Test Test condition pcondition p

ExitExit

Page 41: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Programmer Productivity ToolsProgrammer Productivity Tools

Modular Programming Structured Programming Object-oriented Programming

Page 42: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Modular ProgrammingModular Programming

Large programs are divided by functional parts into subroutines

Strong cohesionLoose coupling

Page 43: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Structured Programming

Composed of sequence, decision (selection), and repetition (looping or iteration) structures

Structured program languages lend themselves to flowcharts, structure charts, and pseudocode.

Structured programming languages work best where the instructions have been broken up into small, manageable parts.

Looks at a problem as proceduresData are maintained separately from data

Page 44: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Structured Program RulesStructured Program Rules

1. Use only sequence, decision, and repetition2. Only one entrance into and one exit from a structure3. Connectors only allowed when continuing processing

from one column or page to another4. Decision and repetition structures can be nested5. Only one STOP instruction is permitted. It must be in

the MAINLINE routine

Page 45: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Structured Programming AdvantagesStructured Programming Advantages

Standard method for solving problems GO-TO less Easier to test and debug Written by more than one programmer Reusability Thrashing minimized

Page 46: How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.

Object-Oriented ProgramsObject-Oriented Programs Developed to respond to programming issues that

structured programming did not adequately address1. Rarely possible to anticipate the design of a completed

system before implementation2. GUIs were difficult to develop in traditional procedure-

oriented languages3. Sharing data across routines is error prone

Information hiding allows programmers to determine what data is exposed to various routines