give the value corresponding to the first testi which yields True
The test gives False, so the "else" expression y is returned.
Only the "else" expression is evaluated in this case.
When you write programs in the Wolfram Language, you will often have a choice between making a single definition whose right‐hand side involves several branches controlled by If functions, or making several definitions, each controlled by an appropriate /; condition. By using several definitions, you can often produce programs that are both clearer, and easier to modify.
This defines a step function, with value 1 for x>0, and -1 otherwise.
This defines the positive part of the step function using a /; condition.
Here is the negative part of the step function.
This shows the complete definition using /; conditions.
The function If provides a way to choose between two alternatives. Often, however, there will be more than two alternatives. One way to handle this is to use a nested set of If functions. Usually, however, it is instead better to use functions like Which and Switch.
This defines a function with three regions. Using True as the third test makes this the default case.
This defines a function that depends on the values of its argument modulo 3.
Mod[7,3] is 1, so this uses the second case in the Switch.
17 matches neither 0 nor 1, but does match _.
An important point about symbolic systems such as the Wolfram Language is that the conditions you give may yield neither True nor False. Thus, for example, the condition x==y does not yield True or False unless x and y have specific values, such as numerical ones.
In this case, the test gives neither True nor False, so both branches in the If remain unevaluated.
You can add a special fourth argument to If, which is used if the test does not yield True or False.
give True if the pattern form matches expr, and give False otherwise
Functions for dealing with symbolic conditions.
The Wolfram Language leaves this as a symbolic equation.
Unless expr is manifestly True, TrueQ[expr] effectively assumes that expr is False.
Unlike ==, === tests whether two expressions are manifestly identical. In this case, they are not.
The main difference between lhs===rhs and lhs==rhs is that === always returns True or False, whereas == can leave its input in symbolic form, representing a symbolic equation, as discussed in "Equations". You should typically use === when you want to test the structure of an expression, and == if you want to test mathematical equality. The Wolfram Language pattern matcher effectively uses === to determine when one literal expression matches another.
You can use === to test the structure of expressions.
The == operator gives a less useful result.
In setting up conditionals, you will often need to use combinations of tests, such as test1&&test2&&…. An important point is that the result from this combination of tests will be False if any of the testi yield False. The Wolfram Language always evaluates the testi in turn, stopping if any of the testi yield False.
evaluate until one of the expri is found to be False
evaluate until one of the expri is found to be True
Evaluation of logical expressions.
This function involves a combination of two tests.
Here both tests are evaluated.
Here the first test yields False, so the second test is not tried. The second test would involve 1/0, and would generate an error.
The way that the Wolfram Language evaluates logical expressions allows you to combine sequences of tests where later tests may make sense only if the earlier ones are satisfied. The behavior, which is analogous to that found in languages such as C, is convenient in constructing many kinds of Wolfram Language programs.