- Back to Home »
- C#.NET »
- C#.NET Else if
Posted by :
Sudhir Chekuri
Thursday, 31 December 2015
This is one type of conditional branching.In this type the compiler 1st goes to if condition,if it is false it goes to else if condition,it also false finally goes to else statement and print output.
Syntax for Else if statement
if(condition)
{
statements
}
else if(condition)
{
statements
}
else
{
statements
}
Example program using Else if statement
Program
Output
Enter number
223
Positive
Output
Enter number
-741
Negative
Output
Enter number
0
zero
Explanation
In the above example we give input is 223,it is greater than zero,then the output will be printed as positive and we give input is -741 it is less than zero,then the compiler goes to else if part of the program and print output as negative and finally gives 0 it is equal to zero,then the compiler goes to else pert of the program and print output as zero.
Syntax for Else if statement
if(condition)
{
statements
}
else if(condition)
{
statements
}
else
{
statements
}
Example program using Else if statement
Program
using System;
namespace elseif
{
class Program
{
static void Main(string[] args)
{
int i;
Console.WriteLine("Enter number");
i = Convert.ToInt32(Console.ReadLine());
if (i > 0)
{
Console.WriteLine("Positive");
}
else if (i < 0)
{
Console.WriteLine("Negative");
}
else
{
Console.WriteLine("zero");
}
Console.ReadKey();
}
}
}
Output
Enter number
223
Positive
Output
Enter number
-741
Negative
Output
Enter number
0
zero
Explanation
In the above example we give input is 223,it is greater than zero,then the output will be printed as positive and we give input is -741 it is less than zero,then the compiler goes to else if part of the program and print output as negative and finally gives 0 it is equal to zero,then the compiler goes to else pert of the program and print output as zero.