# 28 Tests and Conditionals

 28 Tests and Conditionals
Is 2+2 equal to 4? Let’s ask the Wolfram Language.
Test whether 2+2 is equal to 4:
2 + 2 == 4

We can also test whether 2×2 is greater than 5. We do that using >.
Test whether 2×2 is greater than 5:
2*2 > 5

The function If lets you choose to give one result if a test is True, and another if it’s False.
Since the test gives True, the result of the If is x:
If[2 + 2 == 4, x, y]

By using a pure function with /@, we can apply an If to every element of a list.
If an element is less than 4, make it x, otherwise make it y:
If[# < 4, x, y] & /@ {1, 2, 3, 4, 5, 6, 7}

You can also test for less than or equal using , which is typed as <=.
If an element is less than or equal to 4, make it x; otherwise, make it y:
If[# <= 4, x, y] & /@ {1, 2, 3, 4, 5, 6, 7}

This makes an element x only if it is equal to 4:
If[# == 4, x, y] & /@ {1, 2, 3, 4, 5, 6, 7}

You can test whether two things are not equal using , which is typed as !=.
If an element is not equal to 4, make it x; otherwise, make it y:
If[# != 4, x, y] & /@ {1, 2, 3, 4, 5, 6, 7}

Select elements in the list that are greater than 3:
Select[{1, 2, 3, 4, 5, 6, 7}, # > 3 &]

Select elements that are between 2 and 5:
Select[{1, 2, 3, 4, 5, 6, 7}, 2 <= # <= 5 &]

Beyond size comparisons like <, > and ==, the Wolfram Language includes many other kinds of tests. Examples are EvenQ and OddQ, which test whether numbers are even or odd. (The “Q” indicates that the functions are asking a question.)
4 is an even number:
EvenQ[4]

Select even numbers from the list:
Select[{1, 2, 3, 4, 5, 6, 7, 8, 9}, EvenQ[#] &]

In this case, we don’t need the explicit pure function:
Select[{1, 2, 3, 4, 5, 6, 7, 8, 9}, EvenQ]

IntegerQ tests whether something is an integer; PrimeQ tests whether a number is prime.
Select prime numbers:
Select[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, PrimeQ]

Sometimes we need to combine tests. && represents “and”, || represents “or” and ! represents “not”.
Select elements of the list that are both even and greater than 2:
Select[{1, 2, 3, 4, 5, 6, 7}, EvenQ[#] && # > 2 &]

Select[{1, 2, 3, 4, 5, 6, 7}, EvenQ[#] || # > 4 &]

Select elements that are not either even or greater than 4:
Select[{1, 2, 3, 4, 5, 6, 7}, ! (EvenQ[#] || # > 4) &]

The space between letters isn’t a letter; nor is “!”:
{LetterQ["a"], LetterQ["bc"], LetterQ["a b"], LetterQ["!"]}

Turn a string into a list of characters, then test which are letters:
LetterQ /@ Characters["30 is the best!"]

Select the characters that are letters:
Select[Characters["30 is the best!"], LetterQ]

Select letters that appear after position 10 in the alphabet:
Select[Characters["30 is the best!"], LetterQ[#] && LetterNumber[#] > 10 &]

You can use Select to find words in English that are palindromes, meaning that they are the same if you reverse them.
Select[WordList[], StringReverse[#] == # &]

MemberQ tests whether something appears as an element, or member, of a list.
5 appears in the list {1, 3, 5, 7}:
MemberQ[{1, 3, 5, 7}, 5]

Select[Range[100], MemberQ[IntegerDigits[#], 2] &]

ImageInstanceQ is a machine-learning-based function that tests whether an image is an instance of a particular kind of thing, like a cat.
Test if an image is of a cat: