Conditionals and the Debugger
From Real Software Documentation
Aim
In this lesson, you will learn how to program Real Studio to make simple decisions, and how to use the debugger to watch Real Studio execute code line by line.
Contents |
A simple extension of the previous program.
Let’s get right to it.
- Open the program we saved in the second example in the previous lesson.
- Open Window1’s Window Editor and the PushButton’s list of event handlers in the Code Editor browser, so we are taken to its Action event handler.
- Change the event handler to this:
- Run the application. Type various things in and click the button, then try typing “quit” into the top box.
There are a couple of new commands here (Beep and Quit), whose effects should be obvious. The interesting thing is the If-Then-Else-End If construction that gave us the special behavior when you typed in quit. We will go into If-Then-Else-End If in more detail below. For now, we’re going to use Real Studio’s built-in debugger to watch the code executing.
- Click on the grey dash to the left of the first line (the if command). Notice the red dot this creates next to that line.
- Run the program, type something into the boxes, and click the button.
Real Studio will start running the program, but as soon as it is about to execute the line on which we put the breakpoint, it will bring the Real Studio IDE to the front, switch to the Run tab showing that line, and highlights the line. The highlighting shows the line that Real Studio is about to execute.
You will also see a new Variables pane (as well as a pop-up menu we won’t worry about for the moment).
The Variables pane shows all the information that is available from this part of the program. For reasons we will get into in a later lesson, Self refers to the window, while Me refers to the object in which the code is running (that is, the PushButton). So if you click on one of these links, Real Studio will switch the variables pane to show the current properties of the window or the button:
The pop-up menu above the variables pane tells you which object you’re viewing the properties of at the moment. If you pop it up, lets you back out to view the properties you started from to get to these properties (since properties can be objects which themselves have properties, you can “drill down” through quite a few layers of objects; the menu lets you back out the way you came).
We’ll look into using these features more closely in later lessons, but for now, we’re going to look at how we can step through the code.
- Click the Step button in the Debugger’s toolbar.
The highlighting is now over the next line to be executed (which line this is depends on what you’ve typed into the TextFields in the running program).
In this program, there is no difference between Step and Step Into. Either command will let you step one line at a time through the running program. You can also Stop the running program or choose Resume, which stops the debugger and runs the program normally (until the program gets to a line with a Breakpoint again). You can also add and remove Breakpoints from the debugger, by clicking one of the dashes to the left of the code. If a line of code does not have a dash to its left, you cannot put a breakpoint there.
You can switch between REAL Studio and the running program at any time, although if the program is stopped in the debugger, you’ll find it amazingly unresponsive when you switch to it.
Make sure you use the debugger liberally throughout all of our lessons, to help you see what is going on.
A smarter +
We’re not done with If-Then yet. Let’s look at a richer example.
Since + can be made to work with either strings or numbers, let’s make our project do either one automatically: if whatever has been typed into each box is a number, we will add them together; if not, we will concatenate the two strings together (concatenation is the name of what “+” does with strings).
- Change the event handler to this[note 1]:
(CDbl(Source2.text)=0 and Source2.text<>"0") then
Result.text=Source1.text + Source2.text
Else
Result.text=Str(CDbl(Source1.text)+CDbl(Source2.text))
End if
Again, we‘re interested in the new if-then structure for making decisions, which in its full form looks like this:
As indicated by the … here, there are three places to enter Real Studio expressions and commands, to put together an If-Then in Real Studio:
- On the first line, between If and Then, is a Boolean expression (more on that below);
- After that line, you can have as many lines as you like of Real Studio commands;
- If you wish, you can have a line with the word Else, followed by some more Real Studio commands
And it all ends with End If.
Boolean Expressions
We’ve looked at string expressions and numeric expressions in previous lessons. Boolean[note 1] expressions are very similar, except that Boolean expressions deal only with the values True and False. True and False are values that Real Studio can manipulate, just as it does numbers and strings.
There are two types of Boolean expressions:
- Simple: Boolean expressions state something about a value in the program, generally using operators like = (equals), <> (not equals), <= (less than or equal), or >= (greater than or equal). Sometimes a property or something else in the program will actually just be a Boolean value. The checkboxes you see in the Properties pane are good examples. Source1.Bold is a valid Boolean expression, since there is a Bold checkbox in the Properties pane when you click on one of the TextFields — so the expression Source1.Bold would evaluate to True when Source1’s Bold checkbox is checked; and
- Compound: Boolean expressions are made out of other Boolean expressions (which can themselves be either simple or compound), using Boolean operators Not, And or Or.
As with other types of expressions, you will often need to use parentheses in Boolean expressions to make it clear in what order operations should be carried out. Also, as with other types of expressions, Real Studio works out the final value from the inside out.
Let’s see how Real Studio evaluates the expression in our program:
| Expression | Meaning |
|---|---|
| Source1.text | The contents of Source1. |
| CDbl(Source1.text) | The numeric value of Source1.text if it is a number, or 0 if it is anything else. |
| CDbl(Source1.text)=0 | True if CDbl(Source1.text) evaluates to 0; False otherwise. |
| (CDbl(Source1.text)=0 and Source1.text<>"0") | True if CDbl(Source1.text) evaluates to 0, when the text in
Source1 isn’t "0". In other words, True if the user typed in something that wasn’t a number (remember: something that isn’t a number supplied as the argument to Cdbl evaluates to 0). Note that And between two expressions evaluates to True if both the expressions on either side of it are True. |
| (CDbl(Source2.text)=0 and Source2.text<>"0") | The same, just evaluating what was typed into Source2. |
| (CDbl(Source1.text)=0 and Source1.text<>"0") Or (CDbl(Source2.text)=0 and Source2.text<>"0") | True if either Source1 or Source2 has something other than a number entered into it. |
Notice how we put parentheses in to make the order of the operations clear. There are rules for how Real Studio any expression if you leave out the parentheses:
but it is much simpler and clearer to just put in the parentheses.
You may find it helpful to look at the evaluation of that expression in a kind of diagram called a syntax tree:
Real Studio evaluates the expression from the bottom of the syntax tree up. At the bottom of each line in the diagram is a Boolean expression that has a Boolean value, and at the top of each line is a Boolean operator to combine these Boolean values, forming a larger Boolean expression.
So now you can see how the whole if-then-else construct works in our code. If the user didn’t enter numbers into both boxes, Real Studio concatenates the strings:
Otherwise, the computer adds together the values typed into the boxes (the part after Else):
By the way, the other Boolean operator we haven’t looked at, Not, is very easy to use: When you put Not in front of a Boolean value x, the Not expression comes out True if x is False, and comes out False if x is True. So an equivalent form of our event handler would be:
(CDbl(Source2.text)=0 and Source2.text<>"0")) then
Result.text = Str(CDbl(Source1.text) + CDbl(Source2.text))
Else
Result.text = Source1.text + Source2.text
End if
We did a couple of things here:
We applied the Not operator to the entire expression between If and Then. Notice that we put parentheses around that entire expression, so that Not would be applied to the results of the entire expression, rather than just the first part of it; and
- We swapped the commands between Then and Else with those between Else and End If.
Finally, you need to know that the Else part of an If-Then is optional. So we could just add numbers and ignore anything else with this code:
(CDbl(Source2.text)=0 and Source2.text<>"0")) then
Result.text = Str(CDbl(Source1.text) + CDbl(Source2.text))
Else
Result.text = Source1.text + Source2.text
End if
If Not((CDbl(Source1.text)=0 and Source1.text<>"0") or <br
(CDbl(Source2.text)=0 and Source2.text<>"0")) then
Result.text = Str(CDbl(Source1.text) + CDbl(Source2.text))
End if
End Sub
ElseIf
If you need to make a sequence of decisions, you could put if-then statements inside other if-then statements, like this:
(CDbl(Source2.text)=0 and Source2.text<>"0") then
Result.text = Source1.text + Source2.text
Else
If Result.text = “quit” Then
Quit
Else
Result.text = Str(CDbl(Source1.text) + CDbl(Source2.text))
End if
End if
This is perfectly legitimate and will work as you would expect. However, if you need to do a whole sequence of these, it will all get a bit hard to read, and indented a long way to the right.
Real Studio provides a different way of saying this same thing, with the ElseIf command, that moves the next If up onto the same line as the Else, and only requiring one EndIf at the end. In other words, the code immediately above could be written:
(CDbl(Source2.text)=0 and Source2.text<>"0") then
Result.text = Source1.text + Source2.text
ElseIf Result.text = “quit” Then
Quit
Else
Result.text = Str(CDbl(Source1.text) + CDbl(Source2.text))
End if
Real Studio executes the first If or Elseif part whose Boolean expression evaluates to True, and skips the rest.
Experiment
Make sure you understand how to build different types of Boolean expressions, and don’t be afraid to use parentheses to control the order in which REAL Studio does things. A couple of things you might like to try are:
- Make the program beep and show an error message if the user types in a negative number, or concatenate what they typed in otherwise;
- Make the program quit if the user types quit into both boxes, but add the values if they don’t.
References
- Notes



