Introduction to Programming. Prof. George Zolla Prof. Gary Porter (IS 2020)
-
Upload
eleanor-houston -
Category
Documents
-
view
231 -
download
1
Transcript of Introduction to Programming. Prof. George Zolla Prof. Gary Porter (IS 2020)
Introduction to Introduction to ProgrammingProgramming
.
Prof. George Zolla
Prof. Gary Porter (IS 2020)
ProgramsPrograms
A program is a set of A program is a set of step-by-step step-by-step instructions that instructions that directs the computer directs the computer to do the tasks you to do the tasks you want it to do and want it to do and produce the results produce the results you want.you want.
Programming LanguagesProgramming Languages
A programming A programming language is a set language is a set of rules that of rules that provides a way of provides a way of telling a computer telling a computer what operations what operations to perform.to perform.
What Can a Program Do?What Can a Program Do?
A program can only instruct a A program can only instruct a computer to:computer to:• Read InputRead Input• SequenceSequence• CalculateCalculate• Store dataStore data• Compare and branchCompare and branch• Iterate or LoopIterate or Loop• Write OutputWrite Output
Sequence Control Sequence Control StructuresStructures
Sequence control structures direct Sequence control structures direct the order of program instructions.the order of program instructions.
The fact that one instruction The fact that one instruction follows another—in sequence—follows another—in sequence—establishes the control and order establishes the control and order of operations.of operations.
CalculateCalculate
A program can A program can instruct a instruct a computer to computer to perform perform mathematical mathematical operations.operations.
Add 1 to
Counter
StoreStore
A program will A program will often instruct a often instruct a computer to store computer to store intermediate intermediate results.results.
Place 1 in
Counter
Compare and BranchCompare and Branch
A program can instruct a computer to A program can instruct a computer to compare two items and do something compare two items and do something based on a match or mismatch which, based on a match or mismatch which, in turn, redirect the sequence of in turn, redirect the sequence of programming instructions.programming instructions.• There are two forms:There are two forms:• IF-THENIF-THEN• IF-THEN-ELSEIF-THEN-ELSE
IF-THENIF-THEN
Test Test condition pcondition p
falsefalse truetrue
EntryEntry
ExitExitTrue True
statement astatement a
IF-THEN-ELSEIF-THEN-ELSE
falsefalse truetrue
EntryEntry
ExitExit
Test Test condition pcondition p
““true” true” statement astatement a
““false” false” statement astatement a
IterateIterate
A program loop is A program loop is a form of a form of iteration. A iteration. A computer can be computer can be instructed to instructed to repeat repeat instructions under instructions under certain certain conditions.conditions.
NoNo
Iteration Control Iteration Control StructuresStructures
Iteration control structures are Iteration control structures are looping mechanisms. looping mechanisms.
Loops repeat an activity until Loops repeat an activity until stopped. The location of the stopped. The location of the stopping mechanism determines stopping mechanism determines how the loop will work:how the loop will work:
Leading decisionsLeading decisions Trailing decisionsTrailing decisions
Leading DecisionsLeading Decisions
If the stop is at the beginning of If the stop is at the beginning of the iteration, then the control is the iteration, then the control is called a leading decision.called a leading decision.
The command The command DO WHILEDO WHILE performs performs the iteration and places the stop at the iteration and places the stop at the beginning.the beginning.
DO WHILE LoopDO WHILE Loop
NoNo
YesYes
EntryEntry
ExitExit
Test Test condition pcondition p
Loop Loop statement astatement a
Trailing DecisionsTrailing Decisions
If the stop is at the end of the If the stop is at the end of the iteration, the control mechanism is iteration, the control mechanism is called a trailing decision.called a trailing decision.
The command The command DO UNTILDO UNTIL performs performs the iteration and puts the stop at the iteration and puts the stop at the end of the loop.the end of the loop.
DO UNTIL LoopDO UNTIL Loop
Loop Loop statement astatement a
NoNo YesYes
EntryEntry
Test Test condition pcondition p
ExitExit
Programs are SolutionsPrograms are Solutionsto Problemsto Problems
Programmers arrive at these Programmers arrive at these solutions by using one or more of solutions by using one or more of these devices:these devices:
Logic flowchartsLogic flowcharts Structure chartsStructure charts PseudocodePseudocode Structured ProgrammingStructured Programming
Logic FlowchartsLogic Flowcharts
These represent These represent the flow of logic the flow of logic in a program in a program and help and help programmers programmers “see” program “see” program design.design.
Terminator. Shows the starting and ending points of the program. A terminator has flowlines in only one direction, either in (a stop node) or out (a start node).
Data Input or Output. Allows the user to inputdata and results to be displayed.
Processing. Indicates an operation performed by the computer, such as a variable assignment or mathematical operation.
Decision. The diamond indicates a decision structure. A diamond always has two flowlines out. One flowlineout is labeled the “yes” branch and the other is labeled the “no” branch.
Predefined Process. One statement denotes a group of previously defined statements. For instance, “Calculate m!” indicates that the program executes the necessary commandsto compute m factorial.
Connector. Connectors avoid crossing flowlines, making the flowchart easier to read. Connectors indicate where flowlines are connected. Connectors come in pairs, one witha flowline in and the other with a flowline out.
Off-page connector. Even fairly small programs can have flowcharts that extend severalpages. The off-page connector indicates the continuation of the flowchart on another page. Just like connectors, off-page connectors come in pairs.
Flowline. Flowlines connect the flowchart symbols and show the sequence of operations during the program execution.
Common Flowchart Symbols
Common Flowchart Common Flowchart SymbolsSymbols
Start
sum=0
Input price
sum=sum+price
Moreitems?
tax=sum x 0.0725total=sum+tax
Output sum, tax, and total
Stop
No
Yes
Flowchart for aFlowchart for aCash Register ProgramCash Register Program
Structure ChartsStructure Charts
Structure charts illustrate the Structure charts illustrate the structure of a program by showing structure of a program by showing independent hierarchical steps.independent hierarchical steps.
Major divisions are subdivided into Major divisions are subdivided into smaller pieces of information.smaller pieces of information.
PsuedocodePsuedocode
This device is not visual but is This device is not visual but is considered a “first draft” of the actual considered a “first draft” of the actual program.program.
Pseudocode is written in the Pseudocode is written in the programmer’s native language and programmer’s native language and concentrates on the logic in a concentrates on the logic in a program—not the syntax of a program—not the syntax of a programming language.programming language.
sum=0While More items do Input price sum=sum+priceEnd Whiletax=sum x 0.0725total=sum+taxOutput sum, tax, total
Pseudocode for aPseudocode for aCash Register ProgramCash Register Program
Structured ProgrammingStructured Programming
Structured program languages Structured program languages lend themselves to flowcharts, lend themselves to flowcharts, structure charts, and pseudocode.structure charts, and pseudocode.
Structured programming Structured programming languages work best where the languages work best where the instructions have been broken up instructions have been broken up into small, manageable parts.into small, manageable parts.
Analyze the problem
Design the solution algorithm
Design the user interface
Write the code
Test and debug the program
Complete the documentation
The Program Development The Program Development CycleCycle
Levels of Programming Levels of Programming LanguagesLanguages
Machine languageMachine language Assembly LanguageAssembly Language High Level LanguagesHigh Level Languages Fourth Generation Languages Fourth Generation Languages
(4GL)(4GL)
Machine LanguagesMachine Languages
different for each computer processordifferent for each computer processor
01000100
001101001101 100000100000 001101001101 110001110001
0010100101 1000110001 1000010000
0111001110
111001111001
. . .. . .
Assembly Assembly LanguagesLanguages
different for each computer processordifferent for each computer processor
mainmain proc payproc pay
mov ax, dsegmov ax, dseg
mov ax, 0b00hmov ax, 0b00h
add ax, dxadd ax, dx
mov a1, b1mov a1, b1
mul b1, axmul b1, ax
mov b1, 04hmov b1, 04h
High-Level LanguagesHigh-Level Languages
Higher Level LanguagesHigher Level Languages• Use traditional programming logic Use traditional programming logic
where the programming instructions where the programming instructions tell the computer what to do and how tell the computer what to do and how to perform the required operations.to perform the required operations.
4GLs4GLs• Use high-level English-like instructions Use high-level English-like instructions
to specify what to do, not how to do it .to specify what to do, not how to do it .
Interpreter vs CompilerInterpreter vs Compiler
InterpreterInterpreter• Translates instructions to machine Translates instructions to machine
code line-by-line.code line-by-line. CompilerCompiler
• Translates the entire program to Translates the entire program to machine code before running it.machine code before running it.
Types of Programming Types of Programming LanguagesLanguages
Machine languageMachine language Procedure-oriented languagesProcedure-oriented languages Object-oriented languagesObject-oriented languages Event-driven languagesEvent-driven languages
Procedure-Oriented Procedure-Oriented LanguagesLanguages
FORTRANFORTRAN COBOLCOBOL PascalPascal CC AdaAda
OOED LanguagesOOED Languages
Object-oriented languagesObject-oriented languages• SmalltalkSmalltalk• C++C++• Ada 95Ada 95
Event-driven languagesEvent-driven languages• Visual BasicVisual Basic• most Visual languagesmost Visual languages
Programmer’s LingoProgrammer’s Lingo
Program Program - detailed set of instructions for a - detailed set of instructions for a computercomputer
Programming LanguageProgramming Language - tool used to create a - tool used to create a program; defined by semantics and syntaxprogram; defined by semantics and syntax
SemanticsSemantics - the meaning of words in a language- the meaning of words in a language
SyntaxSyntax - rules for combining symbols of a - rules for combining symbols of a languagelanguage
Programmer’s LingoProgrammer’s Lingo
Source Code (code)Source Code (code) - program you write using a programming language
InterpreterInterpreter - translates and executes source code statement by statement
Programmer’s LingoProgrammer’s Lingo
Interpreter Process
Programmer’s LingoProgrammer’s Lingo
Compiler ProcessCompiler Process