C# If Else Statement

Introduction

C# if else statement is a common selection statement. The if else in the C# statement checks a Boolean expression and executes the code based on if the expression is true or false. The if part of the code executes when the value of the expression is true. The else part of the code is executed when the value of the expression is false. The else part of the if..else is optional.

Here is the syntax of the if else statement.

if (Boolean condition)  
{  
    Statement  
}  
else  
{  
    Statement  
}

If statement

The if section of the statement or statement block is executed when the condition is true; if it's false, control executes the code in the else statement or statement block. The ‘else’ portion of the statement is optional.

The following code example uses an if statement to check if the value of variable a is less than 0, then displays a message, ‘a is negative’.

int a = -1;  
if (a < 0)  
{  
   Console.WriteLine("a is negative.");  
}

If else statement

As mentioned previously, the if statement goes with one or more else statements. If the “if” condition is not true, the program control goes to the “else” condition. The following code snippet uses a if..else statement to check if the value of a is less than 0. If not, then it displays a message, ‘a is 0 or positive’.

int a = -1;  
if (a < 0)  
{  
   Console.WriteLine("a is negative.");  
}  
else  
{  
   Console.WriteLine("a is 0 or positive.");  
} 

The {} brackets are optional for a single-line statement. We can replace the above code with the following code.

if (a < 0)  
   Console.WriteLine("a is negative.");  
else  
   Console.WriteLine("a is 0 or positive.");  

If else if statement

An if..else statement can have more if and else. The following code snippet uses an if.. and else if statement to check another condition. In this case, if the first if condition is not true, the program control goes to the next else..if condition and if this condition is not true, then the control statement goes the last else part of the code.

// Read a  
int a = 8;  
if (a < 10)  
{  
   Console.WriteLine("a is less than 10.");  
}  
else if (a > 10 && a < 100)  
{  
   Console.WriteLine("a is between 10 and 100.");  
}  
else  
{  
   Console.WriteLine("a is greather than 100.");  
}

Nested if else

You can have nested if-else statements with one or more else blocks. Nested if can be used in both the if and else parts of the statement. The following code sample uses a nested if inside the else block. You can use multiple nested if statements.

int a = -1;  
if (a < 0)  
{  
   Console.WriteLine("a is negative.");  
}  
else  
{  
   if (a == 0)  
      Console.WriteLine("a is 0.");  
   else  
      Console.WriteLine("a us positive.");  
}

Conditional operators in if

You can also apply conditional or ( || ) and conditional and (&&) operators to combine more then one condition. Listing 56 shows you how to use the if. . .else statement.

int a = -1; int b = 10; int c;  
// Check if both numbers are negative  
if (a < 0 && b < 0)  
{  
   Console.WriteLine("Both a and b are negative.");  
}  
else if (a < 0 || b < 0)  
{  
   Console.WriteLine("One number is negative.");  
}  
else  
{  
   Console.WriteLine("Both numbers are positive.");  
}

The if-else statement can have other nested statements and also can have more than one if..else statement. Listing 1 is a complete example of various use cases of the if else statement.

using System;  
class Program  
   {  
   static void Main(string[] args)  
   {  
      Console.WriteLine("Statements Sample!");  
      int a = -1; int b = 10; int c;  
      // Check if both numbers are negative  
      if (a < 0 && b < 0)  
      {  
         Console.WriteLine("Both a and b are negative.");  
      }  
      else if (a < 0 || b < 0)  
      {  
         if (b > 0 && b <= 10)  
         {  
            c = a + b;  
            Console.WriteLine("Total: {0}", c);  
         }  
      Console.WriteLine("One number is negative.");  
      }  
   else  
   {  
      Console.WriteLine("Both numbers are positive.");  
      }  
   Console.ReadKey();  
  }  
}

Listing 1. The output of Listing 1 looks like the following.

C# If Else

Figure.1

Summary

In this article and code example, we learned how the if..else statement works in C#. We also saw the use of conditional if..else statements.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.