Assignment and Logical Operators
Learn about the assignment operator, logical operators, and conditional logical operators.
Operators like =
, +
, -
, *
, and /
can be combined with an assignment to shorthand expressions. Logical operators like &
, |
, and ^
act on Boolean values to perform logic operations. Understanding these operators can help us write more efficient C# code.
Assignment operators
We have been using the most common assignment operator, =
. To make our code more concise, we can combine the assignment operator with other operators, like arithmetic operators, as shown in the following code:
int p = 6;p += 3; // equivalent to p = p + 3;p -= 3; // equivalent to p = p - 3;p *= 3; // equivalent to p = p * 3;p /= 3; // equivalent to p = p / 3;
Exploring logical operators
Logical operators operate on Boolean values, returning either true
or false
. Let’s explore binary logical operators that operate on two Boolean values:
Step 1: Use your preferred coding tool to add a new ...