C# If Statement

If Statements are used for making decisions;they are very useful and are likely to be used frequently. The condition must be Boolean, so it is either true or false.

Syntax

if (condition) 
{
...Code to be executed   
}

Tip: In the VS studio IDE, if you type if until it's highlighted then hit tab twice, the syntax will automatically be inserted.

Make a new C# project and name it If Statements. Then copy the following code::

string username = null;

Console.WriteLine("Enter username: ");
username = Console.ReadLine();

if (username == "Asim")
            {
                Console.WriteLine("Welcome " + username);
            }

Console.ReadLine();

Here, first we have made a variable called username which is null, and then we request the user's input. If the user enters "Asim" then "Welcome Asim" will display; otherwise nothing will happen as the condition is false.

If Else Statement

Syntax

if (condition)
{
code to execute
} else {
if false execute other code..
}

The If Else Statement is used when something is not true. In the above example if the user did equal Asim, the condition would be true and the message would appear; however if the condition was false nothing would happen because we have not told it do anything.

Example - If Else

  string username = null;

  Console.WriteLine("Enter username: ");
  username = Console.ReadLine();

  if (username == "Asim")
  {
  Console.WriteLine("Welcome " + username);
  }
  else
  {
  Console.WriteLine("Unkown user");
  }
  Console.ReadLine();

This is similar to the previous If Statement, however this time we have the 'else'. If the condition is false, then the message "Unknown User" will appear.

Nested If Else

We can use Nested If Else Statements as well. Comment out the other If and Else Statements put this code below:

 string username = null;
 string password = null;

 string message = "Welcome " + username;
 if (username == "Asim" && password == "12345")
 {
 Console.WriteLine(message);
 }
 else if (username == "Dave" && password == "54321")
 {
 Console.WriteLine(message);
 }
 else if (username == "admin" && password == "superadmin")
 {
 Console.WriteLine(message);
 }
 else
 {
 Console.WriteLine("Sorry Password and username incorrect!");
 }
 Console.ReadLine();

Now we have more decisions. First we made a new variable called 'message'; this avoids the repetition of writing Console.WriteLine("Welcome " + username). Next we had several If Else statements, and we added more conditions. Now if you put 'Dave' and password '54321', you should see:

Welcome Dave

The above process can be quite tedious, and changing the variable names would be an issue. As a result, we have a switch statement which is useful instead of a Nested If Else Statement.

Using Operators

If Statements can use several operators, such as not equal to, and, is equal to and so on. Commonly used tasks are to compare strings or see if a number is greater or less than another number. Below is a list of the common logical operators used in a If Statements:

Operator Description
&& And
== Equal to (don't confuse this with the assignment operator =)
!= Not equal to
< Less than
> More than
<= Less than or equal to
>= More than or equal to

For a full list of operators see: C# Operators Page.

Example - Not Equal To (!=) Operator

int a = 10;
int b = 1;
            
if (a != b)
{
Console.WriteLine("A and B are not the same: A = " + a + " and B = " + b);
}
else
{
Console.WriteLine("A and B are the same!: A = " + a + " and B = " + b);
}
Console.ReadLine();

This will compare the two integer variables A and B. If they are not the same, then the first message will appear; otherwise the second message will show. The != operator is useful when comparing passwords and emails.

Summary

This tutorial covered If Statements, If Else Statements and Nested If Else Statements. We also outlined the different operators available in an If Statement. Remember that:

  • The condition must be Boolean
  • If you are evaluating only one variable and wish to use a Nested If Else Statement, then use a Switch Statement instead