This statement contains two-part and depends on the boolean value evaluated by the condition checked. If the condition is true then the code inside the if
statement is executed or if it is false then the code inside else
statement will be executed.
The syntax of the if..else
statement in C#:
1 2 3 4 5 6 7 8 | if (condition) { //code executed if condition true } else { //code executed if condition false } |
if..else statement Flowchart:
Example of if…else statement in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using System; namespace DecisionStatement { class IfElseStatement { static void Main(string[] args) { int x = 50; // check condition if (x < 30) { Console.WriteLine("True, x is less than 30"); } else { Console.WriteLine("False, x is greater than 30"); } } } } |
Output:
1 | False, x is greater than 30 |