Operators: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>Efilnikufecin
No edit summary
 
imported>Efilnikufecin
No edit summary
Line 136: Line 136:
     //false && true is inside () so it's first and evaluates to false
     //false && true is inside () so it's first and evaluates to false
     //true && false(from true && false) is 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
     }
     }
</code>
</code>

Revision as of 04:48, 21 January 2007

if(boolean)

determines whether to execute a code block which follows or not.

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

in order to create more complex if statements we need to understand the basic relational operators:

Relational operators
Operator
Operation
acceptable variable types
==
equal to
any
 !=
not equal to
any
<
less than
integer or float
>
greater than
integer or float
<=
less than or equal to
integer or float
>=
greater than or equal to
integer or float

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

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


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
   }



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.