Training
Module
Evaluate Boolean expressions to make decisions in C# - Training
Learn the operators and techniques required to evaluate and compare values in your decision statements.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Complex expressions can contain many different operators. The following example illustrates this.
x = (45 * (y + z)) ^ (2 / 85) * 5 + z
Creating complex expressions such as the one in the preceding example requires a thorough understanding of the rules of operator precedence. For more information, see Operator Precedence in Visual Basic.
Often you want operations to proceed in a different order from that determined by operator precedence. Consider the following example.
x = z * y + 4
The preceding example multiplies z
by y
, then adds the result to 4
. But if you want to add y
and 4
before multiplying the result by z
, you can override normal operator precedence by using parentheses. By enclosing an expression in parentheses, you force that expression to be evaluated first, regardless of operator precedence. To force the preceding example to do the addition first, you could rewrite it as in the following example.
x = z * (y + 4)
The preceding example adds y
and 4
, then multiplies that sum by z
.
You can nest expressions in multiple levels of parentheses to override precedence even further. The expressions most deeply nested in parentheses are evaluated first, followed by the next most deeply nested, and so on to the least deeply nested, and finally the expressions outside parentheses. The following example illustrates this.
x = (z * 4) ^ (y * (z + 2))
In the preceding example, z + 2
is evaluated first, then the other parenthetical expressions. Exponentiation, which normally has higher precedence than addition or multiplication, is evaluated last in this example because the other expressions are enclosed in parentheses.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Module
Evaluate Boolean expressions to make decisions in C# - Training
Learn the operators and techniques required to evaluate and compare values in your decision statements.