C# Decision Constructs                                       
                      
Like most languages, C# defines
if/else syntax for executing certain code based on a condition being true or false.


if(boolean expression)
{ // Executes if expression is true.
}
else // else clause is optional.
{ // Executes if expression is false.
}

if(boolean expression)
{ // Executes if expression is true.
}
else if(boolean expression)
{ // Executes if else-if expression is true. You can have any
 // number of else-if clauses.
}
else // else clause is optional.
{ // Executes if all of the above are false.
}



Unlike C/C++, the if expression must return a Boolean value. In other words, zero is not automatically
converted to false.


static void Main(string[] args)
{    

  // Compile error! Cannot implicitly convert int to bool.

  if(SomeMethod())
  {
  }

  // OK! Must check for zero or use Convert.ToBoolean() utility.

  if(SomeMethod() != 0)
  {
  }
}
static int SomeMethod()
{
  return 0;
}



In most cases, an if expression is composed of the following relational operators:





















You can combine expressions using the following conditional operators:


















Unlike C/C++, the conditional operators only work with Boolean types.


static void Main(string[] args)
{    

  // Compile error! SomeMethod returns an int instead of a bool.

  if( (5 > 2) && SomeMethod())
  {
  }
}

static int SomeMethod()
{
  return 0;
}



C# supports switch syntax to simplify testing a single variable against many possible constant values.        
Without switch, you would have to write multiple if/else-if statements, which can get tedious.  Unlike C/C++,
fall through is not allowed unless you explicitly allow it via the goto case syntax.  A ‘default’ case is optional,
and not required.

static string GetDayOfWeek(int day)
{
  string dayOfWeek;
  switch(day)
  {
     case 1:
        dayOfWeek = "Sunday";
        break;
// <-- Required to prevent compile error!
     case 2:
        dayOfWeek = "Monday";
        break;
     
     // etc ...
     default:
        goto case 1; // if number unknown, do case 1.
  }
  return dayOfWeek;
}



The C# switch expression can accept either an integer or a string value.

static int GetDayNumberOfWeek(string day)
{
  int dayNumber;
  switch(day.ToLower())
//<-- Converts the string to all lowercase.
  {
     case "sunday":
        dayNumber = 1;
        break;
     case "monday":
        dayNumber = 2;
        break;

     // etc ...

     default:
        dayNumber = 0;
        break;
  }
  return dayNumber;
}

C# Decision Constructs
Table of Contents
C# Tutorial | C#.NET Tutorial | Decision Constructs Tutorial

Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used
exclusively as an online learning aid.  Any attempts to copy, reproduce, or use for training is
strictly prohibited.
Courseware
Training Resources
Tutorials

Relational
Operator

Example

Meaning in Life

==
(two equal signs)

if(age == 30)

Returns true only if each
expression is the same.

!=

if(myStr != "Foo")

Returns true only if each
expression is different

<, <=, >=, >

if(bonus < 2000)

Returns true if expression A is less
than, less than or equal to, greater
than or equal to, or greater than
expression B.

Conditional Operator

Example

Meaning in Life

&&

if((age == 30) && (name == "Mel"))

True if both expressions are
true.

||

if((age == 30) || (name == "Mel"))

True if at least one expression
is true.

!

if(!myBool)

True if expression is false.
Services