When a RAPTOR program begins its execution, no variables exist. The first time RAPTOR encounters a new variable name, it automatically creates a new memory location and associates this variable name with the new memory. Therefore, in RAPTOR you don’t need to declare variables explicitly, as we probably do in the pseudocode with Declare statements.
The variable will exist from the point at which it is created in the program execution until the program terminates.
Note that any initial value that is enclosed in quotes is considered text(String) e.g. :
A variable’s data type cannot change during the execution of a program.
Now lets provide details about each of the basic symbols: Input,
Assignment, and Output. The other symbols will be covered later.
At this point it is helpful to point out a situation that occurs frequently when coding an output text. Imagine you want the following output to explain to the user how to enter a choice:
Do you want to play the game again? Type "y" for yes, "n" for no.
Often the output includes some text that needs to be wrapped in quotes. However, a String, to the computer, is any text enclosed in quotes. The computer understands that the beginning of a String starts with the first quote symbol (") and ends when it encounters a second quote symbol ("). If the output text shown above was to be stored in a variable named YourChoice and was coded as follows:
YourChoice = "Do you want to play the game again? Type "y" for yes,
"n" for no."
the computer would process "Do you want to play the game again? Type" as the String to be stored in YourChoice and then display an error because the next character, y, is unintelligible to a computer interpreter or compiler.
That is why we not must nest quotes within a String. We must use a different symbol for internal quotes (those that are part of the String text) than the outer quotes, which mark the beginning and end of the String. In this case, a single quote (') indicates that we are still inside the original text String. In some languages, the outer quotes must be double quotes ("), while in other languages double quotes can be nested inside single quotes as well as the other way around.
The correct way to write the code for the YourChoice variable, then is:
YourChoice = "Do you want to play the game again? Type 'y' for yes,
'n' for no."
|Using the Input Box to Change the Value|
of a Declared Variable
|Using the Input Box to Declare and Initialize|
Assignment SymbolThe Assignment symbol is used to set the value of a variable or to perform a computation and then store the results in a variable. The value stored in the variable can be retrieved and used in a statement later in the program.
The dialog box shown in Figure is used to enter the name of the variable being assigned and its value.
The variable name is entered in the small text box: Set ________.
The value is entered in the large text box: to ________. This value can be a number, character, or a string of text. If this is the first time the variable has been used, its type is set by the value you enter. If you type a number, the variable becomes a Number type.
You can also type in a computation for the value of a variable. The result will be assigned to the variable.
Here is how RAPTOR processes an Assignment statement:
- First, the expression on the right-hand side of the Assignment operator is evaluated.
- Then, that value is placed in the memory location associated with the variable on the left side, replacing whatever data value had been stored there previously.
The expression (or computation) of an Assignment statement can be any simple or complex equation that results in a single value. An expression is a combination of values (either constants or variables) and operators. A computer can perform only one operation at a time. RAPTOR follows the same mathematical order as used in other languages.
An operator or function directs the computer to perform some computation on data. Operators are placed between the data being operated on (e.g., X + 3), whereas functions use parentheses to indicate the data they are operating on (e.g., sqrt(4.7)).
When executed, operators and functions perform their computation and return their result. The result of evaluating an expression in an Assignment statement must be either a single number or a single string of text. While many expressions will compute numbers, you can also perform simple text manipulation in RAPTOR by using a plus sign (+) to join (concatenate) two or more strings of text into a single string. You can also join numerical values with strings to create a single string. The following Assignment statement demonstrates string manipulation:
Full_name = "Joe " + "Alexander " + "Smith"
The value of Full_name, after this Assignment statement, is Joe Alexander Smith.
In RAPTOR, an Output statement displays a value to the MasterConsole screen when it is executed.
When you define an Output statement by clicking on the Output symbol in your flowchart, the Enter Output dialog box asks you to specify the text or expression to display and whether or not the output should be terminated by a newline character.
If you check the End current line box, any future output will start on a new line below the displayed text.
You can output text, the value of a variable, or any combination of text and variables.
Note: For the user we cannot just list the values of the variables; we need to add some explanation of what these values represent in the MasterConsole screen.
The RAPTOR development environment, like many programming languages, allows comments to be added to your program. All good programs include comments if the code itself is not self-explanatory. While in the beginning, comments often seem redundant or even downright silly since beginning programs are small and not particularly complex, it is a good idea to get into the habit of including comments.
To add a comment to a symbol in your flowchart, right-click the mouse button on the statement symbol(rclick in Linux get mostly in an unhandled exception so use Edit > Comments in Menu bar ) and select the Comment line before releasing the button. Then, enter the comment text into the Enter Comment dialog box. The resulting comment can be moved in the RAPTOR window by dragging it, but normally you would not need to move the default location of a comment.
Typically, you should not comment every statement in a program but should include comments before each new section of a program.
Documentation Is Always Important
Often a program can achieve the desired results in one of several ways. The decision about which code to use is up to you, the programmer. While many examples are short and simple,real programs may be much longer and more complicated.
Professional programmers often work on code written by other people. It is easier for a programmer to edit someone else’s code if the programmer knows what the segment is designed to do. This is one reason why it is so important to use documentation in the form of comments to explain what your code is doing.
The Assignment Operator and the Comparison Operator in RAPTOR
RAPTOR allows you to use either a single or a double equals sign to indicate a comparison within a test condition. RAPTOR “understands” that a single equals sign indicates comparison by its placement in the diamond shape. RAPTOR also accepts the double equals sign as a comparison operator.
For example, if the condition you wish to check is “Is the value of Count equal to 5?” you could enter either of the following into the Enter Selection Condition Count = 5 or Count == 5 .
It's better use the double equals sign to indicate a comparison because it
is an important distinction which, in a typical programming language, can wreak havoc on a program if used incorrectly.
The Call Symbol and SubchartsThe Call symbol, is used to call submodules from the main program, as these submodules, named subcharts in RAPTOR, are needed.
Subcharts help break a RAPTOR program into logical parts, called as needed by the main program, to simplify design, ensure that flowcharts are manageable, and reduce the chance for errors.
When you start a RAPTOR program, you’ll see a main tab in the upper-left corner of the work area. To create a subchart, simply right-click the main tab and select Add subchart from the menu. Enter the name of your subchart and a new editing window will appear. The name you give your subchart is automatically created in this new window after you click yes when prompted.
|a new subchart named Calculations|
A subchart may be called from the main flowchart or from other subcharts or even from within itself but be careful if you decide to try this! It can easily result in an infinite loop (a loop that runs forever).
As the program runs, when the Call is encountered, control will transfer to the subchart. After all the steps in the subchart are completed, control will automatically return to the next symbol after the Call.
In RAPTOR, each variable declaration uses a separate Assignment symbol. Initializing more than a few variables results in a long list of Assignment boxes that do not have much to do with the logic of the program. Therefore, for convenience, we can call a RAPTOR subchart as soon as the program begins, which will contain all our initial variable declarations. It keeps this long list of Assignment boxes out of the way and allows us to focus on the rest of the program.
You will now see your main work area and a new tab at the top, named Variables. You can click on the Variables tab and begin creating variables.However, it is not necessary and, if you wish, you can initialize your variables in the main program. Since a variable’s data type in RAPTOR is created when the variable is given a value, we will create most of our number variables by giving them an initial value of 0 and most string variables by giving them an initial value of an empty space (" "). Some variables, our named constants, have predetermined values that will not change as the program runs. We can set these values now.
Testing the program at each submodule makes debugging a great deal easier than waiting for the end when you have many more combinations of tests to run and where it may be difficult to pinpoint the source of a bug. In RAPTOR, as you run your tests, look at the watch window on the left side to be sure that your variables are being assigned the correct values.