Making Decisions in C#
The familiar if-then-else of Visual Basic, Pascal and Fortran has its analog in C#. Note that in C#, however, we do not use the then keyword:
if ( y > 0 )
z = x / y;
Parentheses around the condition are required in C#. This format can be somewhat deceptive; as written, only the single statement following the if is operated on by the if statement. If you want to have several statements as part of the condition, you must enclose them in braces:
if ( y > 0 )
{
z = x / y;
Console.writeLine(“z = “ + z);
}
By contrast, if you write:
if ( y > 0 )
z = x / y;
Console.writeLine(“z = “ + z);
the C# program will always print out z= and some number, because the if clause only operates on the single statement that follows. As you can see, indenting does not affect the program; it does what you say, not what you mean.
If you want to carry out either one set of statements or another depending on a single condition, you should use the else clause along with the if statement:
if ( y > 0 )
z = x / y;
else
z = 0;
and if the else clause contains multiple statements, they must be enclosed in braces, as in the code above.
There are two or more accepted indentation styles for braces in C# programs:
if (y >0 )
{
z = x / y;
}
The other style, popular among C programmers, places the brace at the end of the if statement and the ending brace directly under the if:
if ( y > 0 ) {
z = x / y;
Console.writeLine(“z=” + z);
}
You will see both styles widely used, and of course, they compile to produce the same result.
Above, we used the > operator to mean “greater than.” Most of these operators are the same in C# as they are in C and other languages.
Note particularly that “is equal to” requires two equal signs and that “not equal” is different than in FORTRAN or VB.

sonia nawazPosted Feb 26, 2016, 9:47 AM
See my explanation <a href="http://www.codingcabinet.com/if-else-if-statement-in-c-sharp/">If-Else Statement </a>