Home
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Lego
Through
the Origin

Squares (6 and last)

(previous pagetutorials)

But One Last Thing!

What if you type nonsense into the number field and then try to compute the square?  Try it by typing letters instead of digits for example.

Happily this comes up:

I'll discuss debugging much later, debugging is very important to get your programs to work.

For now, we will just make the script robust:  change the MouseUp handler of the Compute button:  (choose the selection tool instead of the browser tool, open the script window and press Apply after you have edited it)

on MouseUp

put field "Number" into x

if x is not a number then

answer "Not a number"

exit MouseUp

end if

put x*x into field "Square"

end MouseUp

And now if I type rubbish in the number field and try to compute the square, we get:

 A few remarks about the changes:

The if is a test, which can pass or fail.  If x is not a number, it passes and the statements after the then are executed.  If x is a number, then the test fails and the statements between the then and the end if are not executed, they are skipped.

The statements between the then and the end if are indented to the right to show clearly that they are dependent on the test.

Again, the end if is itself not a statement but an indicator of how far the list goes that depends on the test.

The statement exit MouseUp ends the actions of the MouseUp handler immediately.

A variant

Here is a variant:

on MouseUp

put field "Number" into x

if x is not a number then

answer "Not a number"

else

put x*x into field "Square"

end if

end MouseUp

Here you can see a fuller version of the if statement.  It has two parts:

One can argue at length whether this version is better than the first one or not.

Personally I prefer the first one, because it deals early with the possible user errors and quits without mixing the job of computing the square with the if-then-else.

 

Tip:  If your program text does not line up correctly, press the tab key to make Revolution do the indentations (the tab key sits on the left of the keyboard, above the shift-lock key).

At the end of each tutorial I will put a summary of what has been learned.

(previous pagetutorials)





next planned revision: 2006-11-30