Difference between revisions of "Control Structures"

From Kolmafia
Jump to navigation Jump to search
imported>Dj d
(added repeat/until)
imported>Dj d
(→‎foreach: Added example)
Line 31: Line 31:
  
 
{}
 
{}
 +
 +
"map" must be an aggregate - a map or a slice.  x takes on each value of the map index in turn.  If there is more than one index, x iterates over the first index.
 +
 +
For example:
 +
boolean [int][string] map;
 +
map[15]["test"] = true;
 +
foreach int_index in map
 +
{
 +
  print(int_index); //this will print '15' once, since there is only one valid value for this index
 +
  foreach string_index in map[int_index] //this iterates over the "slice" of the map where 1 is fixed as the index
 +
  {
 +
    print(string_index); //This will print "test" once, since there is only one valid value for this index
 +
    print(map[int_index][string_index]); //this will print "true"
 +
  }
 +
}
 +
 +
So the output is
 +
15
 +
test
 +
true
  
 
See http://kolmafia.sourceforge.net/advanced.html#maps for details.
 
See http://kolmafia.sourceforge.net/advanced.html#maps for details.

Revision as of 18:08, 3 October 2009

if/else

if (boolean)

{}

else if (boolean)

{}

else

{}

while

while (boolean)

{}

repeat until

repeat {} until (boolean)

for

for x from a to b by c

is the general case. You don't need to specify whether it's going up or down - although doing so by using upto or downto does allow a runtime check to make sure you didn't screw up.

If you don't specify "c", it defaults to incrementing/decrementing by 1. The first iteration is at a and the last is at b (that is to say, it goes from a to b, inclusive).

foreach

foreach x in map

{}

"map" must be an aggregate - a map or a slice. x takes on each value of the map index in turn. If there is more than one index, x iterates over the first index.

For example: boolean [int][string] map; map[15]["test"] = true; foreach int_index in map {

 print(int_index); //this will print '15' once, since there is only one valid value for this index
 foreach string_index in map[int_index] //this iterates over the "slice" of the map where 1 is fixed as the index
 { 
   print(string_index); //This will print "test" once, since there is only one valid value for this index
   print(map[int_index][string_index]); //this will print "true"
 }

}

So the output is 15 test true

See http://kolmafia.sourceforge.net/advanced.html#maps for details.