Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
if-else
Collapse the table of content
Expand the table of content

if-else

The if statement selects a statement for execution based on the value of a Boolean expression. It takes the following form:

if (expression)
   statement1
[else
   statement2]

where:

expression
An expression that can be implicitly converted to bool or a type that contains overloading of the true and false operators.
statement1
The embedded statement(s) to be executed if expression is true.
statement2
The embedded statement(s) to be executed if expression is false.

Remarks

If expression is true, statement1 is executed. If the optional else clause exists and expression evaluates to false, statement2 is executed. After executing the if statement, control is transferred to the next statement.

If any of the two results of the if statement (true or false) results in executing more than one statement, multiple statements can be conditionally executed by including them into blocks.

The statement(s) to be executed upon testing the condition can be of any kind, including another if statement nested into the original if statement. In nested if statements, the else clause belongs to the last if that does not have a corresponding else. For example:

if (x > 10)
   if (y > 20)
      Console.Write("Statement_1");
   else
      Console.Write("Statement_2");

In this example, Statement_2 will be displayed if the condition (y > 20) evaluates to false. However, if you want to associate Statement_2 with the condition (x >10), use braces:

if (x > 10) 
{
   if (y > 20) 
      Console.Write("Statement_1");
}
else
   Console.Write("Statement_2");

In this case, Statement_2 will be displayed if the condition (x > 10) evaluates to false

Example

In this example, you enter a character from the keyboard and the program checks if the input character is an alphabetic character. If so, it checks if it is lowercase or uppercase. In each case, the proper message is displayed.

// statements_if_else.cs
// if-else example
using System;
public class IfTest 
{
   public static void Main() 
   {
      Console.Write("Enter a character: ");
      char c = (char) Console.Read();
      if (Char.IsLetter(c))
         if (Char.IsLower(c))
            Console.WriteLine("The character is lowercase.");
         else 
            Console.WriteLine("The character is uppercase.");
      else
         Console.WriteLine("The character is not an alphabetic character.");
   }
}

Input

2

Sample Output

Enter a character: 2
The character is not an alphabetic character.

Additional sample might look as follows:

Run #2:

Enter a character: A
The character is uppercase.

Run #3:

Enter a character: h
The character is lowercase.

It is also possible to extend the if statement to handle multiple conditions using the following else-if arrangement:

if (Condition_1)
   Statement_1;
else if (Condition_2)
   Statement_2;
else if (Condition_3)
   Statement_3;
...
else
   Statement_n;

Example

This example checks if the input character is lowercase, uppercase, or a number. Otherwise, it is not an alphanumeric character. The program makes use of the else-if ladder.

// statements_if_else2.cs
// else-if
using System;
public class IfTest 
{
   public static void Main() 
   {
      Console.Write("Enter a character: ");
      char c = (char) Console.Read();

      if (Char.IsUpper(c)) 
         Console.WriteLine("The character is uppercase.");   
      else if (Char.IsLower(c))
         Console.WriteLine("The character is lowercase.");
      else if (Char.IsDigit(c))
         Console.WriteLine("The character is a number.");
      else
         Console.WriteLine("The character is not alphanumeric.");
   }
}

Input

E

Sample Output

Enter a character: E
The character is uppercase.

Additional sample runs might look as follows:

Run #2

Enter a character: e
The character is lowercase.

Run #3:

Enter a character: 4
The character is a number.

Run #4:

Enter a character: $
The character is not alphanumeric.

See Also

C# Keywords | ?: Operator | Compare to C++ | switch | C. Grammar

Show:
© 2015 Microsoft