---
title: "What is if-else statement in C#?"  
description: "What is if-else statement in C#?"  
author: "Anonymous User"  
published: 2020-01-21  
updated: 2020-01-21  
canonical: https://www.mindstick.com/forum/155711/what-is-if-else-statement-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# What is if-else statement in C#?

Please [briefly describe](https://www.mindstick.com/interview/22943/briefly-describe-android-application-architecture) me about if-else statement with [flow](https://answers.mindstick.com/qa/105072/how-to-trade-with-the-money-flow-index) [chart](https://www.mindstick.com/articles/1509/show-google-analytics-data-in-chart-in-asp-dot-net-mvc) and also with example.

## Replies

### Reply by Nishi Tiwari

In C# if-else is a common selection statement. An if-else statement checks the Boolean expression and executes the code based upon if the expression is true or false. An if part of the code will executes when the value of the expression is true. The else part of the code will executed when the value of the expression is false. The else portion of the if-else statement is optional.

**Syntax of the if-else statement.**

```
1.	if (Boolean condition)
2.	   {
3.	      //code if condition is true
4.	   }
5.	   else
6.	   {
7.	      Statement
8.	}
```

## If statement

An if part of the statement or statement block is **[executed when the condition is true;](https://www.mindstick.com/forum/145552/c-sharp-introduction)** if it is false, then the control executes the code in the else statement or else statement block.

The following example uses the if statement to check if the value of variable a is less than 0, then display a message, ‘a is negative’.

```
1.	int a = -1;
2.	if (a < 0)
3.	{
4.	   Console.WriteLine("a is negative.");
5.	}
```

## If-else statement

As mentioned above, an if statement executes with one or more else statements. If the “if” condition will not true, then the program control goes to the “else” condition. In C# if-else statement also tests the condition. It will executes the if block if condition is true otherwise else block will be executed.

## C# If-else Example

```
1.	using System;
2.	public class IfExample
3.	    {
4.	        public static void Main(string[] args)
5.	        {
6.	            int num = 11;
7.	            if (num % 2 == 0)
8.	            {
9.	                Console.WriteLine("It is even number");
10.	            }
11.	            else
12.	            {
13.	                Console.WriteLine("It is odd number");
14.	            }
15.
16.	        }
17.	    }
```

## Output:

```
It is odd number
```

![What is if-else statement in C#?](https://www.mindstick.com/mindstickforums/9188b1cd-adb9-4651-bc23-dcb857720a94/images/6703eee9-6b19-449d-b66f-6945e27c796c.jpeg)\


---

Original Source: https://www.mindstick.com/forum/155711/what-is-if-else-statement-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
