Upload
blanche-warner
View
217
Download
3
Embed Size (px)
DESCRIPTION
Hardware Machine code Assemblers Compilers Interpreters Progression
Citation preview
Programming and Programming and LanguagesLanguages
Dept. of Computer and Information ScienceDept. of Computer and Information ScienceIUPUIIUPUI
Programming as Problem Programming as Problem SolvingSolving
Problem solving Problem solving principles:principles:1.1. Completely Completely
understand the understand the problemproblem
2.2. Devise a plan to Devise a plan to solve it solve it
3.3. Carry out the planCarry out the plan4.4. Review the resultsReview the results
Developing a Developing a Program:Program:1.1. Analyze the Analyze the
problemproblem2.2. Design the Design the
programprogram3.3. Code the programCode the program4.4. Test the programTest the program
HardwareHardware Machine codeMachine code AssemblersAssemblers CompilersCompilers InterpretersInterpreters
ProgressionProgression
Hardware encodingHardware encoding
Change physical Change physical structure of machinestructure of machine
Extremely tediousExtremely tediousVery difficultVery difficultProne to errorProne to error
Programming LanguagesProgramming Languages
Programming languages allow Programming languages allow programmers to code software.programmers to code software.
The three major families of languages The three major families of languages are:are: Machine languagesMachine languages Assembly languagesAssembly languages High-Level languagesHigh-Level languages
Machine LanguageMachine Language Stored Language ConceptStored Language Concept Von NeumannVon Neumann Treat Instructions as dataTreat Instructions as data Comprised of 1s and 0sComprised of 1s and 0s The “native” language of a computerThe “native” language of a computer Difficult to program – one misplaced 1 or 0 Difficult to program – one misplaced 1 or 0
will cause the program to fail.will cause the program to fail. Example of code:Example of code:1110100010101 111010101110 1110100010101 111010101110 10111010110100 1010001111011110111010110100 10100011110111
Assembly LanguagesAssembly LanguagesMnemonicsMnemonics Labeled cellsLabeled cells somewhat easier on programmerssomewhat easier on programmersAssembly languages are a step towards easier Assembly languages are a step towards easier
programming. programming. Assembly languages are comprised of a set of Assembly languages are comprised of a set of
elemental commands which are tied to a specific elemental commands which are tied to a specific processor.processor.
Assembly language code needs to be translated Assembly language code needs to be translated to machine language before the computer to machine language before the computer processes it.processes it.
Example:Example:ADD 1001010, 1011010ADD 1001010, 1011010
High-level LanguageHigh-level LanguageTyped in near-human languageTyped in near-human languageConverted to assemblerConverted to assembler Much easier on programmersMuch easier on programmersCan be assembled or compiledCan be assembled or compiledLanguage is still very precise, but more nearly Language is still very precise, but more nearly
readablereadableHigh-level languages represent a giant leap High-level languages represent a giant leap
towards easier programming.towards easier programming.The syntax of HL languages is similar to English. The syntax of HL languages is similar to English. Historically, we divide HL languages into two Historically, we divide HL languages into two
groups:groups: Procedural languagesProcedural languages Object-Oriented languages (OOP)Object-Oriented languages (OOP)
Procedural LanguagesProcedural Languages
Early high-level languages are typically called Early high-level languages are typically called procedural languages.procedural languages.
Procedural languages are characterized by Procedural languages are characterized by sequential sets of linear commands. The focus sequential sets of linear commands. The focus of such languages is on of such languages is on structurestructure..
Examples include C, COBOL, Fortran, LISP, Examples include C, COBOL, Fortran, LISP, Perl, HTML, VBScriptPerl, HTML, VBScript
Most object-oriented languages are high-level Most object-oriented languages are high-level languages.languages.
The focus of OOP languages is not on The focus of OOP languages is not on structure, but on structure, but on modeling datamodeling data..
Programmers code using “blueprints” of data Programmers code using “blueprints” of data models called models called classesclasses..
Examples of OOP languages include C++, Examples of OOP languages include C++, Visual Basic.NET and Java.Visual Basic.NET and Java.
Object-orientedObject-oriented
InterpretingInterpreting
Some programs are translated using an Some programs are translated using an interpreterinterpreter. Such programs are translated . Such programs are translated line-by-line instead of all at once (like compiled line-by-line instead of all at once (like compiled programs). Interpreted programs generally programs). Interpreted programs generally translate quicker than compiled programs, but translate quicker than compiled programs, but have a slower execution speed.have a slower execution speed. Interpreter runs in backgroundInterpreter runs in background Instructions are translated in real timeInstructions are translated in real time high-level code is distributedhigh-level code is distributed More flexibleMore flexible SlowerSlower
CompilingCompilingSome programs are translated using a Some programs are translated using a
compilercompiler. When programs are compiled, they . When programs are compiled, they are translated all at once. Compiled programs are translated all at once. Compiled programs typically execute more quickly than typically execute more quickly than interpreted programs, but have a slower interpreted programs, but have a slower translation speed.translation speed. Compiler converts entire program into machine Compiler converts entire program into machine
languagelanguage binary code is distributedbinary code is distributed Less flexibleLess flexible FasterFaster
Regardless of the HL Language, all HL Regardless of the HL Language, all HL programs need to be translated to machine programs need to be translated to machine code so that a computer can process the code so that a computer can process the program.program.
Structured ProgrammingStructured Programming
A method for designing and coding programs A method for designing and coding programs in a systematic, organized manner. in a systematic, organized manner.
It combines the principles of top-down design, It combines the principles of top-down design, modularity and the use of the three accepted modularity and the use of the three accepted control structures of control structures of sequencesequence, , repetitionrepetition and and selectionselection..
Control StructuresControl StructuresSequenceSequence –in sequential order. –in sequential order.
The simplest of control structures – start at The simplest of control structures – start at the beginning and continue in sequential the beginning and continue in sequential order.order.
SelectionSelection – selectively execute statements – selectively execute statements Called a Called a branchbranch, it requires a condition to , it requires a condition to
determine when to execute statements.determine when to execute statements.
Control StructuresControl Structures
RepetitionRepetition – repeat statements more than once – repeat statements more than once Called a Called a looploop, it needs a stop condition, I.e, the , it needs a stop condition, I.e, the
program will continue to loop until some condition is program will continue to loop until some condition is met.met.
Sequence StructureSequence Structure
Start – where the Start – where the program beginsprogram begins
Input – data to be Input – data to be used for the programused for the program
Step One, Step Two, Step One, Step Two, etc. – the sequence etc. – the sequence of the program until of the program until it finishesit finishes
No deviationNo deviation
Start
Input data
Step One
Step Two
Selection StructureSelection StructureStart the programStart the program Input – data to be usedInput – data to be usedAsking the question; Asking the question;
only one questiononly one questionThe answer, YES or NOThe answer, YES or NODepending on the Depending on the
answer, the action to answer, the action to be takenbe taken
The program moves on The program moves on or endsor ends
Loop StructureLoop StructureProgram startsProgram startsThe condition is set The condition is set Input – ask for Input – ask for
passwordpasswordAnswer is evaluated, Answer is evaluated,
compared to conditioncompared to condition If no, action of asking If no, action of asking
for password for password continues until input continues until input of correct passwordof correct password
Event-Driven ProgrammingEvent-Driven Programming
In an In an event-driven event-driven program, the flow of program, the flow of control is based on the user’s clicking on control is based on the user’s clicking on menus and buttons, etc. These user actions menus and buttons, etc. These user actions are called are called eventsevents..
Event-driven programming still uses the basic Event-driven programming still uses the basic principles of structured programming – principles of structured programming – program modules, control structures, good program modules, control structures, good programming style, and program testing.programming style, and program testing.
Types of ErrorsTypes of ErrorsSyntax – wrong grammar, i.e., breaking Syntax – wrong grammar, i.e., breaking
the rules of how to write the languagethe rules of how to write the language Forgetting punctuation, misspelling Forgetting punctuation, misspelling
keywordkeyword The program will not run at all with syntax The program will not run at all with syntax
errorserrorsLogic - the program runs, but does not Logic - the program runs, but does not
produce the expected results.produce the expected results. Using an incorrect formula, incorrect sequence of Using an incorrect formula, incorrect sequence of
statements, etc.statements, etc.
Programming ExampleProgramming ExampleSimple programming problem: Simple programming problem: Convert a Convert a
price from British pounds into Dollars.price from British pounds into Dollars.PseudocodePseudocode
Input the price of the item, PoundPrice, in pounds
Compute the price of the item in dollars:
Set DollarPrice = 1.62 * PoundPriceWrite DollarPrice
Programming ExampleProgramming Example
Translating to Basic:Translating to Basic:
INPUT PoundPriceLET DollarPrice = 1.62 * PoundPricePRINT DollarPriceEND
<script language = "JavaScript"> <!– var strUserName = "";
// user's name, this is a STRING var strUserCity = "";
// user's city, this is a STRING var intNumLetters = 0;
// number of letters in user's first name, this is an INTEGER var strMessage = ""
// output to the user, this is a STRING strUserName = window.prompt("What is your name?");
//this is asking for the user's name strUserCity = window.prompt("Where do you live?");
//this is asking what city does the user live in strMessage = ("Hello, "); strMessage += (strUserName.toUpperCase());
//this puts the user's name into uppercase letters strMessage += (". You live in "); strMessage += (strUserCity.toUpperCase());
//this puts the user's city into uppercase letters window.alert(strMessage);
//this is the message given to the user intNumLetters = strUserName.length;
// output to the userwindow.alert("There are " +intNumLetters+" letters in your first name.");
//this is a string method, it is counting the number of letters in the user's name//this tells the user how many letters that user has in their name
// --> </script>