One of the core features of any programming language is the ability to conditionally execute a segment of code. This means that a program will run a segment of code only if a given condition is met.
Consider a banking application that can remind you when a bill is due. The application will notify you that a bill is due soon, but only if the bill has not already been paid.
The condition for the above example is: Send a notification of an upcoming bill only if the statement “the bill is unpaid” is true. In order to state something like this in C#, we need to understand how programming languages represent true and false.
5.1.1. Boolean Values¶
The C# data type for storing true and false values is
named after the British mathematician George Boole.
George Boole created Boolean Algebra, which is the basis of all modern computer arithmetic.
There are only two boolean values—
false. C# is
False are not valid boolean values.
1 2 3
Console.WriteLine(true); Console.WriteLine(true.GetType()); Console.WriteLine(false.GetType());
True System.Boolean System.Boolean
false are not strings. If you use quotes to
surround booleans (
"false"), those values become strings.
5.1.2. Boolean Conversion¶
As with the numeric and string data types, the boolean type also has a
Convert.ToBoolean(). It works similarly to the
Double.Parse() methods, attempting to convert a non-boolean value to a boolean. This is very limited as is,
but this could be useful if working with user input, specifically the words “true” and “false”.
Convert.ToBoolean() converts various non-boolean values. Explore differnt data types.
1 2 3 4 5 6 7
Console.WriteLine(Convert.ToBoolean("true")); Console.WriteLine(Convert.ToBoolean("TRUE")); Console.WriteLine(Convert.ToBoolean(0)); Console.WriteLine(Convert.ToBoolean(1)); Console.WriteLine(Convert.ToBoolean(-1)); Console.WriteLine(Convert.ToBoolean("")); Console.WriteLine(Convert.ToBoolean("LaunchCode"));
5.1.3. Boolean Expressions¶
A boolean expression is an expression that evaluates to either
False. The equality operator,
==, compares two values and returns true
or false depending on whether the values are equal.
Console.WriteLine(5 == 5); Console.WriteLine(5 == 6);
In the first statement, the two operands are equal, so the expression evaluates
True. In the second statement, 5 is not equal to 6, so we get
126.96.36.199. Comparison Operators¶
== operator is one of six common comparison operators.
|Greater than (
|Less than (
|Greater than or equal (
|Less than or equal (
Although these operations are probably familiar, the C# symbols are
different from the mathematical symbols. A common error is to use a single
equal sign (
=) instead of a double equal sign (
==). Remember that
is an assignment operator and
== is a comparison operator. Also note
=> are not recognized operators.
An equality test is symmetric, meaning that we can swap the places of the
operands and the result is the same. For a variable
a == 7 is
7 == a is also
True. However, an assignment statement is
a = 7 is legal while
7 = a is not.
5.1.4. Check Your Understanding¶
Under which conditions does
Convert.ToBoolean() convert a string to
- When the string is
- Whenever the string contains any non-whitespace character.
- Whenever the string is non-empty.
- Never. It converts all strings to
Which of the following is a Boolean expression? Select all that apply.
3 == 4
3 + 4
3 + 4 == 7