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 Mind
What’s in the Box? PowerTools 1
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.
PROCESSING
(CPU)
INPUT
(Keyboard)
Store
(RAM)
COMP 4—Power Tools for the Mind
OUTPUT
(Monitor)
What’s in the Box? PowerTools 2
Ideas from Spreadsheet Formulas
• In Excel: part to right of =
expression.
• Spreadsheet expressions can be made up of
–
–
–
–
–
constants
1500 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
COMP 4—Power Tools for the Mind
What’s in the Box? PowerTools 3
Ideas from Spreadsheet Formulas
• 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
COMP 4—Power Tools for the Mind
What’s in the Box? PowerTools 4
Storing INPUT
• 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.
• Turing needs a place to store, keep track, and use
RAM !
input data values: where?
• Two keywords will be required for this:
– var
– get
COMP 4—Power Tools for the Mind
What’s in the Box? PowerTools 5
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.
Address
• Much like what in memory?
• Much like what in a SS?
Cell reference
Note: my shortcut for variable name: varname
– 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 Mind
What’s in the Box? PowerTools 6
• 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,
COMP 4—Power Tools for the Mind
real,
string (define)
What’s in the Box? PowerTools 7
• Examples:
var firstName : string
var age : int
var payRate : real
• 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.
Quotable...
COMP 4—Power Tools for the Mind
What’s in the Box? PowerTools 8
•
•
More about Input
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
COMP 4—Power Tools for the Mind
What’s in the Box? PowerTools 9
• Step through each statement, just like the
computer does: (we’ll run program at computer later)
var firstName : string
get firstName
Turing Environment
Editor Window
RAM
OOT Run Window
RUN
firstName
Mary
var firstName : string
2
get firstName
Mary <ENTER>
Output
• Excel: there is visual representation of the values of all
cells (-> all variables) on the screen at all times.
• 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” This is certainly not a complete
program! What’s missing?
put totPay
– At run time, the run window will display:
Your total pay will be
650
COMP 4—Power Tools for the Mind
or whatever is stored in
totPay at run time.
What’s in the Box? PowerTools 11
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 Mind
What’s in the Box? PowerTools 12
• 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:
Output?
Hello, World!
put “Hello, World!”
put “You are ”, yourAge, “ yrs old” You are 22 yrs old
Margaret
put YourName
Your Name
put “Your Name”
16
put 8+8
put “8+8= ”, 8+8
COMP 4—Power Tools for the Mind
8+8= 16
What’s in the Box? PowerTools 13
How things work
– 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, continued
– 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…
Descargar

An Introduction to Programming