intro to programming with raptor
TRANSCRIPT
-
8/9/2019 Intro to Programming With RAPTOR
1/14
D. Introduction to Programming with RAPTOR
By Lt Col Tom Schorsch
The RAPTOR Programming Language
RAPTOR is a visual programming language based on flowcharts. Flowcharts were developed asa design aid for developing programs; however, with RAPTOR they are a programming language
in their own right as the flowcharts you develop can be eecuted by the RAPTOR development
environment.
!e use RAPTOR in "#$$% for several reasons. First, the RAPTOR language has minimal
synta &grammar' when compared with other programming languages. This ma(es Raptor
programs easy to write as there are fewer language elements that you have to learn to beproductive. #econd the RAPTOR language is visual; RAPTOR programs are diagrams &directed
graphs'. One of the hardest aspects of programming to understand and get right is the flow of
control of the statements in your program. The visual nature of the flow of control in RAPTOR
programs ma(es them easier to understand and program. Third, we developed the programminglanguage and development environment. !e sought to ma(e the development environment easy
to use and the error messages that RAPTOR displays to you easy to understand. !e areconstantly trying to improve RAPTOR, so if you have suggestions please tell us. Fourth, our
purpose is not to teach you how to program in particular language. !e want to teach you how to
design and eecute algorithms to solve problems, and to implement algorithms using computer)based problem solving tools. These ob*ectives do not re+uire a heavy)weight commercial
programming language such as " or -ava.
!hat will you be missing by using RAPTOR instead of some other more conventionalprogramming language First, you will miss using a comple development environment that has
been designed to be useful to professional software developers but is etremely difficult to use inan educational environment by novice programmers. #econd, you will miss programming in alarge comple programming language with comple synta and semantics. Third, you will not
see cryptic error messages describing the multitude of synta errors you might commit while
programming in the comple programming language using the comple programmingenvironment.
RAPTOR Program Structure
A RAPTOR program is a directed graph. !hen eecuting a program, you begin at
the Startsymbol at the top and follow the arrows to eecute the program. Raptor
programs end with the Endsymbol. The smallest RAPTOR program &which does
nothing' is depicted at the right. /y placing additional RAPTOR statementsbetween the Start and End symbols you create more meaningful RAPTOR
programs.
0)$
-
8/9/2019 Intro to Programming With RAPTOR
2/14
RAPTOR Variables
1ariables are memory locations that hold a value. At any given time a variable can only hold asingle value of a particular type of data, however, as the program eecutes, the data value stored
in the variable can change. They are called variables because the data stored by them can vary as
the program eecutes.
As an eample, the RAPTOR statement X32assigns the data value 32 to
the variable X. 2f that statement is followed by the statement XX+1 the
value of 32is retrieved from X, the value 1is added to it, and the result &33'
is stored bac( in variable X replacing the value that was previously stored
there. Thus, in the program at the right, the variable 3 initially had no value,
then it is assigned the value 32, then it is assigned the value 33, and finally it
is assigned the value 66. 2f you are reading this on)line &and have RAPTOR
installed' you can eecute the program by double)clic(ing here4 Variablevalue changing over time Examplerap. 5ou can step through the program
and see the value of variable 3 change by clic(ing on the button.
A variable is normally used to hold a value that is not (nown before the
program starts running. This value could be read in from the user, or itcould be computed from other values, and so the eact value that is
stored in the variable may vary every time the program is run &another
reason why it is called a variable'.
1ariables are one of the most important programming concepts as all
code involves the processing of data that is stored in variables. 2t isvariables, and their changing data values, that enable the same programs
to act differently every time you run them and to solve different versionsof the same problem. The program at the left gets a value from the user,the variable Numbercould have a different value each time the program
is eecuted. Variable value !i""erent each time the program is run
examplerap
An instructor will often as( a student to tell them the value of a variable. !hat they are as(ingfor is the value that was last assigned to that variable or was read into that variable at a
particular time during the programs execution. The first assignment of a value to a variable is
called initializing a variable. 1ariables do not automatically have values. 2f you try to use avariable6s value before it has been given one, epect a run)time error li(e the following.
0)7
-
8/9/2019 Intro to Programming With RAPTOR
3/14
-
8/9/2019 Intro to Programming With RAPTOR
4/14
RAPTOR Statements
RAPTOR has > basic statements4 2nput, Output, Assignment, "all,
#election, and Eoop. Dach of these statements is indicated by a differentsymbol in RAPTOR as shown at the right.
The #election and Eoop statements are also called control structurestatements. They are control statements because they control how theprogram eecutes. They are structured statements because they can
enclose other statements.
The RAPTOR program
on the left contains
eamples of some of the> basic statements. The
comment eplains what
the program does. oticehow the loop statement
controls the eecution
flow and encloses otherstatements. Those of you
that have read 0ouglas
Adam6s boo(, The
Hitchhiker's Guide to the
Galaxy will understandthe reference.
2f you are reading this on)line you can eecute the program by clic(ing on4 The #ltimate$uestion o" Li"e% the #niverse an! Everythingrap,
0)
-
8/9/2019 Intro to Programming With RAPTOR
5/14
The RAPTOR program to the right
contains eamples of the remainingbasic statements.
The comment eplains what theprogram does. otice how the
selection control statement encloses
other statements and that only one ofthe statements will be eecuted. To
try it yourself, clic( here4
&ra'(Examplerap.
Statement
or
Instructio
n
A programming language instruction to the computer. An eecutable
statement that causes a specific action to occur when the program is run.
The Assignment, "all, 2nput, and Output statements are described below. A separate reading
describes the #election and Eoop control structure statements.
Assignment Statement
Programming often involves using formulas to compute
some value. The assignment statement is used to perform
a computation and then store the results in a variable. The
value stored in the variable can then be retrieved and usedin a later statement in the program.
The dialog bo at the right is used to enter both the name
of the variable being assigned and the computation that
will be evaluated and whose result will be assigned to thevariable.
Syntax "or an assignment statement1ariableG DpressionG or Set 1ariableG to DpressionG
An eample assignment statement is4
Cost (Tax_Rate Non_!ood_Tota"# + !ood_Tota"
0)=
Assignment #ymbolDpression to be evaluated
and assigned during run time
-
8/9/2019 Intro to Programming With RAPTOR
6/14
Expression
Any se+uence of literals, variables, operators, etc. that, during run)time, can
be evaluatedto produce a single value. That value is referred to as the resultof the expression.
The semantics for an assignment statement are4 Dvaluate the epression on the right hand side of the assignment operator.
Ta(e the value that was calculated and place it in the memory location associated with the
variable, replacing whatever data value had been stored there previously.
Assignmen
t
An assignment statement is used to modify or replace the data stored at thememory location associated with a variable.
Built in Operators and Functions
An operator or function directs the computer to perform some computation on data. Operatorsare placed between the data being operated on &i.e. X$3, %+&, etc.' whereas functions use
parentheses to indicate the data they are operating on &i.e. s'rt(), s*n(2)#'. !hen
eecuted, operators and functions perform their computation and return their result. RAPTOR
has the following built)in operators and functions.basic math4 +, ,, , $, -, , rem, mod, s'rt, "og, abs, .e*"*ng, /"oor
trigonometry4 s*n, .os, tan, .ot, ar.s*n, ar.os, ar.tan, ar..ot
relational4 0, 0, HI, , , 0, 0
logical4 and, or, not
miscellaneous4 random, 4engt5_o/
The basic math operators and functions include the familiar &+, ,, , $' as well as some that areunfamiliar.
and -are eponentiation, e 2is 16 3-2is
rem&remainder' and mod&modulus' return the remainder &what is left over' when the
right operand divides the left operand, e 17 rem 3is 1, 17 mod 3is 1also
s'rtreturns the s+uare root, e s'rt(#is 2
"ogreturns the natural logarithm, e "og(e#is 1
absreturns the absolute value, e abs(,#is
.e*"*ngrounds up to a whole number, e .e*"*ng(3)118#is
/"oorrounds down to a whole number, e /"oor(17$3#is 3
+also wor(s as a concatenation operation to *oin two strings or a string and a number,e 9T5e average *s : + (Tota" $ Number#
4engt5_;/returns the number of characters in a string variable &also the number of
elements in an array which you will learn about later', e Name 9Stu//:
followed by 4engt5_;/(Name#is 8
5ou should be familiar with the trigonometric functions &s*n, .os, tan, .ot, ar.s*n,
ar.os, ar.tan, ar..ot'. They wor( on angles whose units are radians. &i.e. you must
0)>
1ariable being assigned
-
8/9/2019 Intro to Programming With RAPTOR
7/14
-
8/9/2019 Intro to Programming With RAPTOR
8/14
The logical operators are defined by the following tables. The operands used by the logical
operators should be :/oolean< values &meaning the values returned by relational operators or
logical operators'.
Dpression ResultTrue andTrue TrueTrue and!a"se !a"se!a"se andTrue !a"se!a"se and!a"se !a"se
Dpression ResultTrue orTrue TrueTrue or!a"se True!a"se orTrue True!a"se or!a"se !a"se
Dpression Resultnot(True# !a"senot(!a"se# True
0)J
-
8/9/2019 Intro to Programming With RAPTOR
9/14
The random function returns a number between % and $, e Xrandom could be %, %.7@,
%.>=?J, etc. 2f you need a random number in a different range then you can combine the
randomfunction with other operations. For eample, random177will evaluate to a number
between 7 and 177. .e*"*ng(random177#will evaluate to a whole number between 1
and 177.
Built in Constants
"onstants are pre)defined variables whose values cannot be changed. RAPTOR has the
following built)in constants.
constants4 =*, e, true, /a"se, 2>1&8176.
Trueand %esare defined to be 1.
!a"seand Noare defined to be 7.
The constants True, !a"se, %es, and Noare used by the RAPTOR eecution system to
determine the results of a decision.
Proce!ure Call statements
5ou have heard that a pilot follows ta(e)off and landing procedures, a car mechanic follows a
procedure to change your oil or replace your transmission, and you probably have a procedurefor shining your shoes. 2 follow the change)oil procedure myself, but 2 get a car mechanic to do
the replace transmission procedure for me when that becomes necessary. Typically, a procedure
has something that it is acting upon, li(e the specific transmission and car. #imilarly, a calledprocedure often must be supplied data, and it often returns data. 2n this class you will only be
calling procedures, not creating procedures.
A procedure is a named collection of programming statements that accomplish a tas(. "alling aprocedure suspends eecution of your program, eecutes the steps associated with the called
procedure, and then returns bac( and starts eecuting your program again. 5ou do not have to
see the instructions or even (now what they are in order to use a procedure; you *ust have to(now what the procedure will do to use it properly.
To use an eisting procedure, you must callit by invo(ing its name and providing it with data
that it needs. 5ou may call a procedure many times sending it different data each time.#imilarly, a car mechanic may (now the procedure for replacing a transmission &or it could be
written down in a boo(' and could eecute that procedure on many different cars and rebuilttransmissions. Thus you can tal( and reason about the procedure separately from the specific
item &be it a specific transmission or a specific piece of data' a procedure is acting upon.
The 0raw Dample &several pages ago' contained @ eamples of procedure calls which arerepeated below. The first opens up a graphics window that is $%% piels wide by $%% piels high.
0)B
-
8/9/2019 Intro to Programming With RAPTOR
10/14
The second draws a circle centered at piel =%, =% with a radius of 7= piels. The last draws a
bo that has a lower left corner at piel 7=, 7= and an upper right corner at piel ?=, ?=. /oth the
circle and s+uare are blac( in color and both are filled. /y changing any of the data being passedinto the procedure, you change what the procedure does.
Built in Procedure
The de"a
-
8/9/2019 Intro to Programming With RAPTOR
11/14
Output statements
2n RAPTOR, an output statement is a special type of procedure thatdisplays a value to the output screen. A dialog bo as(s you to identify whether you are writing
out an epression or tet &using a radio button' and whether a new line will be output &using a
chec(bo'. The dialog boes depict different eamples depending on whether umber or Tetoutput is chosen &see below'.
!hen outputting tet, you can place additional spaces after the tet as shown in the dialog bo to
the left above and in the RAPTOR program to the right above. This will cause the umberoutput to be separated from the tet by a space. !hen outputting an Dpression, you can write
out a mathematical epression as well as a variable as shown in the dialog bo in the middle
above.
5our instructor &or your assignment' will also often say :0isplay the results in a user)friendly
manner
-
8/9/2019 Intro to Programming With RAPTOR
12/14
Selection an! Loop statements
As mentioned previously, the selection and loop statements are described in a separate reading.
Comments in RAPTOR
RAPTOR, li(e many other programming languages, allows you to add comments to yourprogram. The sole purpose of a comment is to eplain some aspect of a program. "omments
should inform or add information for the reader, especially in places where the program code is
comple and hard to understand. "omments are not eecuted and mean nothing to the computerbut can be very informative to the human reader.
CommentDplanatory tet that is written for the human reader of the program code.
"omments are not instructions to the computer.
0)$7
-
8/9/2019 Intro to Programming With RAPTOR
13/14
*hat 'e have learne!+
!e understand the basic structure and parts of a RAPTOR program.
!e (now what the arithmetic, logical, and relational operators are and what they can do.
!e (now purposes of and how to use the > basic RAPTOR statements.
!e (now how to display information in a user)friendly fashion.
!e understand what an epression is and can evaluate an epression to determine its value.
!e can assign a value to a variable using an assignment statement
Rea!ing Sel",Chec-
$. Eabel the following RAPTOR identifiers as &L' legal or &)' 2llegal. 2f illegal then eplain why.KKKK $' T5*s_@s_A_Test
KKKK 7' _2
KKKK @' Bone
-
8/9/2019 Intro to Programming With RAPTOR
14/14
KKKKKKKKKK ' 6 2
KKKKKKKKKK =' 12 13
KKKKKKKKKK >' 38 7
KKKKKKKKKK ?' 127 $0 672
KKKKKKKKKK J' 6 )8
KKKKKKKKKK B' True and!a"seKKKKKKKKKK $%' (!a"se and!a"se# orTrue
KKKKKKKKKK $$' &8)6 32)# or(1 2>#
KKKKKKKKKK $7' > 17 27
KKKKKKKKKK $@' True and&
KKKKKKKKKK $' 121)86
KKKKKKKKKK $=' && + ,112>
KKKKKKKKKK $>' &@ L @'LL7 J% or&True an!&>?.= $7$.=''
=. 2dentify the following as &A' Arithmetic, &E' Eogical, or &R' relational operators.
KKKK $' 0
KKKK 7'
KKKK @' $
KKKK ' not
0)$