Programming Concepts : 02 Boolean Algebra

Boolean algebra is the subarea of algebra in which the values of the variables are the truth values true and false, usually denoted 1 and 0 respectively.  – Wikipedia

An expression is a collection of logical operands and logical operators. In boolean algebra, operands are statements like (4<6 , ahmed is taller than Ali). Logical operators are logical AND,OR and NOT. These operators are written in different forms from a language to another).

Boolean Operators evaluation :

   A     B     A AND B     A OR B     NOT A

F     F          F                 F              T

F     T          F                 T              T

T     F          F                 T              F

T     T          T                 T              F

Comparison Operators :

Most of times, boolean expressions involve comparison operators that are evaluated to give a (T or F) value. These comparison operators are like (>,<,=,<=,>=,!=). ** ! means NOT

Expressions are used to ask yes or know question and have the answer as True or False. For an example let’s ask “Is Ahmed taller than Ali?”, assuming that Ahmed is 190 cm and Ali is 170 cm so the expression will return True.

Combining Comparison and Boolean Operators :

Assume that Ahmed wants to join a basketball team. To join this team Ahmed has to be at least 190 cm and less than 95 Kg. So these conditions will be coded into these expressions :

height >= 190 AND weight < 95 

So now each comparison expression must be evaluated to a (True or False) value, then we apply boolean operators to the resulting operands.

For more than two comparison expressions, expressions are evaluated from left to right, and parenthesis can change the order like :

height > 190 AND ( weight<100 OR speed>20)

Notes:

– Multiple conditions separated by OR : at least one true operand to make the result true

– Multiple conditions separated by AND : all the operands must be true to make the result true

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s