- Back to Home »
- C#.NET »
- C#.NET If else
Posted by :
Sudhir Chekuri
Thursday, 31 December 2015
It is one of the statements in conditional branching. It is used to check whether the condition is true or false. if it is true, it goes to if condition and print output. if it is false, it goes to else part of the program and print the output.
Syntax for if else statement
if(condition)
{
statement
}
else
{
statement
}
Example program using if else statement
Program
using System;
Output
Enter number
12
Positive
Output
Enter number
-6
Negative
Explanation
In the above example we can give input is 12 then the output will be printed as positive because 12 is greater than zero. We can give input is -6 then the output will be printed as negative because -6 is less than zero.
Syntax for if else statement
if(condition)
{
statement
}
else
{
statement
}
Example program using if else statement
Program
using System;
namespace ifelse
{
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
{
Console.WriteLine("Negative");
}
Console.ReadKey();
}
}
}
Output
Enter number
12
Positive
Output
Enter number
-6
Negative
Explanation
In the above example we can give input is 12 then the output will be printed as positive because 12 is greater than zero. We can give input is -6 then the output will be printed as negative because -6 is less than zero.