An if statement consists of a Boolean expression followed by a block of codes. If the Boolean expression is true, the block of code inside the if statement will be executed else if statement will be skipped. This is the simplest one of all the decision-making statements.
The syntax of the if
statement in C#:
1 2 3 4 | if (condition) { //block of statement } |
Note: If the curly brackets { } are not used with the if statement then the statement just next to it will only be executed and others will not be considered as a part of the statement.
if statement Flowchart:
Example of C# if statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using System; namespace DecisionStatement { class IfStatement { static void Main(string[] args) { int x = 50; // check condition if (x < 100) { Console.WriteLine("True, x is less than 100"); } } } } |
Output:
1 | True, x is less than 100 |