A conditional statement is an expression that produces a
true or false result. You can use that result as you see fit. To create the
expression, you use the Boolean operators we studied in the previous lesson.
In the previous lesson, we saw only how to perform the operations and how to
get the results, not how to use them. To use the result of a Boolean
operation, the C# programming language provides some specific conditional
operators.

Application:
Introducing Conditional Expressions

Start Microsoft Visual Studio

To create a new application, on the main menu, click File -> New
Project...

In the middle list, click Empty Project

Change the Name to ElectronicStore2

Press Enter

To create a new class, in the Class View, right-click
ElectronicStore2 -> Add -> Class...

Enter the type of house you want to purchase
1. Single Family
2. TownHouse
3. Condominium
You Choice? 3
Desired House Type: Unknown
Press any key to continue . . .

To check if an expression is true and use its Boolean
result, you can use the if operator. Its formula is:

if(Condition) Statement;

The Condition can be the type of Boolean
operation we studied in the previous lesson. That is, it can have the
following formula:

Operand1 BooleanOperator Operand2

If the Condition produces a true result, then
the compiler executes the Statement. If the statement to execute is
short, you can write it on the same line with the condition that is being
checked. Here is an example:

You can also write the Statement on its own
line even if the statement is short enough to fit on the same line with
the Condition.

Although the (simple) if statement is used to
check one condition, it can lead to executing multiple dependent
statements. If that is the case, enclose the group of statements between
an opening curly bracket "{" and a closing curly bracket "}". Here is an
example:

If you omit the brackets, only the statement that
immediately follows the condition would be executed. As an alternative to
create an if conditional statement, right-click the
section where you want to add the code and click Code Snippet...
Double-click Visual C#. In the list, double-click if:

Just as you can create one if condition, you
can write more than one. Here are examples:

If you use an if condition to perform an
operation and if the result is true, we saw that you could execute the
statement. As we saw in the previous section, any other result would be
ignored. To address an alternative to an if condition, you can use
the else condition. The formula to follow is:

if(Condition)
Statement1;
else
Statement2;

Once again, the Condition can be a Boolean
operation like those we studied in the previous lesson. If the
Condition is true, then the compiler would execute Statement1.
If the Condition is false, then the compiler would execute
Statement2. Here is an example:

If you have a condition that can be checked as an
if situation with one alternate else, you can use the ternary
operator that is a combination of ? and :. Its formula is:

Condition ? Statement1 : Statement2;

The compiler would first test the Condition. If
the Condition is true, then it would execute Statement1,
otherwise it would execute Statement2. When you request two numbers
from the user and would like to compare them, the following program would
do find out which one of both numbers is higher. The comparison is
performed using the conditional operator:

At one time, you may want to assign the value of such
a variable to another variable. If the variable is holding null, it means
it does not have a value, so assigning to another variable would be
meaningless. A solution is to first check whether the variable is
currently holding null or an actual value. That is, you can ask the
compiler to check whether the variable is currently null or it holds a
value:

If the variable is holding an actual value, not null, assign its
value to the new variable

If the variable is null, assign an alternate value (you must
provide that value) to the variable

To support this, the C# language provides the
null-coalescent operator: "??". The formula to use it is:

TargetVariable = OriginalVariable ?? AlternateValue;

You must have declared the OriginalVariable and
it must be able to hold null, which is done by adding ? to it. You can
first declare the TargetVariable or declare it when initializing
it. Of course, both variables must be compatible. Here is an example of
using the ?? operator:

If you use an if...else conditional statement,
you can process only two statements. In some cases, you may deal with more
than two conditions. In this case, you can use an if...else if
condition. Its formula is:

if(Condition1) Statement1;
else if(Condition2) Statement2;

The compiler would first check Condition1. If
Condition1 is true, then Statement1 would be executed. If
Condition1 is false, then the compiler would check Condition2.
If Condition2 is true, then the compiler would execute Statement2. Any
other result would be ignored. Here is an example:

Enter the type of house you want to purchase
1. Single Family
2. Townhouse
3. Condominium
You Choice? 1
Does the house have an indoor garage (1=Yes/0=No)? 1
Desired House Type: SingleFamily
Has indoor garage? Yes
Press any key to continue . . .

Here is another example of running the program:

Enter the type of house you want to purchase
1. Single Family
2. Townhouse
3. Condominium
You Choice? 2
Does the house have an indoor garage (1=Yes/0=No)? 6
Desired House Type: Townhouse
Has indoor garage? No
Press any key to continue . . .

Notice that only two conditions are evaluated. Any
condition other than these two is not considered. Because there can be
other alternatives, the C# language provides an alternate else as the last
resort. Its formula is:

The compiler will check the first condition. If
Condition1 is true, it executes Statement1. If Condition1
is false, then the compiler will check the second condition. If
Condition2 is true, it will execute Statement2. When the
compiler finds a Condition-n to be true, it will execute its
corresponding statement. It that Condition-n is false, the compiler
will check the subsequent condition. This means you can include as many
conditions as you see fit using the else if statement. If after
examining all the known possible conditions you still think that there
might be an unexpected condition, you can use the optional single else.
Here is an example:

Enter the type of house you want to purchase
1. Single Family
2. Townhouse
3. Condominium
You Choice? 3
Does the house have an indoor garage (1=Yes/0=No)? 0
Desired House Type: Condominium
Has indoor garage? No
Press any key to continue . . .