The if else statement in C# used to compare the two or more values and execute the true condition. The if else statement commonly used along with the logical and comparison operators. Lets consider we have x and y variable 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's are some of scenarios where if condition is applicable, we can compare any values which is supported by C# datatypes.
The C# if Statement
The if statement can also be used without the else condition, In this scenario only true code block is evaluated.
Example with integer value
The else Statement
The else statement is used to execute the False block if given condition is not True, The if else statement gives more flexibility to perform the action in both the scenarios either condition is true or false.
The Multiple If-else If Statement
Let's write 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