Controlling Program Flow – Conditional Instructions

A quick look to some common and hidden conditional operators...

In order to control the flow of your program, C# offers a set of several operators in order to test conditions. In following articles we will see how to act based on this conditions to execute a set of code against other, or, loop for a certain number of times through the a block of code.

Before you start: I will be providing just an sample code snippet code of each of the following operators. Here you can download the source code for the whole article (and more samples). I believe the best way to learn is to try it yourself. Go download the code and debug it TO STEP INTO EVERY LINE and see what is happening.

Without further introductions, enter C# operators to evaluate conditions:

Relational Operators

< Less Than Operator

bool sample1 = (3 &lt; 5);
Console.WriteLine("3 &lt; 5 = " + sample1);
// Output: false

> Greater Than Operator

bool sample2 = (5 &gt; 3);
Console.WriteLine("5 &gt; 3 = " + sample2);
// Output: true

<= Less Than or Equal Operator

bool sample3 = (5 &lt;= 5);
Console.WriteLine("5 &lt;= 5 = " + sample3);
// Output: true

>= Greater Than or Equal Operator

bool sample4 = (5 &gt;= 3);
Console.WriteLine("5 &gt;= 3 = " + sample4);
// Output: true

== Equality Operator

bool sample5 = (6 == 6);
Console.WriteLine("6 == 6 = " + sample5);
Output: true

!= Not Equal Operator

bool sample6 = (6 != 7);
Console.WriteLine("6 != 7 = " + sample6);
// Output: true

Boolean Operators

To explain these operators I will use the following static methods:

bool ReturnTrue()
{
    Console.WriteLine(".. inside ReturnTrue ..");
    return true;
}

bool ReturnFalse()
{
    Console.WriteLine(".. inside ReturnFalse ..");
    return false;
}

& Logical And Operator

bool sample1 = ReturnFalse() &amp; ReturnTrue();
Console.WriteLine("false &amp; true = " + sample1);
// Output: true

| Logical Or Operator

bool sample2 = ReturnTrue() | ReturnFalse();
Console.WriteLine("true | false = " + sample2);
// Output: true

^ Logical XOr Operator

bool sample3 = ReturnTrue() ^ ReturnTrue();
Console.WriteLine("true ^ true = " + sample3);
// Output: false

! Not Operator

bool sample4 = !ReturnTrue();
Console.WriteLine("!true = " + sample4);
// Output: false

&& And Operator (Lazy And)

bool sample5 = ReturnFalse() &amp;&amp; ReturnTrue();
Console.WriteLine("false &amp;&amp; true = " + sample5);
// Output: false

|| Or Operator (Lazy Or)

bool sample6 = ReturnTrue() || ReturnFalse();
Console.WriteLine("true &amp;&amp; false = " + sample6);
// Output: true

Conditional Operators

To explain these operators I will use the following functions:

object ReturnNull()
{
    Console.WriteLine(".. inside ReturnNull ..");
    return null;
}

object ReturnNotNull()
{
    Console.WriteLine(".. inside ReturnNotNull ..");
    return "Hello World!";
}

? Operator

string result = (5 &gt; 3) ? "greater" : "shorter";
Console.WriteLine("(5&gt;3) ? \"greater\" : \"shorter\" = " + result);
// Output: "greater"

?? Null-coalescing Operator

Sample 1

object result2 = ReturnNull() ?? "object was null";
Console.WriteLine("ReturnNull() == \"object was null\" = " + result2.ToString());
// Output: "object was null"

Sample 2

object result3 = ReturnNotNull() ?? "object was null";
Console.WriteLine("ReturnNotNull() == \"object was null\" = " + result3.ToString());
// Output: "Hello World!"