# Using Conditional Statements

### General

This article assumes that you have read Computation Rules and Comparisons and Boolean Arithmetic and that you understand the material covered in those two articles.

At times, we will want different values to be computed given the status of the loan, such as having different days that we want to send out the late fee notice depending on how much the original loan amount was. To do this, we use the conditional operator, also known as the “IF” operator.

#### Ideology

The basic idea of the operator is that *if* a condition is true, *then* do a specified action, *otherwise* do a different action or do nothing.

For example, let's say we want to show the number 5 on every Tuesday. We would want an IF statement that said, *if today is Tuesday, show the number 5*. Since we haven’t specified what we want to do any other day of the week, we don’t include our otherwise statement.

Let’s say that we want to show the number 5 on every Tuesday and the number 6 every other day of the week. We would then want an IF statement that said, *if today is Tuesday, show the number 5 otherwise, show the number 6*. Since we specified the otherwise statement we now include it.

IF statements can be chained as well. This can be thought of as *if* condition A is true, *then* do action A *otherwise, if *condition B is true, *then *do action B *otherwise*, do action C. For example, let’s say we want to show 5 on a Tuesday, 4 on a Wednesday, and a 6 otherwise. We would then want an IF statement that said *if today is Tuesday, show the number 5, otherwise if today is Wednesday, show the number 4, otherwise show the number 6*.

#### Using it in LISP

The syntax for the IF operator in LISP is as follows:

(if {*condition} {action if true} [action otherwise]*)

For example, let’s say we want a LISP statement that returns 5 if the remaining interest is less than 50, and it returns 6 otherwise. The LISP statement could look like the following:

(if (< status-remaining-interest 50) 5 6)

Let’s say we want a LISP statement that returns 5 if the remaining interest is less than 50, we want it to return 6 if the amount is between 50 and 75 (inclusive), and we want it to return 7 otherwise. The LISP statement could look like the following

(if (< status-remaining-interest 50) 5 (if (and (>= status-remaining-interest 50) (<= status-remaining-interest 75)) 6 7))

Additionally, we could write the same statement as follows:

(if (< status-remaining-interest 50) 5 (if (<= status-remaining-interest 75) 6 7))

The reason is that we will only reach the second condition if the status-remaining-interest is greater than or equal to 50. Otherwise, statements can be thought of as *if not Condition, then do otherwise action. *For example, consider the statement:

(if (< x y) 5 6)

This could be thought of as saying either *if x is less than y return 5 otherwise return 6* or *if x is not less than y return 6, otherwise return 5*. If you draw out a truth table it can become more obvious.

(if(< x y)5 6)

x | y | x < y | Output |

4 | 7 | T | 5 |

7 | 4 | F | 6 |

(if (not (< x y)) 6 5)

x | y | not (x < y) | Output |

4 | 7 | F | 5 |

7 | 4 | T | 6 |

Please remember that when you do this, not only do you invert the condition but you also must invert the order of the results, otherwise we will get the wrong output.

Now let us us compare the truth tables for the following statements:

(if (< status-remaining-interest 50) 5 (if (and (>= status-remaining-interest 50) (<= status-remaining-interest 75)) 6 7))(if (< status-remaining-interest 50) 5 (if (<= status-remaining-interest 75) 6 7))

Please remember that for us to reach the otherwise statement, the first statement must be false. That means that we must include the negation of the initial statement in our truth table for evaluating if the second condition is true. For brevity, Col2 references the values in the second column, Col3 references the values in the third column, and Col4 references the values in the fourth column.

(if (< status-remaining-interest 50) 5 (if (and (>= status-remaining-interest 50) (<= status-remaining-interest 75)) 6 7))

status-remaing-interest | status-remaining-interest < 50 | status-remaining-interest >= 50 | status-remaining-interest <= 75 | and Col4 not(Col2) and Col3 and Col4 | Output |

40 | T | F | T | F | 5 |

50 | F | T | T | T | 6 |

60 | F | T | T | T | 6 |

80 | F | T | F | F | 7 |

Since the negation of status-remaining-interest < 50 is status-remaining-interest >= 50 the last column has the same statement written twice. We could then remove this and get the shorter example expression. To try this out, let’s draw the truth table for the shorter example expression and see if the outputs match. For brevity, Col2 references the values in the second column, and Col3 references the values in the third column.

(if (< status-remaining-interest 50) 5 (if (<= status-remaining-interest 75) 6 7))

status-remaing-interest | status-remaining-interest < 50 | status-remaining-interest <= 75 | not(Col2) and Col3 | Output |

40 | T | T | F | 5 |

50 | F | T | T | 6 |

60 | F | T | T | 6 |

80 | F | F | F | 7 |

Since the outputs match, the two statements appear to be equal. Please note that the truth table does not cover all possible values for status-remaining-interest. Generally, you won’t even include that column, but it is included here for clarification. If you do chose to include the column, please include input to match ALL possible outcomes of all of the test cases.