Difference between revisions of "Operators"

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
m
imported>StDoodle
Line 1: Line 1:
 
==Mathematical Operators==
 
==Mathematical Operators==
  
The following operators are used in KoLmafia:
+
The following mathematical operators are used in KoLmafia:
 
{{
 
{{
 
eztable|
 
eztable|
{{eztr|{{eztd| + |Addition}}}}
+
{{eztr|{{eztd| + |Addition|Performs addition}}}}
{{eztr|{{eztd| - |Subtraction}}}}
+
{{eztr|{{eztd| - |Subtraction|Performs subtraction}}}}
{{eztr|{{eztd| * |Multiplication}}}}
+
{{eztr|{{eztd| * |Multiplication|Performs multiplication}}}}
{{eztr|{{eztd| / |Division}}}}
+
{{eztr|{{eztd| / |Division|Performs division}}}}
{{eztr|{{eztd| % |Modulo}}}}
+
{{eztr|{{eztd| % |Modulo|Returns the remainder after division}}}}
 
}}
 
}}
 +
 
==Relational Operators==
 
==Relational Operators==
  

Revision as of 17:49, 13 March 2010

Mathematical Operators

The following mathematical operators are used in KoLmafia:

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


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:

== 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 || false && true)
   {
   print("This line gets printed");
   //true or false is true
   //true(from true or false) and true equals true
   }
 if(true && (false && true))
   {
   print("this line never gets printed");
   //false && true is inside () so it's first and evaluates to false
   //true && false(from true && false) is false
   }
 if(true && !(false && true))
   {
   print("This line gets printed");
   //false && true is inside () so it's first and evaluates to false
   //the ! operator converts the false (from (true && false)) to true
   //true && true(from !(true && false)) is true
   }


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.

   if(false)
     {
     print("this line never gets printed");
     }
     else
     {
     print("this line gets printed"); 
     } 



We also need to understand nesting if statements.

 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");
   }



Now you only need to put it all together as needed for your situation.