From the author of

From the author of

This article provides the information needed to make logical decisions, iteratively
execute a sequence of instructions, and modify the normal flow of control in
programs. Although there is much more to C#, this article provides ample tools
necessary to create useful, sophisticated programs.

For C++ and Java Programmers

Many of the statements in this article contain a Boolean expression for
decision-making capability. A C++ program can interpret positive integers
as true values; it does not work that way in C#. In C#, the Boolean
expression must return a true or false Boolean value.
It does interpret an integral value as being true or false.

if Statements

if statements allow evaluation of an expression and, depending on
the truth of the evaluation, the capability to branch to a specified sequence of
logic. C# provides three forms of if statements: simple if,
if-then-else, and if-else
if-else.

Simple if

A simple if statement takes the following form:

if (Boolean expression)
[{]
true condition statement(s)
[}]

As expected, the Boolean expression must evaluate to either true or
false. When the Boolean expression is true, the program
performs the following true condition statements:

The curly braces are optional if there's only one action. It's
usually a good practice to add them anyway. Their omission has been known to
cause unexpected bugs.

if-then-else

The simple if statement guarantees only that you can perform certain
actions on a true condition. It's either done or it's not. To
handle both the true and the false conditions, use the
if-then-else statement. It has the following
form:

In a sequential order, each statement, beginning with if and
continuing through each else if, is evaluated until one of its Boolean
expressions evaluates to true. The dots indicate possible multiple
else if blocks. There can be any number of else if blocks
required.

Once one of the Boolean expressions evaluates to true, the
true condition statements for that if or else if are
executed, and then flow of control transfers to the first statement following
the entire if-else if-else structure.

When none of the Boolean expressions evaluates to true, the
false condition statement(s) of the else section is executed.
Here's an example: