The if else statement in C# is used to compare the two or more values and execute the true condition. The if else statement is commonly used along with the logical and comparison operators. Let's consider we have X and Y variables having certain values, then we can write condition for as.
- If x is less than y : x > y
- If x is greater than y : x > y
- If x is greater than or equals to y : x >= y
- If x is no equals to y : x != y
- If x is equals to y : x = = y
- If x is less than or equals to 10 and y is equals to 20 : x <= 10 && y = = 20
- If x is less than or equals to 20 or y is equals to 10 : x <= 20 || y = = 10
The preceding scenarios are some of the scenarios where if the condition is applicable, we can compare any values which are supported by C# datatypes.
The C# if Statement
The if statement can also be used without the else condition. In this scenario only the true code block is evaluated.
Example with integer value
The else Statement
The else statement is used to execute the False block if the given condition is not True. The if else statement gives more flexibility to perform the action in both the scenarios, whether either condition is true or false.
The Multiple If-else If Statement
Let's write a simple C# console program by creating the console project using Visual Studio to demonstrate the above examples.
After running the above program, then the following output will be shown.