Normal Topic Boolean Logic (like in "If" statements) (Read 346 times)
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Boolean Logic (like in "If" statements)
Dec 4th, 2007 at 6:56pm
Print Post Print Post  
There are several boolean operators that come up in SBasic, usually in the arguments to an "if" statement or a while statements. These are best understood by going under the hood a little and seeing how they actually work.

The arguments to a conditional (like "if" or "while") are a boolean expression. That means an expression that resolves to either a one or a zero. Here is a simple expression:

Company = "Hammer Data"

If we put that in an "If" statement, it would appear as:
Code
Select All
if(Company = "Hammer Data")
{
  // Do stuff
}
else
{
  // Do other stuff
}
 



The expression portion "resolves". In this case resolving is a little bit like solving an equation in math. The "math" operator in this case is the equal sign. It tells SBasic to compare the value in the variable "Company" with the absolute value of the string "Hammer Data". If it finds that these are the same, it "returns" a one. If they are different, it returns a zero. The "if statement" grabs the value the expression returns and if it is a one it runs the code in the first set of curly braces. If it returns a zero, it runs the code in the "else condition" curly braces (if they exist).

All "truth" expression, comparison expressions, return either a one or a zero. So if we put the value 10 in a variable called "count", we would get:
Code
Select All
count = 10 // returns 1
count <> 10 // returns 0
count >= 10 // returns 1
count < 10 // returns 0
etc...
 



The boolean operators (AND, NOT, OR) operate on the one or zero returned by one or more expressions. So, if you have one expression, like "company = "Hammer Data" and it is true (returns a one) and you run that through a NOT operator, the NOT operator will reverse its value and return a zero.
Code
Select All
NOT count = 10 // returns 0
 



OR and AND each take two arguments, the results of two expressions. Each of these two results will either be a one or a zero and the result returned by AND or OR will also be a one or a zero.

In the case of AND, both of the expressions must return a one (be true) for AND to return a one. If either of the expressions returns a zero (is untrue), AND will return a zero:
Code
Select All
1 AND 1 // returns 1
1 AND 0 // returns 0
0 AND 0 // returns 0
 



In the case of OR, if either expression returns a one (is true), OR will return a one:
Code
Select All
1 OR 1 // returns 1
1 OR 0 // returns 1
0 OR 0 // returns 0
 



Expressions can be nested very deeply before they end up returning a one or zero to the conditional statement ("while" or "if") that is resolving the conditional. For that reason it very important to use parenthesis so as to be very clear as to the order of resolution.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Boolean Logic (like in "If" statements)
Reply #1 - Dec 6th, 2007 at 7:27pm
Print Post Print Post  
Thanks for the tutorial, Mark!
  

**
Captain Infinity
Back to top
IP Logged