Intro To Programming With RAPTOR
Intro To Programming With RAPTOR
D-1
RAPTOR Variables
Variables are memory locations that hold a value. At any given time a variable can only hold a
single value of a particular type of data, however, as the program executes, the data value stored
in the variable can change. They are called variables because the data stored by them can vary as
the program executes.
As an example, the RAPTOR statement X32 assigns the data value 32 to
the variable X. If that statement is followed by the statement XX+1 the
value of 32 is retrieved from X, the value 1 is added to it, and the result (33)
is stored back in variable X replacing the value that was previously stored
there. Thus, in the program at the right, the variable X 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. If you are reading this on-line (and have RAPTOR
installed) you can execute the program by double-clicking here: Variable
value changing over time Example.rap. You can step through the program
and see the value of variable X change by clicking on the
button.
A variable is normally used to hold a value that is not known before the
program starts running. This value could be read in from the user, or it
could be computed from other values, and so the exact value that is
stored in the variable may vary every time the program is run (another
reason why it is called a variable).
Variables are one of the most important programming concepts as all
code involves the processing of data that is stored in variables. It is
variables, and their changing data values, that enable the same programs
to act differently every time you run them and to solve different versions
of the same problem. The program at the left gets a value from the user,
the variable Number could have a different value each time the program
is executed. Variable value different each time the program is run
example.rap
An instructor will often ask a student to tell them the value of a variable. What they are asking
for 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. Variables do not automatically have values. If you try to use a
variables value before it has been given one, expect a run-time error like the following.
D-2
All variables should be given meaningful names by the programmer. The names that you use for
variables should be easily understood and relate to the purpose the variable serves in your
program. Remember, a variable name must start with a letter and can contain letters, numbers,
and underscores (but no spaces or other special characters).
One way of understanding the purpose of variables is to think of them as a means to
communicate information between one part of a program and another. By using the same
variable name in different parts of your program you are using the value that is stored at that
location in different parts of your program. Think of the variable as a place holder or storage
area for values between their use in computations. Most of what a program does is to place
values into variables and retrieve values from variables. When you get information from the user
of a program, that information must be stored in a variable in order for you to use it later. When
you perform a mathematical computation via an assignment statement, the resulting value is also
stored in a variable for later use.
Many programs are based on getting data, processing the data, and displaying the results of your
processing. All of this cannot be done without variables. For example, first you get data from
the user and store that data in variables. Second, you perform some sort of computation using
those variables (and the data that was entered and stored in them) and then store the results in
still more variables. Finally, you display the results of your computations to the user by
displaying the computed values that were stored in variables.
Variable
A variable is a value holder. The value of a variable can change during the
execution of the program. Physically, variables are memory locations that hold
a value. Using a variable identifier (its name) in an instruction enables data to
be retrieved from, or stored to that memory location. All variables start with a
letter and may contain additional letters, numbers, and underscores.
Unlike most programming languages, RAPTOR variables are not declared in a separate section
of the program. Instead, RAPTOR variables are defined upon their first use. All RAPTOR
variables are of type Number or String, meaning they could be a whole number like 12, 567,
-342, etc., a floating point number like -12.4, 3.14159, 0.000369, etc., or a string value like
Hello how are you?, James Bond, Female, etc.
As variables are not declared in advance, it can be easy to forget which variables you are using
and to mistype the name of a variable. Either of these problems can lead to your program not
working as you would like it to. For example, the variables Average and Avg are two different
variables as their names are different. Based on their names, both variables probably store the
average of something. However, the RAPTOR development environment cannot read your mind
and determine if Average and Avg should be the same variable so it treats them as different
variables. It is incumbent upon you, the programmer, to consistently use the same name for a
variable in all of its uses in your program.
D-3
RAPTOR Statements
RAPTOR has 6 basic statements: Input, Output, Assignment, Call,
Selection, and Loop. Each of these statements is indicated by a different
symbol in RAPTOR as shown at the right.
The Selection and Loop statements are also called control structure
statements. They are control statements because they control how the
program executes. They are structured statements because they can
enclose other statements.
D-4
The Assignment, Call, Input, and Output statements are described below. A separate reading
describes the Selection and Loop 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 used
in a later statement in the program.
The dialog box 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 the
variable.
Syntax for an assignment statement
<Variable> <Expression>
or
Assignment Symbol
Expression to be evaluated
and assigned during run time
D-5
Expression
Any sequence of literals, variables, operators, etc. that, during run-time, can
be evaluated to produce a single value. That value is referred to as the result
of the expression.
convert from degrees to radians before using these functions.). The arctan and arccot are
the two parameter versions of these functions. (i.e. arctan(X/Y) is written in RAPTOR as
arctan(X,Y)).
In RAPTOR, the relational operators and logical operators can only be used in decisions as part
of Selection and Loop statements, which are discussed more fully in the next reading. Relational
operators are =, != (not equal to), /= (not equal to), <, >, >=, and <=. The relational operators
return a Boolean value true or false (yes or no). For example, the operation X<Y would
return true if the value stored in variable X is less than the value stored in variable Y.
Otherwise false is returned. The results of a relational operation can be used by logical
operators.
D-7
The logical operators are defined by the following tables. The operands used by the logical
operators should be Boolean values (meaning the values returned by relational operators or
logical operators).
Expression
True and True
True and False
False and True
False and False
Expression
True or True
True or False
False or True
False or False
Expression
not (True)
not (False)
Result
True
False
False
False
Result
True
True
True
False
Result
False
True
D-8
The random function returns a number between 0 and 1, ex Xrandom could be 0, 0.23,
0.46578, etc. If you need a random number in a different range then you can combine the
random function with other operations. For example, random*100 will evaluate to a number
between 0 and 100. ceiling(random*100) will evaluate to a whole number between 1
and 100.
Built in Constants
Constants are pre-defined variables whose values cannot be changed.
following built-in constants.
constants:
pi, e, true, false, yes, no
pi is defined to be 3.14159274101257.
e is defined to be 2.71828174591064.
True and Yes are defined to be 1.
False and No are defined to be 0.
The constants True, False, Yes, and No are used by the RAPTOR execution system to
determine the results of a decision.
Procedure Call statements
You have heard that a pilot follows take-off and landing procedures, a car mechanic follows a
procedure to change your oil or replace your transmission, and you probably have a procedure
for shining your shoes. I follow the change-oil procedure myself, but I 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, like the specific transmission and car. Similarly, a called
procedure often must be supplied data, and it often returns data. In this class you will only be
calling procedures, not creating procedures.
A procedure is a named collection of programming statements that accomplish a task. Calling a
procedure suspends execution of your program, executes the steps associated with the called
procedure, and then returns back and starts executing your program again. You do not have to
see the instructions or even know what they are in order to use a procedure; you just have to
know what the procedure will do to use it properly.
To use an existing procedure, you must call it by invoking its name and providing it with data
that it needs. You may call a procedure many times sending it different data each time.
Similarly, a car mechanic may know the procedure for replacing a transmission (or it could be
written down in a book) and could execute that procedure on many different cars and rebuilt
transmissions. Thus you can talk 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 Draw Example (several pages ago) contained 3 examples of procedure calls which are
repeated below. The first opens up a graphics window that is 100 pixels wide by 100 pixels high.
D-9
The second draws a circle centered at pixel 50, 50 with a radius of 25 pixels. The last draws a
box that has a lower left corner at pixel 25, 25 and an upper right corner at pixel 75, 75. Both the
circle and square are black in color and both are filled. By changing any of the data being passed
into the procedure, you change what the procedure does.
Built in Procedure
The delay_for procedure pauses the execution of the program for the number of seconds
specified. delay_for is not a function or a operation and cant be used in an assignment
statement. Instead, it should be used in a call. Ex, delay_for(0.5) delays operation for half
a second.
Input statements
Every programming language has statements that enable the program to get information from the
user of the program via the keyboard and that can display information on the computer terminal.
Without such statements the user could not type instructions to the computer or give the
computer requested information. Nor could the program display messages to the user, provide
results to the user, or request information from the user.
An input statement is a special type of procedure that gets
information from the user of the program so that the program
can act upon it.
In RAPTOR, an input statement displays a prompt to the user
(prompting them to input a value) and then gets a single value
and stores it in a given variable. In RAPTOR, a dialog box asks
you for the prompt to use and the variable in which to store the
entered data.
The prompt should be as explicit as possible. If a value in
particular units is desired (such as a height in feet or meters) you
should mention the units in the prompt.
As you can see by the Enter Input dialog box at the right there are two types of input prompts,
Text prompts and Expression prompts. An Expression prompt enables you to mix text and
variables together like the following prompt: Enter a number between + low + and + high
+ : .
D-10
Output statements
In RAPTOR, an output
statement is a special type of procedure that
displays a value to the output screen. A dialog box asks you to identify whether you are writing
out an expression or text (using a radio button) and whether a new line will be output (using a
checkbox). The dialog boxes depict different examples depending on whether Number or Text
output is chosen (see below).
When outputting text, you can place additional spaces after the text as shown in the dialog box to
the left above and in the RAPTOR program to the right above. This will cause the Number
output to be separated from the text by a space. When outputting an Expression, you can write
out a mathematical expression as well as a variable as shown in the dialog box in the middle
above.
Your instructor (or your assignment) will also often say Display the results in a user-friendly
manner. What your instructor means is that you should not just write out the results numbers,
but you should also write out some explanatory text explaining what the number is. In other
words, you should tell the user what the data is ("The circle volume is "), and then
display the data as was done in the RAPTOR code above and to the right.
D-11
Explanatory text that is written for the human reader of the program code.
Comments are not instructions to the computer.
D-12
3. True or False. In RAPTOR, a variable does not have a value (in its memory location) until a
program instruction gives it a value.
4. Identify both the result of the following expressions or indicate if the expression is in error
Result
__________ 1)
__________ 2)
__________ 3)
46 / 2
46 / 3
46 rem 3
D-13
__________ 4)
__________ 5)
__________ 6)
__________ 7)
__________ 8)
__________ 9)
__________ 10)
__________ 11)
__________ 12)
__________ 13)
__________ 14)
__________ 15)
__________ 16)
46 ** 2
12 < 13
35 > 90
120 /= 60*2
46 < 49.5
True and False
(False and False) or True
(45.6 < 32.4) or (14 < 28)
8 < 10 < 20
True and 7
4**4 < 121.456
77 + -1128
(3 * 3)**2 < 80 or (True and (67.5 < 121.5))
5. Identify the following as (A) Arithmetic, (L) Logical, or (R) relational operators.
____ 1)
____ 2)
____ 3)
____ 4)
!=
**
/
not
D-14