Imagine you are writing a program for the Motor
Vehicle Administration. When processing a driver's license, you want to be
able to ask an applicant if he or she wants to be an organ donor. This
section of the program can appear as follows:

The possible answers to this question are y, yes, Y, Yes, YES, n, N, no, No, NO, I don’t know,
It depends, Why are you asking?, What do you mean?, What kind of organ are
you referring to? What kind of person would possibly want my organ? Are
you planning to sell my body parts?, etc. When you ask this type of
question, make sure you let the applicant know that this is a simple
question expecting a simple answer. For example, the above question can be
formulated as follows:

Locate and select your CSharp Lessons folder. Display it in the Save In
combo box

Click the Create New Folder button

Create a folder named Conditions1 and display it in the Save In
combo box

Save the file as Exercise.cs

Open the Command Prompt and switch to the folder that contains the above
Exercise.cs file

To compile the exercise, type csc Exercise.cs and press Enter

To execute the exercise, type Exercise

Return to Notepad

Boolean Variables

A variable is referred to as Boolean if it is meant to
carry only one of two logical values stated as true or false. For this
reason, such a variable is used only when testing conditions or when dealing
with conditional statements.

To declare a Boolean
variable, you can use the bool keyword. Here is an example of declaring a Boolean variable:

bool TheStudentIsHungry;

After declaring a Boolean variable, you can give it a
value by assigning it a true or false value. Here is an example:

One of the most regularly performed operations on a program
consists of checking that a condition is true or false. When something is true,
you may act one way. If it is false, you act another way. A condition that a
program checks must be clearly formulated in a statement, following specific
rules. The statement can come from you or from the computer itself.
Examples of statements are:

"You are 12 years old"

"It is raining outside"

You live in Sydney"

One of the comparisons the computer performs is to find out if a statement
is true (in reality, programmers (like you) write these statements and the
computer only follows your logic). If a statement is true, the computer
acts on a subsequent instruction.

The comparison using the if statement is used to check whether a condition is true or false. The syntax to use it is:

if(Condition)Statement;

If the Condition is true, then the compiler
would execute the Statement. The compiler ignores anything
else:

If the statement to execute is (very) short, you can write it on the same line with the condition that is being checked.

Consider a program that is asking a user to answer Yes or No to a question such as
"Are you ready to provide your credit card number?". A source
file of such a program could look like this:

You can also write the statement on its own line if the statement is too long 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:

The if condition is used to check one possibility and ignore anything else. Usually, other conditions should be considered. In this case, you can use more than one
if statement.
For example, on a program that asks a user to answer Yes or No, although the positive answer is the most expected, it is important to offer an alternate
statement in case the user provides another answer. Here is an example:

Do you consider yourself a hot-tempered individual(y=Yes/n=No)? y
This job involves a high level of self-control.
We will get back to you.
Press any key to continue

The problem with the above program is that the second if is not an alternative to the first, it is just another condition that the program has to check and execute after executing the first. On that program, if the user provides y as the answer to the question, the compiler would execute the content of its
statement and the compiler would execute the second if condition.

You can also ask the compiler to check a condition; if that condition is true, the compiler will execute the intended statement. Otherwise, the compiler would execute
alternate statement. This is performed using the syntax:

Save, compile, and test the file twice, once with 1 to the question, then
with 0 to the last question

Return to Notepad

The Ternary Operator (?:)

The conditional operator behaves like a simple if…else statement. Its syntax 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:

The compiler will check the first condition. If Condition1 is true, it will execute
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
that you can include as many conditions as you see fit using the else if statement.
After examining all the known possible conditions, if you still think that there might be an unexpected condition, you can use the optional single
else.

A program we previously wrote was considering that any answer other than y was negative. It would be more professional to consider a negative answer because the program anticipated one. Therefore, here is a better version of the program:

When defining an expression whose result would lead to a specific program execution, the
switch statement considers that result and executes a statement based on the possible outcome of that expression, this possible outcome is called a case. The different outcomes are listed in the body of the
switch statement and each case has its own execution, if necessary. The body of a
switch statement is delimited from an opening to a closing curly brackets: “{“ to “}”. The syntax of the switch statement is:

In C++, you can omit the break keyword in a case.
This creates the "fall through" effect as follows: after code
executes in a case, if nothing "stops" it, the execution
continues to the next case. This has caused problems and confusing
execution in the past in some C++ programs. To avoid it, C# requires code
interruption at the end of every case. This interruption is done using the
break keyword.

The expression to examine in a case statement is an integer. Since
a member of an enumerator
(enum) and the character (char) data types are just other forms of integers, they can be used too. Here is an example of using the
switch statement:

When establishing the possible outcomes that the switch statement should consider, at times there will be possibilities other than those listed and you will be likely to consider them. This special case is
handled by the default keyword. The default case would be considered if none of the listed cases matches the supplied answer. The syntax of the
switch statement that considers the default case would be:

An enumerator is a list of natural numeric values
with each value represented by a name. Enumerators are highly useful in
conditional statements because they allow using recognizable names to identify
confusing numbers.

To create an enumerator, use the enum
keyword followed by a name. The values, called members of the enumerator, are included between an
opening curly bracket and the list ends with a closing curly bracket. Here is an example:

enum MemberCategories { Teen, Adult, Senior }

As stated already, each member of the enumerator holds a
value of a natural number, such as 0, 4, 12, 25, etc. In C#, an
enumerator cannot hold character values (of type char). After creating an enumerator, you can declare a variable
from it. For example, you can declare a variable of a MemberCategories type as
follows:

After declaring such a variable, to initialize it, specify
which member of the enumerator would be given to it. You should only assign a
known member of the enumerator. To do this, on the right side of the assignment
operator, type the name of the enumerator, followed by the period operator, and
followed by the member whose value you want to assign. Here is an example:

As mentioned already, an enumerator is in fact a list of
numbers where each member of the list is identified with a name. By default, the first item
of the list has a value of 0, the second has a value of 1, etc. For example, on
the MemberCategories enumerator, Teen has a value of 0 while Senior has a
value of 2. These are the default values. If you don't want these values, you
can explicitly define the value of one or each member of the list. Suppose you
want the Teen member in the above enumerator to have a value of 5. To do this, use
the assignment operator "=" to give the desired value. The enumerator would be:

enum MemberCategories { Teen=5, Adult, Senior };

In this case, Teen now would have a value of 5, Adult
would have a value of 6, and Senior would have a value of 7. You can also
assign a value to more than one member of an enumerator. Here is an example:

enum MemberCategories { Teen=3, Adult=8, Senior };

In this case, Senior would have a value of 9.

An enumerator is very useful in a switch statement where it can be used
case sections. An advantage of using an enumerator is its ability to be more explicit than a regular integer.
To use an enumerator, define it and list each one of its members for the case that applies. Remember that, by default, the members of an enumerator are counted with the first member having a value of 0, the second is 1, etc. Here is an example of a
switch statement that uses an
enumerator.