Operators: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
imported>StDoodle
mNo edit summary
Line 4: Line 4:
{{
{{
eztable|
eztable|
{{eztr|{{eztd| + |Addition|Performs addition}}}}
{{eztr|{{eztd| + |Addition|Performs addition and string concatenation}}}}
{{eztr|{{eztd| - |Subtraction|Performs subtraction}}}}
{{eztr|{{eztd| - |Subtraction|Performs subtraction}}}}
{{eztr|{{eztd| * |Multiplication|Performs multiplication}}}}
{{eztr|{{eztd| * |Multiplication|Performs multiplication}}}}
Line 10: Line 10:
{{eztr|{{eztd| % |Modulo|Returns the remainder after division}}}}
{{eztr|{{eztd| % |Modulo|Returns the remainder after division}}}}
}}
}}
 
Note that, with the exception of using <nowiki>"+"</nowki> for string concatenation, these operators can only be used on int or float datatypes.
==Relational Operators==
==Relational Operators==


Line 82: Line 82:
}
}
</syntaxhighlight>}}
</syntaxhighlight>}}
We also need to understand operator precedence. Statements inside a () pair are always evaluated first, then from left to right.
We also need to understand operator precedence. Statements inside a () pair are always evaluated first, then from left to right.
 
{{
<code>
CodeSample|
  if(true || false && true)
code=
    {
<syntaxhighlight>
    print("This line gets printed");
if ( true || true && false )
    //true or false is true
{
    //true(from true or false) and true equals true
  print( "This line DOES get printed." );
    }
// evaluated left-to-right
  if(true && (false && true))
// true or (true && false) returns true
    {
}
    print("this line never gets printed");
if ( true && ( true && false ) )
    //false && true is inside () so it's first and evaluates to false
{
    //true && false(from true && false) is false
  print( "This line does NOT get printed." );
    }
// ( true && false ) is evaluated first since it is inside of parentheses
  if(true && !(false && true))
// so we end up evaluating ( true && false ) which returns false
    {
}
    print("This line gets printed");
if ( true && ! ( true && false ) )
    //false && true is inside () so it's first and evaluates to false
{
    //the ! operator converts the false (from (true && false)) to true
  print( "This line DOES get printed." );
    //true && true(from !(true && false)) is true
// ( true && false ) is evaluated first since it is inside of parentheses
    }
// the ! operator converts the false from ( true && false ) to true
</code>
// ( true && true ) returns true
<Br/>
}
Using '''else''' we can have a code block which executes when the if statement evaluates to true, and another code block which executes when the if statement evaluates to false.
</syntaxhighlight>}}
<code>
    if(false)
      {
      print("this line never gets printed");
      }
      else
      {
      print("this line gets printed");
      }
</code>
<Br/>
<Br/>
We also need to understand nesting if statements.
 
<code>
  if(true)
    {
    if(true)
      {
      print("this line gets printed");
      }
      else
      {
      print("this line never gets printed");
      }   
    print("this line gets printed also");
    }
  if(false)
    {
    if(true)
      {
      print("this line never gets printed");
      //though inside an if(true) statement,
      //the outer if(false) stops the code from ever getting here.
      }
      else
      {
      print("this line never gets printed");
      }
    print("this line never gets printed");
    }
</code>
<Br/>
<Br/>
Now you only need to put it all together as needed for your situation.

Revision as of 19:19, 13 March 2010

Mathematical Operators

The following mathematical operators are used in KoLmafia:

+ AdditionPerforms addition and string concatenation
- SubtractionPerforms subtraction
* MultiplicationPerforms multiplication
/ DivisionPerforms division
 % ModuloReturns the remainder after division

Note that, with the exception of using "+"</nowki> for string concatenation, these operators can only be used on int or float datatypes. ==Relational Operators== To follow these examples, a basic understanding of the concepts found on [[Control Structures]] would be helpful. In order to create more complex if statements, we need to understand the basic relational operators: {{ eztable| {{eztr|{{eztd| <nowiki>== |equal to}}}}

 != not equal to < less than > greater than <= less than or equal to >= greater than or equal to }} Note that you cannot mix datatypes within a comparison or KoLmafia will abort wil an error, with the exception of mixing types int and float, where KoLmafia will do a transparent type conversion behind-the-scenes. If you need to compare different datatypes, use one or more of the Datatype Conversion functions.

if ( true == true )
{
   print( "This line DOES get printed." );
}
if ( true == false )
{
   print( "This line does NOT get printed." );
}
if ( 1 == 1.0 )
{
   print( "This line DOES get printed." );
}
if ( 1 == 2 )
{
   print( "This line does NOT get printed." );
}



Boolean Operators

&& and
|| or
 ! not

Note that the above operators only work with boolean values & datatypes. To make use of them with other datatypes, you will either need to first perform a Datatype Conversion, or you will need to nest your operations such that a boolean value is used with the boolean operators.


if ( true && true )
{
   print( "This line DOES get printed (both possibilities proved true)." );
}
if ( true && false )
{
   print( "This line does NOT get printed (only one possibility proved true)." );
}
if ( true || false )
{
   print( "This line DOES get printed (since at least one of the possibilities proved true)." );
}
if ( ! false )
{
   print( "This line DOES get printed (since the not operator converted false to true)." );
}

We also need to understand operator precedence. Statements inside a () pair are always evaluated first, then from left to right.

if ( true || true && false )
{
   print( "This line DOES get printed." );
	 // evaluated left-to-right
	 // true or (true && false) returns true
}
if ( true && ( true && false ) )
{
   print( "This line does NOT get printed." );
	 // ( true && false ) is evaluated first since it is inside of parentheses
	 // so we end up evaluating ( true && false ) which returns false
}
if ( true && ! ( true && false ) )
{
   print( "This line DOES get printed." );
	 // ( true && false ) is evaluated first since it is inside of parentheses
	 // the ! operator converts the false from ( true && false ) to true
	 // ( true && true ) returns true
}