Difference between revisions of "Operators"

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
m
imported>StDoodle
m
Line 10: Line 10:
 
{{eztr|{{eztd| % |Modulo}}}}
 
{{eztr|{{eztd| % |Modulo}}}}
 
}}
 
}}
 
 
==Relational Operators==
 
==Relational Operators==
  
Line 16: Line 15:
  
 
In order to create more complex if statements, we need to understand the basic relational operators:
 
In order to create more complex if statements, we need to understand the basic relational operators:
 +
{{
 +
eztable|
 +
{{eztr|{{eztd| <nowiki>==</nowiki> |equal to}}}}
 +
{{eztr|{{eztd| <nowiki>!=</nowiki> |not equal to}}}}
 +
{{eztr|{{eztd| <nowiki><</nowiki> |less than}}}}
 +
{{eztr|{{eztd| <nowiki>></nowiki> |greater than}}}}
 +
{{eztr|{{eztd| <nowiki><=</nowiki> |less than or equal to}}}}
 +
{{eztr|{{eztd| <nowiki>>=</nowiki> |greater than or equal to}}}}
 +
}}
 +
  
  <table width=95%  cellspacing=0 cellpadding=0>
 
    <tr>
 
      <td><center><b>Relational operators</b></center></td>
 
      <table width=100%  cellspacing=0 cellpadding=1>
 
        <tr>
 
          <td><center><b> Operator </b></center></td>
 
          <td><center><b> Operation </b></center></td>
 
          <td><center><b> acceptable variable types </b></center></td>
 
        </tr>
 
        <tr>
 
          <td><center> == </center></td>
 
          <td><center> equal to </center></td>
 
          <td><center> any </center></td>
 
        </tr>
 
        <tr>
 
          <td><center> != </center></td>
 
          <td><center> not equal to </center></td>
 
          <td><center> any </center></td>
 
        </tr>
 
        <tr>
 
          <td><center> < </center></td>
 
          <td><center> less than </center></td>
 
          <td><center> integer or float </center></td>
 
        </tr>
 
        <tr>
 
          <td><center> > </center></td>
 
          <td><center> greater than </center></td>
 
          <td><center> integer or float </center></td>
 
        </tr>
 
        <tr>
 
          <td><center> <= </center></td>
 
          <td><center> less than or equal to </center></td>
 
          <td><center> integer or float </center></td>
 
        </tr>
 
        <tr>
 
          <td><center> >= </center></td>
 
          <td><center> greater than or equal to </center></td>
 
          <td><center> integer or float </center></td>
 
        </tr>
 
      </table>
 
    </tr>
 
  </table>
 
  
 
The variable (or function results) on the right must be of the same type as is on the left. The exception to this is integer and float in which you can test an integer against a float. See [[(ASHRM) Datatype Conversions ]] for methods allowing cross type checking.
 
The variable (or function results) on the right must be of the same type as is on the left. The exception to this is integer and float in which you can test an integer against a float. See [[(ASHRM) Datatype Conversions ]] for methods allowing cross type checking.

Revision as of 17:20, 13 March 2010

Mathematical Operators

The following operators are used in KoLmafia:

+ Addition
- Subtraction
* Multiplication
/ Division
 % Modulo

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



The variable (or function results) on the right must be of the same type as is on the left. The exception to this is integer and float in which you can test an integer against a float. See (ASHRM) Datatype Conversions for methods allowing cross type checking.

 if(true == true)
   {
   print("this line gets printed");
   }
 if(false == true)
   {
   print("this line never gets printed");
   }
 if(1 == 1)
   {
   print("this line gets printed");
   }
 if(1 == 2)
   {
   print("this line never gets printed");
   }

Boolean Operators

We also need to understand Basic Boolean operators. These only work with boolean:

Boolean operators
Operator
Operation
&&
and
||
or
 !
not


 if(true && true)
   {
   print("this line gets printed because both possibilities proved true");
   }
 if(false && true)
   {
   print("this line never gets printed");
   }
 if(false || true)
   {
   print("This line gets printed because 1 of the possibilities was true");
   } 
 if(!false)
   {
   print("This line gets printed because 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.