37
|
1 # loop() gives an infinite loop - let's not run that!
|
|
2
|
|
3 # While loops aren't too unfamiliar:
|
|
4 # while(condition, code)
|
|
5 # where calls can be put in a "block" with semicolons
|
|
6 i := 1
|
|
7 # Notice the lack of "++"
|
|
8 while(i <= 11, i println; i = i + 1); "This one goes up to 11" println
|
|
9 "Or we could have put the line here" println
|
|
10
|
|
11 # For loops are similarly simple:
|
|
12 # for(variable, min, max, code)
|
|
13 for(i, 1, 11, i println); "For this one also goes up to 11" println
|
|
14
|
|
15 # Oddly (but necessarily) the increment is in the middle of that as an optional parameter
|
|
16 increment := 2
|
|
17 for(i, 1, 11, increment, i println); "For this one goes to 11 in 2s" println
|
|
18
|
|
19 # This is bad in some cases, because Io accepts extra args without complaining
|
|
20 # BUT it'll fill in extra args first
|
|
21 # AND things have potentially unexpected return values (e.g. "11 println" returns 11)
|
|
22 # Although it's actually just returning *something* so that you can chain more methods
|
|
23 # and in many cases then "self" is the best return, e.g. "11 println println" prints twice
|
|
24 for (i, 1, 10, 2, i println, "extra arg gets ignored")
|
|
25 "Bad arg example" println
|
|
26 for (i, 1, 10, /* no increment arg, so command runs *and* becomes increment*/ i println, "extra arg becomes message")
|
|
27 # However, we apparently only see the last print! Seems odd and unexpected - unless we're taking it from the previous call?
|
|
28 for (j, 1, 10, /* no increment arg, so command runs *and* becomes increment*/ j println, "extra arg becomes message")
|
|
29 # Yep, this crashes out!
|
|
30
|