Control Structures: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
imported>StDoodle
mNo edit summary
Line 76: Line 76:
Note that each "case" can only test against a single value; that value can be a variable itself, but not a test expression.
Note that each "case" can only test against a single value; that value can be a variable itself, but not a test expression.


==Looping==
==Loops==
===while===
===while===
{{
{{
Line 113: Line 113:


===for===
===for===
  for x from a to b by c
{{
 
CodeSample|
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.
code=
<syntaxhighlight>
for x from a to b by c {
  //do stuff
}
</syntaxhighlight>}}
Above 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).
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===
  foreach x in map
{{
 
CodeSample|
  {}
code=
<syntaxhighlight>
foreach key in aggregate {
  //do stuff
}
</syntaxhighlight>}}
Assigns each key in the supplied map or slice to "key" and iterates through the 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.
{{
 
CodeSample|
For example:
description=For example:|
  boolean [int][string] map;
code=
  map[15]["test"] = true;
<syntaxhighlight>
  foreach int_index in map
boolean [int][string] map;
  {
map[15]["test"] = true;
    print(int_index); //this will print '15' once, since there is only one valid value for this index
foreach int_index in map {
    foreach string_index in map[int_index] //this iterates over the "slice" of the map where 1 is fixed as the index
  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(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"
       print(map[int_index][string_index]); //this will print "true"
    }
  }
  }
}
</syntaxhighlight>|
moreinfo=
So the output is
So the output is
  15
<pre>
  test
15
  true
test
true
</pre>}}


Note that instead of nesting foreach statements, for a multidimensional map, two iterators can be used inline.
Note that instead of nesting foreach statements, for a multidimensional map, two iterators can be used inline.
Line 151: Line 168:
   }
   }
See http://kolmafia.sourceforge.net/advanced.html#maps for more details on maps.
See http://kolmafia.sourceforge.net/advanced.html#maps for more details on maps.
===continue/break===
===Continuation & Exiting===
Like many languages with looping structures, ASH supports the break and continue statements.  All looping structures (for, while, repeat until, and foreach) support these statements.
Like many languages with looping structures, ASH supports the break and continue statements.  All looping structures (for, while, repeat until, and foreach) support these statements.


  break;
===break===
Breaks out of the smallest enclosing loop.  In a switch statement, breaks out of the switch statement.  Execution resumes at the first statement after the end of the loop/switch statement.
Breaks out of the smallest enclosing loop.  In a switch statement, breaks out of the switch statement.  Execution resumes at the first statement after the end of the loop/switch statement.


  continue;
===continue===
Continues on to the next iteration of the loop (skipping any statments in this iteration that occur after the continue statement).  In a switch statement, continue is allowed iff the switch is inside a loop, and acts as any other continue.
Continues on to the next iteration of the loop (skipping any statments in this iteration that occur after the continue statement).  In a switch statement, continue is allowed iff the switch is inside a loop, and acts as any other continue.
===return===
Exits the function and returns the value following the return statement, if specified. Note that the value's datatype must match that of the function itself (void functions can only use return by itself).

Revision as of 19:13, 18 April 2010

Conditional

if

if ( boolean )
{
   // any statements here
   // are only going to be executed
   // if the boolean returns true
}

Single-statement conditionals may omit the curly braces.

if ( boolean )
   // curly braces aren't required if only one statement follows the conditional

else

if ( boolean )
{
   // statements if true
}
else
{
   // the statements here
   // are only going to be executed
   // if the boolean returns false
}


else if

if ( boolean 1 )
{
   // statements if true
}
else if ( boolean 2 )
{
   // the statements here
   // are only going to be executed
   // if boolean 1 returns false
   // & boolean 2 returns true
}


switch

switch ( variable )
{
   case value:
      // statements if variable == value
      // more such statements
      break;
   //repeat above for as many values as desired
   default:
      // statements executed if
      // none of the cases were true
}


Note that each "case" can only test against a single value; that value can be a variable itself, but not a test expression.

Loops

while

while ( boolean )
{
   // as with an if statement
   // this area is only entered
   // if the boolean tests true
   // once all this is done
   // it goes back to the begining
   // and will keep executing
   // as long as the boolean remains true
}


repeat until

repeat
{
   // unless I am mistaken
   // this is essentially the same
   // as the while loop above
   // with the one exception being
   // that all of this code
   // will be execute at least once
   // as the test doesn't occur
   // until the very end
} until ( boolean )


for

for x from a to b by c {
   //do stuff
}

Above 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 key in aggregate {
   //do stuff
}

Assigns each key in the supplied map or slice to "key" and iterates through the map.

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


Note that instead of nesting foreach statements, for a multidimensional map, two iterators can be used inline.

 foreach x,y in map

is identical to

 foreach x in map
 {
   foreach y in map[x]
 }

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

Continuation & Exiting

Like many languages with looping structures, ASH supports the break and continue statements. All looping structures (for, while, repeat until, and foreach) support these statements.

break

Breaks out of the smallest enclosing loop. In a switch statement, breaks out of the switch statement. Execution resumes at the first statement after the end of the loop/switch statement.

continue

Continues on to the next iteration of the loop (skipping any statments in this iteration that occur after the continue statement). In a switch statement, continue is allowed iff the switch is inside a loop, and acts as any other continue.

return

Exits the function and returns the value following the return statement, if specified. Note that the value's datatype must match that of the function itself (void functions can only use return by itself).