How do I use if-else statement?
Category: Introduction, viewed: 2K time(s).
The example below shows you how to use the if-else statement for checking a condition. The if block will be executed when the boolean expression passed resulted in a true value otherwise the else block will be executed.
using System;
namespace Kodecsharp.Example.Intro
{
class IfElseDemo
{
public static void Main(string[] args)
{
int a = 10;
int b = 25;
//
// A simple if-else block
//
if (a <= b)
{
Console.WriteLine("a is less than or equals to b");
}
else
{
Console.WriteLine("b is greater than a");
}
//
// An if-else if-else block
//
if (a * 4 < b)
{
Console.WriteLine("a * 4 is less than b");
}
else if (a * 5 - 8 < b)
{
Console.WriteLine("a * 5 - 8 is less that b");
}
else
{
Console.WriteLine("a = " + a + "; b = " + b);
}
Console.ReadLine();
}
}
}
Our program result:
a is less than or equals to b
a = 10; b = 25