Making Decisions in C#

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.

 

Comparison Operators

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. In Table 2-5, note particularly that “is equal to” requires two equal signs and that “not equal” is different than in FORTRAN or VB.

 

> 

greater than

< 

less than

==

is equal to

!=

is not equal to

>=

greater than or equal to

<=

less than or equal to

Table 1: Comparison Operators in C#

 

Combining Conditions

When you need to combine two or more conditions in a single if or other logical statement, you use the symbols for the logical and, or, and not operators. These are totally different than any other languages except C/C++ and are confusingly like the bitwise operators shown in Table 2.

 

&&

logical And

||

logical Or

~

logical Not

Table 2 Boolean operators in C#

 

So, while in VB.Net we would write:

If ( 0 < x) And (x <= 24) Then

Console.writeLine (“Time is up”)

in C# we would write:

if ( (0 < x) && ( x <= 24) )

Console.writeLine(“Time is up”);

The Most Common Mistake

Since the is equal to operator is == and the assignment operator is = they can easily be misused. If you write

if (x = 0)

Console.writeLine(“x is zero”);

instead of:

if (x == 0)

Console.writeLine(“x is zero”);

you will get the confusing compilation error, “Cannot implcitly convert double to bool,” because the result of the fragment:

(x = 0)

is the double precision number 0, rather than a Boolean true or false. Of course, the result of the fragment:

(x == 0)

is indeed a Boolean quantity and the compiler does not print any error message.

 

Shashi Ray