This tutorial is about conditional statements in C#. It focuses on the if---else construct to control the flow of the program

Don't forget to check out our site http://howtech.tv/ for more free how-to videos!
http://youtube.com/ithowtovids - our feed
http://www.facebook.com/howtechtv - join us on facebook
https://plus.google.com/103440382717658277879 - our group in Google+

Introduction

You can control a program by using conditional statements. Conditional statements in C# allow you to execute selective statements based on a condition that is evaluated. The construct used is if---- else construct
A conditional statement is an expression that produces a true or false result.

Alt Text "conditional statement"
Consider an example to check if the person is above 18 yrs of age or not. If the age is above 18, the output should read "you are above the age limit".

Step 1 -- User Input
To start with, first of all we will get the input from the user for age.

Step 2 -- Implementing If construct
The following code snippet shows the using of the if else statement in C:
if (age more then= 18)
{
Console.WriteLine("You are above the age limit");
}


The condition in the if statement checks the age and if it is true the statements in the if block gets executed.

Step 3 -- Implementing Else construct
If the condition is false, you can have an else construct to write the output and the statement is as follows:
else
{
Console.WriteLine("You are below the age limit");
}

The statement in else construct gets executed.

Here, the else block is optional. Thus the conditional statement in C# provides flexibility in programming.

Step 4 -- Implementing nested if---else construct
The if---else construct can also be nested inside each other. Consider an example where you want to categorize people based on their age, children from 1 to 12, teens from 13 to 19 and adults after 20 years of age. Therefore, here you have to make two decisions one after the other. You can take successive decisions in your program by using the nested if---else construct. The following code snippet shows the nested if---else construct:
if((agemore then0)&&(ageless then=12))
Console.WriteLine("You are a child");
else if((agemore then12)&&(ageless then=19))
Console.WriteLine("You are a Teen");
else
Console.WriteLine("You are an adult");

The nested if else statement in C has been represented by using logical operators.
When the program is executed, the output is as follow: