1. Login to Give a bone

## If and functions...

Looking at this code I'm having some trouble understanding the if and function and how it works. This might be a simple but I'm just starting out...

(if (and (> b a) (< b (* a b)))
b
a)

I know that in the if statement, the predicate is evaluated and if true, the consequent is evaluated and returns it's value. If it's false, the alternative is evaluated and returns it's value.

In the and statement, the expressions are evaluated left to right, one at a time. If any of the expressions are false, then that and expresson is false and the rest of the expressions are not evaluated. If all of the expressions are true, the value of the and expression is the value of the last one.

I understand these individually, but what happens when both of these are in the same coding. Does the and expression cross out the if since it is written latter?

Any help would be appreciated.

Thanks

2. Login to Give a bone

## Re: If and functions...

From the HELP file...

AND returns... Nil, if any of the expressions evaluate to nil; otherwise T. If and is issued without arguments, it returns T.

Everything is evaluated from the inside out.
So the (>) and (<) functions are evaluated first, then the (and) function, then the (if) function.

Assume that b is greater than a, so this returns TRUE.
Assume that a times b is greater than b, this returns TRUE.
At this point the (and) statement is going to return TRUE, and the (if) statement will eval the first expression, or "b"

3. Login to Give a bone

## Re: If and functions...

With if, if the expression is non nil, then it will execute what is the first expression of the if statement, and return the last item evaluated. With and, it only returns T or nil, not the last item evaluated. So if all the items in the and statement are true,then the first expression in the if statement will be evaluated and returned.

Hope that helps.

4. Login to Give a bone

## Re: If and functions...

That clears it up. I see what it's saying now. Thanks.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•