Variables and Loops

From Real Software Documentation

Jump to: navigation, search

Aim
In this lesson, we will learn to store information and to repeat actions.


Contents

Variables

In order to do something a fixed number of times, we need a way of keeping track of our count as we go. This count will be a whole number, or integer in mathematical terms. The thing in which we keep this information is called a variable. Think of it as a piece of paper we can write information on. The piece of paper has a name so we can refer to it. When we use the name of a variable in an expression, Real Studio evaluates that name to whatever information is kept in the variable with that name at that moment.

Repeat a Beep

We’re going to create a new project that will repeat an action a certain number of times.

  1. Create a new project.
  2. Drag a Label, a PushButton and a Slider into the window, and set it up so it looks this.
    The user interface for the new project.
  3. Click on the Slider in Window1.
    The Properties pane changes to show its properties.
  4. Set its Minimum property to 1, its Value property to zero, and its Maximum property to 10.
  5. Double-click on the Pushbutton, and set its Action event handler to:
    Dim counter as Integer
    For counter = 1 to slider1.Value
    Beep
    Next
  6. If you’re doing this on Mac OS X, multiple beeps all come out at the same time, so change this code to read:
    Dim counter as Integer
    For counter = 1 to slider1.Value
    MsgBox "Beep"
    Next
  7. Run the project, click the button, move the slider, click the button again.
    Read about the Slider control in the built-in help if you wish.

Dim

We mentioned in an earlier lesson that Real Studio is a compiled programming language, and that this occasionally meant that we had to do things to help the resulting program run faster (these things also help us to catch our own programming errors).

One of the things we have to declare to Real Studio is what type of information a variable will [note 1] If we try to put the wrong type of information into a variable, Real Studio will produce an error.

If we could just use any variable to store any type of information, every time Real Studio wanted to use a value we’d stored in a variable, it would first have to check what type of information that variable contained, and this would slow everything down.

Declaring the type is a kind of contract between you and Real Studio: you promise not to put the wrong type of data into the variable, and Real Studio can rely on this assumption to make the code run faster.

The command to do this is called Dim for historical reasons: Real Studio’s language is based on an older programming language called BASIC (short for Beginner’s All-Purpose Symbolic Instruction Code). Real Studio is much more advanced than the older BASIC language, but it shares its design goal of making things simple for beginners. It also supports the main commands that made BASIC what it is. Also, most old BASIC code will run with simple modifications in Real Studio.

One of those old commands was dim, short for dimension. In those older programming languages, dim was reserved for declaring arrays (we’ll get to arrays in a later lesson), for which the command name made sense. So the name is a little weird now because it has grown out of an older, narrower sense.

You can Dim as many variables in your code as you wish, using as many separate Dim commands as you wish. You can declare a variable anywhere within the code that you wish, but note that you will only be able to use it below the point where you declare it and also, that if you declare it within a loop, you can only use it within that loop (this is actually useful, since if you just need a variable within a small piece of code, you can have it automatically spring into existence and be removed after you’re done with it).

For… Next

The other new element in our code is the For… Next loop, that repeats an action. A For loop has this basic structure:

  • A line that begins with For, followed by a variable name, an = sign, a start value, the word to and a finish value.
  • As many Real Studio commands as you wish, each on a separate line (called the body of the loop); and.
  • The word Next on a separate line.

In our example (For counter=1 to Slider1.value), the variable is named Counter, the start value is 1, and the finish value is the Value property of Slider1.

When Real Studio encounters a For-Next loop, it sets the variable to the start value, then repeatedly checks to see if the variable is greater than the finish value, and until it is, it runs the code in the body of the loop, then adds 1 to the variable.

If you look in the built-in Language Reference, you will see that you can add a Step command to the end of the For line, to get it to add a value other than 1 to the variable each time around. There is also a DownTo keyword for counting backward.

We didn’t need to do it in this case, but you are free to use the value of the variable within the body of the loop, anywhere you need a numeric value. You can also just change the value of the variable, perhaps to make the loop end early. We will see in a moment how to access and change the value of a variable.

Finally, note that the variable must be of a numeric type (integer is the only numeric type we’ve seen so far, and will be the most common control variable in for loops).

Repeating a String

Let’s try something a little more complex:

  1. Change Window1 so it looks this.
    Use a TextField below the top Label.
    Window1 for the second project.

    The large text area below the Slider is a TextArea, not a TextField. A TextArea is similar to a TextField except that it can have more than one line of text and a scrollbar.
    Remember: to change the name of a control, click on it and edit its name in the Properties pane. In this project, theTextField is named SourceString; the larger one ResultString. Also, check the Multiline checkbox for the ResultString box in the Properties pane.
    The slider has the same settings we gave it before.
  2. Add the following code to the PushButton’s Action event handler:
    Dim store as String
    For counter as Integer = 1 to slider1.Value
    store = store + SourceString.text
    Next
    ResultString.text = store

The Dim statement creates a string variable so we have somewhere to build a new string. The Counter variable that is used in the For loop is declared as an Integer inside the For statement.

Within the For loop is a single command,

store = store + SourceString.text

This is an assignment command. It is written as the name of a variable or property, an = sign, then an expression. The result of the expression must be of the same type as the variable or property on the left of the equals sign.

Notice in this case that the variable itself is mentioned in the expression. This is important, so we’ll put it in a box:

The right-hand side of an assignment is fully evaluated, and then it is assigned to the variable or property on the left.

So in this case, we take the current string in the store variable, put the text from the SourceString TextField after it, and then put the result back into the store variable.

In other words, we attach the text in the SourceString TextField to the end of store. And we do this as many times as the current value of the slider (because that’s how many times we run the For loop).

Since when we create a variable, it has an empty kind of value (strings have no characters; numbers are 0), what we get is a number of copies of the string entered into the SourceString TextField.

The final line of the code is another assignment, but this time it is an assignment to a property. In this case, we put the string we just built up into the text of the bottom TextField.

Run the code, so you can see that this is indeed what happens. Don’t forget to type something into the top box before you click on the button.

A Couple of Easy Improvements

This isn’t really the subject of this lesson, but it’s so easy to do, it’s hard to resist:

  1. Copy the code from the PushButton’s Action event handler, and put it in the slider’s ValueChanged event handler.
  2. Run the program again. This time, you don’t need the button. You can even go into the window editor, click on it and press the Delete key.
  3. Now, go to the window again, click on the slider, and turn on its LiveScroll property, then try it all again.

This changes the way the slider calls its event handler. Before, it called the event handler only when you released the mouse button. But with LiveScroll set to True, the event handler is called as you move the slider. Much more satisfying, although if the handler takes too long to run, it might be too slow, so you need to decide when to use this feature.

Experiment

There are many things you could do from here, combining the things we’ve learned so far:

  • Make a program that does what this last one did, unless you type the word “beep” into the SourceString TextField. In that case, it beeps as many times as the slider indicates.
  • Make a program that has a slider and a TextField, and it displays the numeric value of the slider in the TextField. Note that you can turn on the ReadOnly property of an TextField, to stop the user from being able to type into it.
  • Have a program with a button and a slider, in which the slider makes the button move around. (This is surprisingly easy: you can modify anything you can see in the Properties pane in code, by assigning to a property of the button with the same name you see in the Properties pane. If you click on a PushButton control, you’ll see it has a Left property. All you need to is to say something like:
PushButton1.Left = Slider1.Value * 5
  • Take a look at the other properties you see for the controls. You can do all sorts of weird and wonderful things by changing them now.

References

Notes
Personal tools