Thursday, October 7, 2010

if...else construct in c sharp.

The if…..else conditional construct is followed by a logical expression where data is compared and a decision is made on the basis of the result of the comparison.

The following is the syntax of the if…else construct.

If (expression)
{
Statements;
}
Else
{
Statements;
}

The following example of if...else construct:

using System;

namespace Branching
{
public class TestIfElse
{
static void Main()
{
int valueOne = 20;
int valueTwo = 30;

Console.WriteLine("Testing valueOne against valueTwo...");
if (valueOne > valueTwo)
{
Console.WriteLine(
"ValueOne: {0} larger than ValueTwo: {1}",
valueOne, valueTwo);
} // end if
else
{
Console.WriteLine(
"Nope, ValueOne: {0} is NOT larger than ValueTwo: {1}",
valueOne, valueTwo);
Console.ReadLine();
} // end else

} // end Main
} // end class
} // end namespace


The following output this program.
Testing valueOne against valueTwo...
Nope , valueOne: 20 is NOT larger than valueTwo: 30


C Sharp Statements Related Contents.

Understanding the while Loop constructs in C sharp.
Understanding the for loop in c sharp.
Understanding the Switch… case construct in C sharp.
How to use do…while loop in C #.

No comments:

Recent Posts