Table of Contents
- What is a Boolean expression?
- Identify different operators used for creating Boolean expressions in C#
- How to formulate longer Boolean expressions
Boolean expression is an expression that evaluates to either
false. Boolean expressions are used
in control statements to make decisions during the execution of a program.
Here are some examples of Boolean expressions:
age > 21weather == "sunny"temperature <= 32A || Btrue
Recall boolean data type
bool can only take on two values:
false. For this reason
variable can be used as a Boolean expression by itself.
Relational operators evaluate the relationship between its operands.
The operands can be variables or literal values of the same type (or such that they can be implicitly converted to same type)
and the result of the operation is logical value
Below is a table listing relational operators in C#
|Not equal to|
|Greater than or Equal to|
|Less than or Equal to|
In C# you can use
!= operators to compare operands of different types for equality and inequality (including strings).
>= can be used with all integral and floating-point numeric types.
1int a = 4, b = 7;23Console.WriteLine(a > b); // False4Console.WriteLine(a < b); // True5Console.WriteLine(a >= 4); // True6Console.WriteLine(12 <= b); // False
== compares if the two operands are the same. The operation returns
false when the operands are different.
Note that when using it to compare string equality, the letter case of string value must match for the operation to evaluate as
1Console.WriteLine("abc" == "abc"); // True2Console.WriteLine("hello" == "Hello"); // False3Console.WriteLine('C' == 'E'); // False
If you want to create a Boolean expression to test if two operands are different, you can use not equals
1Console.WriteLine("ABC" != "abc"); // True2Console.WriteLine("bye" != "bye "); // True3Console.WriteLine(23 != 23); // False
Logical operators AND
&& and OR
|| enable combining multiple Boolean expressions to create more complex logical expressions.
Logical negation operator
! (NOT) reverses the value of a Boolean expression. The operands must evaluate to Boolean values.
To understand the expected results of AND, OR, and NOT operators review their truth tables:
- AND operations will be true if and only if both operands are true and false otherwise
- OR will be true if at least one of the operands is true and false otherwise
- NOT will negate the truth value
Try changing values of
weekend and see how it changes the output.
AND and OR operators allow chaining multiple expressions together. You can combine an unlimited number of Boolean expressions
using these operators. Refer to the section below for order of precedence or use parentheses to enforce a specific order of evaluation.
|| are short-circuiting operators, meaning once the truth value of the expression is known, the evaluation of the expression will end.
1int a = 4,2 b = 3,3 c = 7;4bool success = true;56// Combining two expression -> False7Console.WriteLine(b > c || a < b);89// Combining 3 expressions -> True10Console.WriteLine(c == b || a == b || a != c);1112// This expression will short circuit -> True13Console.WriteLine(success || (b == c && a >= c));
The operator precedence determines the order in which expressions are evaluated. This becomes important when writing longer boolean expressions with multiple operators. Relational and logical operators are evaluated in this order, from highest to lowest:
Operators of same type are evaluated left to right. Using parentheses allows enforcing alternative order of evaluation. See this document for complete reference.
Recall single equals sign
= is used for assigning a value to a variable. If you are new to programming, it is very easy to make the mistake of
= in place of
== and vice versa. The compilation of the program will fail and you will see an error if you misuse these operators.
When performing relational comparison, the operands must be comparable. For example, this expression does not make sense:
5 > "Hi".
5.6D > 8.9M is not allowed; but you can compare
5.6D > 8.9f because
float can be implicitly converted to
You can avoid this issue by comparing operands of same type.
The following is a very common mistake:
color == "orange" || "red". When combining Boolean expression using
OR or AND operators, each operand must evaluate to
"red" is a string; it is neither
false and therefore cannot be evaluated. The proper way of writing
this expression is:
color == "orange" || color == "red".
Boolean expression is a an expression that evaluates to
- Equality Operators:
- Relational Operators:
- Logical Operators:
Relational expressions can be used to create simple expressions, for example:
age > 21
Logical AND and OR operators enable combining multiple expressions to create more complex logic:
age > 21 && weather =="sunny" && isWeekend