COMP 4—Power Tools for the Mind1 PowerTools What’s in the Box? 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 Now we meet the ideas put together in a single programming language. Digression: Why learn Programming? Why Turing ?
COMP 4—Power Tools for the Mind2 PowerTools What’s in the Box? Ideas from Machine Language 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. INPUT (Keyboard) OUTPUT (Monitor) Store ( RAM ) PROCESSING (CPU)
COMP 4—Power Tools for the Mind3 PowerTools What’s in the Box? In Excel: part to right of = expression. Spreadsheet expressions can be made up of –constants “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
COMP 4—Power Tools for the Mind4 PowerTools What’s in the Box? Turing has same basic ideas in its expressions, called program statements. –A program is made up of these statements. Constants: “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
COMP 4—Power Tools for the Mind5 PowerTools What’s in the Box? Turing: interactive language: –During run time, 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 keywords will be required for this: – var – get
COMP 4—Power Tools for the Mind6 PowerTools What’s in the Box? Variables Much like SS cells: cell reference stays same, but contents can change, thereby destroying what was stored there before. Variable name identifies a chunk of RAM. Much like what in memory? Much like what in a SS? varname Note: my shortcut for variable name: varname Address Cell reference –Similar to SS cell name... we refer to varname but are interested in what’s stored there. Varname: acts much like absolute cell reference –General languages: the programmer creates mnemonic varnames—that helps his/her associate their meaning.
COMP 4—Power Tools for the Mind7 PowerTools What’s in the Box? Turing’s syntax for 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)
COMP 4—Power Tools for the Mind8 PowerTools What’s in the Box? 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.
COMP 4—Power Tools for the Mind9 PowerTools What’s in the Box? 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 run time : Programmer writes get, followed by varname. var firstName : string why 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] 2.Grabs keyed data and stores it in RAM chunk that was pre-assigned that varname More about Input
Step through each statement, just like the computer does: (we’ll run program at computer later) var firstName : string get firstName RAM Editor Window Turing Environment var firstName : string get firstName RUN OOT Run Window firstName Mary <ENTER> Mary 2
COMP 4—Power Tools for the Mind11 PowerTools What’s in the Box? Excel: there is visual representation of the values of all cells (-> all variables) on the screen at all times. 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 Run window at 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 This is certainly not a complete program! What’s missing? or whatever is stored in totPay at run time.
COMP 4—Power Tools for the Mind12 PowerTools What’s in the Box? 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
COMP 4—Power Tools for the Mind13 PowerTools What’s in the Box? Can put out more than 1 item* on a display line: – list each item in one program statement, AND separate each entire item* with a comma: 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 statements individually: put “Hello, World!” Hello, World! put “You are ”, yourAge, “ yrs old” put YourName put “Your Name” You are 22 yrs old Margaret Your Name put 8+8 put “8+8= ”, = 16 Output?
–Launch Turing program… provides programming environment and the Turing translator program (a compiler ), all in one. –Program entry & editing: Editor window. 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
–CLEAN compilation? Machine language instructions are executed, one after the other (that’s when the machine cycles begin….) Run window will pop up if program includes put and/or get –for data entry (user) –for screen prompts, any input data you 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