1 / 15

Turing 1: An Introduction to Programming

You will learn elementary computer programming in a high-level language called Turing . (Turing 4.0.2) You’ve already met most of the important ideas: How a processor works; how memory works What machine language instructions look like Spreadsheet formulas and functions

Download Presentation

Turing 1: An Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. You will learn elementary computer programming in a high-level language called Turing. (Turing 4.0.2) You’ve already met most of the important ideas: How a processor works; how memory works What machine language instructions look like Spreadsheet formulas and functions Now we meet the ideas put together in a single programming language. Turing 1: An Introduction to Programming Digression: Why learn Programming? Why Turing?

  2. Statements are executed one after the other. Control can branch to a different statement. Statements can be repeated over and over again. It should be able to read input, transform or process it, and write output. PROCESSING(CPU) Store (RAM) Ideas from Machine Language OUTPUT(Monitor) INPUT(Keyboard)

  3. In Excel: part to right of =expression. Spreadsheetexpressions can be made up of constants1500 3.14 -62 “fired” cell names (variables) $B$7 $D$5 $C$12 mathematical operators: + – * / ^ relational operators: = < > <= >= Boolean operators: AND OR NOT special symbols (known w/in context): , : ( ) functions (verbs/commands): SUM AVERAGE IF Ideas from Spreadsheet Formulas

  4. Turing has same basic ideas in its expressions, called program statements. A program is made up of these statements. Constants: 5150 3.14 -62 “fired” variable names: FirstName Age Grade assignment operator: := mathematical operators: + – * / ^ relational operators: = < > <= >= Boolean operators: & | not special symbols (within context): , : ( ) verbs/commands called keywords:put get Ideas from Spreadsheet Formulas

  5. Turing: interactive language: During runtime, it can stop to ask user for data. Hmmm….. Another example of that behavior in a service that you use/interact with quite often? Then Resumes execution. Plenty of other ways to get input; we won’t cover. Storing INPUT • Turing needs a place to store, keep track, and use input data values: where? RAM ! • Two keywordswill berequired for this: • var • get

  6. Much like SS cells: cell reference stays same, but contents can change, thereby destroying what was stored there before. Variables • Variable name identifies a chunk of RAM. • Much like what in memory? • Much like what in a SS? Note: my shortcut for variable name: varname Address Cell reference • Similar to SS cell name... we refer to varname but are interested in what’sstored there.Varname: acts much like absolutecell reference • General languages: the programmer creates mnemonic varnames—that helps his/her associate their meaning.

  7. Turing’s syntaxfor varnames: begin with alphabetic character only letters, digits, and _(no spaces!) not longer than 50 characters not a reserved word (see handout) Valid varnames:empName Age hours M16 Amt Not valid:3D emp name var true Declaring variables With freedom to choose name comes obligation to tell the translator a unique name and a data type before you can use it. 3 data types: int, real, string(define)

  8. Examples: var firstName : string var age : int var payRate : real Quotable... • Syntax for declaration statement: var [yourVarname] : [type] • var and the different types are reserved keywords • var is also a command • The colon is part of the lexicon and means something (semantics) to the translator within this context. • Syntax dictates the ordering of the statement parts, and dictates what varnames are legal.

  9. How do you store input data in a SS cell? Program: stores data in a variable a few ways: (1) ask user to type it at runtime: Programmer writes get, followed by varname. var firstName : stringwhy do we need this? get firstName Syntax for get statement: get [varname] What get command does at run time: 1. Turing pauses, waits for user to type in data [Enter] Grabs keyed data and stores it in RAM chunk that was pre-assigned that varname More about Input

  10. Step througheach statement, just like the computer does: (we’ll run program at computer later) var firstName : string get firstName OOT Run Window firstName Mary 2 Turing Environment Editor Window RAM RUN Mary<ENTER> var firstName : string get firstName

  11. Excel: there is visual representation of the values of allcells (-> all variables) on the screen at all times. This is certainly not a complete program! What’s missing? or whatever is stored in totPay at run time. Output • Turing: screen output needs an explicit put operations followed by the values you want displayed on screen: • variables (input data, or result of formula); constants; or some combination • Values will appear in the Runwindowat run time. • Observe these statements: • put “Your total pay will be” • put totPay • At run time, the run window will display: • Your total pay will be • 650

  12. Output • String (constant) values are placed inside quotes. • Where have you seen this behavior before in a SS? • Why do you suppose you need to do this? • What about varnames? Put inside quotes? Why? • Observe these statements: • put “Your total pay will be” • put “totPay” • At run time, that run window will display: • Your total pay will be • totPay

  13. Can put out more than 1 item* on a display line: list each item in one program statement, AND separate each entire item* with acomma: put “Your total pay will be ”, totPay --I included a space before the final quote; WHY? --What if I put the comma inside the final quote? *Items:var’s, constants (number or string), & results of formulas. • Evaluate these separate program statementsindividually: Output? Hello, World! put“Hello, World!” You are 22 yrs old put “You are ”, yourAge, “ yrs old” Margaret put YourName Your Name put “Your Name” 16 put 8+8 8+8= 16 put “8+8= ”, 8+8

  14. Launch Turing program… provides programming environment and the Turing translator program (a compiler), all in one. Programentry & editing:Editorwindow. File/new for another window. Simple editing tools. Press Enter only after each complete statement! Click Run to make it start: 1st: Program is translated into machine code Error Viewer Window displays syntax errors. Fix them; Run (translate) again; More errors? Fix, Run again…. Called: Run & Debug How things work

  15. CLEAN compilation? Machine language instructions are executed, one after the other (that’s when the machine cycles begin….) Run windowwill pop up if program includes put and/or get for data entry(user) for screen prompts, any input datayou want echoed back to user, and desired program output. TEST the output! How? Why? Then fix program as needed, run again….test… How things work, continued Demos!

More Related