“Day 2″ with Io on my more and more misnamed quest. In this chapter Bruce explains basics of conditionals, looping, operator definition and reflection. He also spends a bit more time to explain message passing in Io.
In conditionals and looping section we get overview of some typical looping and branching structures. Such as a simple infinite loop that will keep evaluating until a break:
loop("you spin me right round..." println)
This can come in handy when implementing things such as servers etc. We also get to meet our good old friends (often seen in most languages) for and while loop:
i := 1 while(i <= 10, i println; i = i + 1) for(i, 1, 10, i println) for(i, 10, 1, -1, i println)
First two lines are an example of a while loop. It will print numbers from 1 to 10. While loop takes a condition as a first parameter and it will keep evaluating statements passed in second parameter while the condition holds. Lines 4 and 6 are examples of a for loop. First one prints numbers from 1 to 10 and second one counts back from 10 to 1. This is due to the fact that second for loop utilises the optional step parameter. The parameters of a for loop method are: the name of the counter, the first value, the last value, optional step, and a message with sender. As we can see from example step parameter can be negative. Read on »