IF Statement And Nested IF Statements with code examples

C# IF-Statement

The "if-statement" is a selection statement or condition statement available in C# language. This statement is available in other different programming/scripting languages like (PHP, Javascript, VB.NET, Java) for same usage style. 

Syntax of C# IF-Statement

 
if(boolean expression or condition){
//block of code
}

Syntax of Simple C-sharp IF-ELSE Statement

 
if(boolean expression or condition expression){
//block of code
}else{
//block of code
}

If-statement selects a statement for execution, based on the value of a Boolean expression.

If the condition or Boolean expression is true then the control goes to the body of if block, that is code inside If-block will execute.

If the condition or Boolean expression is false then the control goes to the next level i.e. body of else block, that is code inside else- block will execute.

C# IF-ELSE IF Statement

If you want to execute multiple condition expression then you can use IF-ELSE IF Statement which helps to execute multiple condition expression.

If "if-Statement" boolean expression returns false then control goes to next zone of "Else-if" statement to execute. If the boolean expression is true then code executes else of the expression returns false then control goes to "Else" statement. When all expressions returns false then by default "Else" statement body executes.

Syntax of C-sharp Else-If Statement

 
if(boolean expression or condition expression){
//block of code
}else if(){
//Block of code
}else{
//block of code
}

In c# we can have any number of Else-If statements associated with if-statement

Example: IF-Else-IF statement

class Program {
        static void Main(string[] args){
            Console.WriteLine("Enter a any number between 1 to 10");
            int num1 = Convert.ToInt16(Console.ReadLine());
            if (num1 > 10 && num1 < 99 || num1 < 1){
                Console.WriteLine("Wrong number, {0}", num1);
            }else if (num1 == 9){
                Console.WriteLine("Jackpot number, {0}", num1);
            }else if (num1 > 100){
                Console.WriteLine("Out of Range : {0}", num1);
            }else{
                Console.WriteLine("You entered , {0}", num1);
            }
        }
}

C# NESTED IF-Statement

C-Sharp Nested if-statement is another way to execute the conditional statement by nesting the if-else condition inside the parent body of If-statement. In simple words inside "If-statement" body or "if-else-if" statement body we can have nested "if-statements", "if-else-if statements".

if(boolean expression or condition expression){
      
      if(boolean expression or condition expression){
      //block of code
      }else{
      //block of code
      }  

}else{
//block of code
}

Example: Nested if statements

class Program{
        static void Main(string[] args){
            Console.WriteLine("Enter a any number between 1 to 10");
            int num1 = Convert.ToInt16(Console.ReadLine());
            if (num1 > 100){
                Console.WriteLine("Wrong number, {0}", num1);
            }else{
                if (num1 == 9){
                    Console.WriteLine("Jackpot number, {0}", num1);
                }else{
                    Console.WriteLine("Your number, {0}", num1);
                }
            }   
        }
   }

Hey friends, this is all about conditional statements (if-statements) in c-sharp language but this same conditional statement is available in other languages too with almost same syntax and usage. If you have any query regarding conditional statements please let me know through your comments which you can find it below this article. Kindly share this article with your friends on Facebook, google+ and twitter. Thanks.

Author: Gurunatha Dogi

Gurunatha Dogi

Gurunatha Dogi is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more..... read more

Add a Comment