Wednesday, 5 February 2014

Programming Variables

I was recently “motivated” to provide elaborate details of Scratch programming blocks, although, the name of each block is self-explanatory by itself, I can understand some of the blocks remain a challenge, particularly to a complete beginner. In this post, we will demystify the enigmas of some of these blocks.

Let’s start with the “Variables” block. Variables play an important role in computer programming as it provides flexibility to a programmer to write programs. Without it, it’s analogous to driving a car without steering wheel.

To keep the definition simple, a “variable” in programming context, is a storage used to hold or process data during execution of the program and a reference name is usually assigned to it.

We’ll start with a simple program how variables are applied, and a decision-making block will be included to demonstrate how computers make decision.

Say that I want the default Sprite (a.k.a the cat) to move back and forth continuously (or “forever” in Scratch’s term)  on the screen, and the speed of its movement is controlled by the “up” and “down” key of the keyboard.  Old habits die hard, I’ll start with a flow chart as follows

The chart on the left column depicts the main program and this is how it works :
  1.  Set the variable "Speed" to zero
  2.  Insert value of the variable to the “move () steps” block
  3.  If on edge, bounce
  4. Loop back to the “move () steps” instruction.

The chart at the middle and right column represents the blocks to vary the speed. It increments by 0.1 when the “Up Arrow” key is pressed and decreases 0.1 by the “Down Arrow” key. Note that these two blocks are independent of the main block. 

The Sprite remains inactive at the beginning of the program until the “Up Arrow” or “Down Arrow” key is pressed. 

Below is how the blocks are snapped together to make the script :

A variable named  “Speed” is created and inserted to the value of “move () steps” block, its value  is now zero instead of its default value of 10.

By now, any experienced programmer should be able to spot the “bug” in the above example.
Now, where did we go wrong ??

What if the “Down Arrow” key is pressed first at the beginning of the program ???
The value of the Speed will decrease from zero to a negative value and goes on indefinitely as long as the key is pressed. Negative value on the “move ()” block moves the Sprite backwards and positive values move the Sprite forward. You can control the variable to avoid undesirable values that you do not want, but again, why not let the computer does the dirty job ??.

Unless the script is intended that way, you might like to set a minimum and maximum limit of the variable “Speed”.  Let’s say we want the minimum speed to be zero and the maximum
limit to be 25. That is where the decision-making blocks take on the role.  

The revised flow chart is shown below.

Let’s look at how the new algorithm works.
When the program starts, variable “Speed” is set to zero and enters into a loop. The Sprite stays stationary until one of the Arrow keys is pressed.

Assume the “Down Arrow” is first pressed,
  1. The variable “Speed” is decreased by 0.1 from the initial setting of zero. The new value of “Speed” is now -0.1
  2. Next, the decision-making block checks if “Speed” is less than zero. In this case, it holds true and falls into the “if ()” block loop whereby the variable is reset to zero. When the execution within the “if ()” block is completed, it exits the loop and just stops there (as no further instruction is given after the “if ()” block) until the next key press is detected.

The same process applies to the "Up Arrow" key with the exception that the value is positive and limited to a maximum of 25.

And the final script is shown below.

Take a step back and suppose the "move () steps" block was not assigned with a variable and uses its default value instead, the speed of its movement will be constant and there’s no way to make alteration to it while the program is running. Variables help to solve that problem.

I’ll continue with more examples in my next post how variables are updated / varied by its own during the program execution , so long for now.