“Decision logic” and “branching logic” are terms programmers use to describe the change in execution path based on the evaluation of some expression. For example, we may write code that evaluates user input. If the value the user inputs is equal to the string value “a”, execute one code block. If the value the user inputs is equal to the string value “b”, execute a different code block. Here, we’re changing the execution path of our application based on user input. This is what is meant by the terms “branching” and “decision”.
Equality operator
Console.WriteLine("a" == "a");
Console.WriteLine("a" == "A");
Console.WriteLine(1 == 2);
string myValue = "a";
Console.WriteLine(myValue == "a");
Output
True
False
False
True
string’s built-in helper methods
Console.WriteLine("a" == "a ");
Here we add a blank space at the end of the string. This, too, outputs false
.
In some cases, this might be perfectly acceptable. However, if you need to accept a match that is inexact, you can “massage” the data first. “Massaging” the data means that you need to perform some cleanup before you perform a comparison for equality.
To massage two strings before checking for equality, you should:
- Make sure both strings are all upper-case or all lower-case using the
ToUpper()
orToLower()
helper method on any string value. - Remove a leading or trailing blank spaces using the
Trim()
helper method on any string value.
string value1 = " a";
string value2 = "A ";
Console.WriteLine(value1.Trim().ToLower() == value2.Trim().ToLower());
When you run the code this time, the output will be true
.
Use the inequality operator
Console.WriteLine("a" != "a");
Console.WriteLine("a" != "A");
Console.WriteLine(1 != 2);
string myValue = "a";
Console.WriteLine(myValue != "a");
False
True
True
False
Comparison operators
Console.WriteLine(1 > 2);
Console.WriteLine(1 < 2);
Console.WriteLine(1 >= 1);
Console.WriteLine(1 <= 1);
False
True
True
True
Method invocation expression
string pangram = "The quick brown fox jumps over the lazy dog.";
Console.WriteLine(pangram.Contains("fox"));
Console.WriteLine(pangram.Contains("cow"));
True
False
Logical negation
The term “Logical Negation” refers to the !
operator. Some people simply call this the “not operator”. Adding the !
operator before a conditional expression like a method call checks to ensure the expression is false.
// These two lines of code do the same thing:
Console.WriteLine(pangram.Contains("fox") == false);
Console.WriteLine(!pangram.Contains("fox"));
Logical Negation operator
string pangram = "The quick brown fox jumps over the lazy dog.";
Console.WriteLine(!pangram.Contains("fox"));
Console.WriteLine(!pangram.Contains("cow"));
False
True
Recap
- There are many different kinds of expressions that evaluate to either
true
orfalse
. - Evaluate equality using the
==
operator. - Evaluating equality of strings requires you consider the possibility that the strings have different case and leading or trailing spaces. Depending on your situation, use the
ToLower()
orToUpper()
helper methods, and theTrim()
helper method to improve the likelihood that two strings are equal. - Evaluate inequality using the
!=
operator. - Evaluate greater than, less than and similar operations using comparison operators like
>
,<
,>=
, and<=
. - If a method returns a bool, it can be used as a Boolean expression.
- Use the logical negation operator
!
to evaluate the opposite of a given expression.