“If I had won the lottery, I would have bought a boat.” In this sentence, it is impossible for the condition to be fulfilled, because it refers to an event in the past. I neither won the lottery, nor bought a boat.
Like the English language, programming languages make extensive use of conditional statements. When writing programming functions, conditional clauses are always given a boolean value of true or false. If the sentence in the lottery example was implemented into code in a conditional statement, it would return ‘false’, because neither condition can be fulfilled.
Web developers and other programers can combine multiple conditional clauses to make complex conditional statements. For example, suppose I have two php values called $p and $q, with values of “2=2” and “3=5”, respectively. $p is true and $q is false. I can compare these expressions with ‘and’ and ‘or’. $p AND $q returns ‘false’, because something cannot be both true and false simultaneously. The statement $p OR $q returns ‘true’, because at least one of the statements is true.
Conditionals become increasingly difficult when one or more ‘NOT’ clauses is present. A truth table can aid in deciphering the logic behind these complex conditional statements.
p | q | p AND q | p OR q | NOT (p OR q) | NOT p | NOT q | (NOT p) OR (NOT q) |
T | T | T | T | F | F | F | F |
T | F | F | T | T | F | T | T |
F | T | F | T | T | T | F | T |
F | F | F | F | T | T | T | T |
If you know the boolean value for both p and q, you can apply them to the table. For example, if you are looking to solve for either NOT p or NOT q, and p and q are both true, the conditional would return false, as indicated by the first line of the table. Truth tables are a handy way of quickly determining the outcome of your conditional statement.
By: Sarah
The comments are closed.
No reviews yet