If Else

C# Conditional statements

C# supports usual logical statemnts like any other langugage supports. Those are mathematic expressions.

  • equal to like x == y
  • not equal to like x != y
  • less than like x < y
  • less than or equal to like x <= y
  • greater than like x > y
  • greater than or equal to like x >= y

we can perform various action using above conditions to take different actions as required.

C# supports multiple contional statements as below

  • if condition, can be used to executes the code block if condition evaluates true
  • else, can be used to executes the code block if same condition evaluates false
  • else if, can be used to executes the code block if previous condition evaluates false
  • switch, can be used to if we have more choice to executes, we will learn more about switch in another lession.

Now we will see one by one conditional statements with an example

The if Statement

You can use the if statement to execute the block of C# code if given condition evaluates true. For eample

Try it
int firstNumber = 12;
int secondNumber = 15;
if (firstNumber < secondNumber)
{
    Console.WriteLine("firstNumber is less than secondNumber");
}
//Output
firstNumber is less than secondNumber
		

In above example output is printed on the console that means condition evaluates true.

C# is a case sensitive langugage so all the keywords we need to use in lowercase. Use if as lowercase only else it will throw compile time error.

The else Statement

You can use the else statement to executes the block of C# code if given condition evaluates false. For eample

Try it
int firstNumber = 17;
int secondNumber = 15;
if (firstNumber < secondNumber)
{
    Console.WriteLine("firstNumber is less than secondNumber");
}
else
{
    Console.WriteLine("firstNumber is greater than secondNumber");
}
//Output
firstNumber is greater than secondNumber
		

The else if Statement

You can use the else if statement if first/previous condition evaluates the false. If first if statement evaluates false then control will transer to next else if statement, if that is also evaluates false then control move to next else if block and so on. Once if there is no more else if statement then it will check for the else part.

Try it
int age = 28;
if (age >= 1 && age <= 17)
{
    Console.WriteLine("You are child");
}
else if (age >= 18 && age <= 35)
{
    Console.WriteLine("You are young adults");
}
else if(age >= 36 && age <= 55)
{
    Console.WriteLine("You are middle-aged adults");
}
else
{
    Console.WriteLine("You are old people");
}
		

If...Else shorten

If we don't want to write this long if else condition then we can shorten this condition as well. It's ternary operator as it's made with 3 operands. By using ternary operator we can reduce the multiple lines of code in single line. If it is simple if else statement then it's good to convert into short hand statement using ternary operator. For example

Instead of writing above code

Try it
string day = "sunday";
if (day == "saturday" || day == "sunday")
{
    Console.WriteLine("Happy weekend.");
}
else
{
    Console.WriteLine("Happy weekdays.");
}
			

You can write like this

Try it
string day = "sunday";
string greeting = (day == "saturday" || day == "sunday") ? "Happy weekend." : "Happy weekdays.";
Console.WriteLine(greeting);
		

 

← Previous

Next →