if, else if Statements

If Statement

An if statement is one of the most commonly used statements when programming, not just in C#, but in any high level programming language. The only difference is that the syntax may vary from one to another but the principle is all the same.

An if statement tests a condition, and the result can only be a boolean (True/False). The result will determine whether to execute (run) the next piece of code or to skip it. See below for basic syntax structure.

if(condition equals true)
{
  // code block
  // executes code here 
}
// if condition is false the code block is skipped

Now we can extend the if statement, by add else. What this essentially means that if the condition is false, then execute (run) code withing the else section. See below for basic syntax with else.

if(condition equals true)
{
  // if block 
  // executes code here if condition is true
} else
{
  // else block
  // other wise code here will be run
}
// please note that only the if block or else block will run at any one time 

As you can see from the syntax, the if and else uses brackets. These brackets are recommended but not essential for simple if statements that are only going to run one line only. Visual Studio will automatically enter these brackets (braces) for you. If you wanted to add more lines of code to your condition then you would need to add brackets.  Now to see a working example simple example:

using System;
 
namespace GetCoding
{
  class Program
  {
    static void Main()
    {
      byte age = 17;
 
      if (age > 17)
      {
        // this line doesn't get run at all unless age is 18 or more
        Console.WriteLine("Person age is 18+");
      }
      else
      {
        // This line get run.
        Console.WriteLine("Person age younger than 18");
      }
 
      Console.Read(); // stops console from closing so can see result
    }
  }
}

As the if statement only runs one line of code, the brackets can be removed. But to add extra lines of code then brackets would need to be added.

byte age = 18;
 
if (age > 17)
  Console.WriteLine("Person age is 18+"); // gets run as age is 18
else
  Console.WriteLine("Person age younger than 18");
 
Console.Read(); // stops console from closing so can see result

However the if statements may be simple enough to write them as ternary operator, which would reduce to one line of code. 

Else If

Esle if extends the if statement by allowing you to test for more than one condition, each may require to do different things. the standard syntax is a follows:

if(condition)
{
  // if block - executes code here if condition is true
} 
else if(condition) { // else if block - executes code if this if statement is true }
else { // else block - will run if all if conditions fail. }

The else if statement is just like the basic if statement above. also you can add as many else if statements as you want, but remember the else statement must be last. Like the the if statement else block may not be required, but that is dependent on what you are doing. Some if - else if statements can be converted into a Switch Statement.