Decision logic and branching logicDecision logic and branching logic
0 0
Read Time:2 Minute, 53 Second

“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() or ToLower() 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 or false.
  • 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() or ToUpper() helper methods, and the Trim() 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.

https://docs.microsoft.com/en-us/learn/modules/csharp-evaluate-boolean-expressions/2-exercise-boolean-expressions

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

About Author

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *