Computer Science Canada Io: Control Flow |
Author: | wtd [ Wed Jan 04, 2006 4:01 am ] | ||||||||||||||||||||||||||||
Post subject: | Io: Control Flow | ||||||||||||||||||||||||||||
Loops, loops everywhere! All of our programs need control flow. I've already demonstrated conditionals in my intro so let's look at some other control flow in Io. So, a simple loop in Io?
There are some important things going on here. The "break" message will exit the loop. As for the use of the "=" message instead of ":=", that requires a bit of explanation. The ":=" message sets a slot. In doing so, it creates a new slot. In the above code, this would also have worked, but it's more appropriate to use "=", which updates the existing slot. We can simplify this a bit with a while loop.
We can even further simplify this using a for loop.
Let's say I want to count from zero to six by increments of two.
Here the continue message is used not to break out of the loop entirely, but to skip ahead to the next iteration of the loop. Let's look at an alternate way to use these messages when paired with the "and" message.
We can of course simplify this even further with the other loopig messages, but especially with "for", when provided with a "step" argument.
The loop that wasn't... and then was Programmers familiar with other languages might wonder where the "do...while" loop is. This runs the loop once, then checks a test condition. Fortunately, as alluded to in my previous article on Io, we can easily create new constructs by creating new methods/messages.
The doWhile message will be accompanied by two arguments. The first is a message to evaluate as the body of the loop. The second is the message which comprises the test. Internally we use a plain old loop. We first evaluate the body, then evaluate the test condition. If that condition is Nil (false), then we break out of the loop. We can now use this in a program.
Exceptions Exceptions in Io are straightforward. As with everything else, they are objects. Let's raise an exception.
So, now how do we catch that?
But, what if we need more information about the exception?
We can handle multiple types of exceptions.
And we can handle an exception, then send it up to the next handler.
We can, of course create our own Exception objects.
New above was the super message which sends messages to the object's prototype. In this case the raise message gets sent to Exception. |
Author: | rdrake [ Wed Jan 04, 2006 4:23 pm ] | ||
Post subject: | |||
It should probably be noted that the doWhile method wtd explained in the tutorial was written by him, and the do method in the language does not seem to work. The following is how it should be in the language, provided it actually worked.
|
Author: | wtd [ Wed Jan 04, 2006 4:51 pm ] | ||||||
Post subject: | |||||||
The "do" message does not do what you may expect coming from languages with something like:
You can see it at work in, for instance, the following:
Here it saves us from having to do:
|
Author: | paulkwatyra [ Sat Jan 07, 2006 6:56 pm ] |
Post subject: | |
HOW DO YOU RUN THIS THING???????? |
Author: | paulkwatyra [ Sat Jan 07, 2006 6:57 pm ] |
Post subject: | |
I downloaded it but i cant find an exe |
Author: | wtd [ Sat Jan 07, 2006 7:10 pm ] |
Post subject: | |
Probably requires MinGW. http://zeropage.org/~june/io/ When you download the zip file, unzip it. Inside that you'll find an executable. Run it. |