An If statement consists of a Boolean expression followed by one or more statements.
If the Boolean expression is true, the code block within the IF statement is executed. If the Boolean expression is false, the first set of code after the end of the IF statement (after closing the parentheses) is executed.
int a = ten; /* Check the Boolean condition using the IF statement * /if (a <) {/ * If the condition is true, output the following statement * /Console.WriteLine ("A is less than"); } Console.WriteLine (The value of "a" is {0} ", a); Console.ReadLine ();
If...else if...else Statements
An if statement can be followed by an optional else If...else statement, which can be used to perform a variety of conditions.
When using the If...else if...else statement, the following points need to be noted:
- An if can be followed by 0 or an else, it must be after any other else if.
- An if can be followed by 0 or more else if, they must precede else.
- Once an else if match succeeds, the other else if or else will not be executed
if (Boolean_expression 1) {/ * When Boolean expression 1 is true, execute */}else if (boolean_expression 2) {/ * When Boolean expression 2 is True */}else if (Boole An_expression 3) {/ * When Boolean expression 3 is True */}else {/ * is executed when none of the above conditions are true */}
Here's an example:
/* local variable definition */ int a = +; /* Check the Boolean condition * /if (a = =) {/ * If the IF condition is true, then output the following statement * /Console.WriteLine ("A's value is ten"); } else if (a = =) {/ * If the else if condition is true, output the following statement * /Console.WriteLine ("A's value is"); } else if (a = =) {/ * If the else if condition is true, output the following statement * /Console.WriteLine ("A has a value of"); } else { /* If none of the above conditions are true, output the following statement * /Console.WriteLine ("No matching value"); } Console.WriteLine (The exact value of "a" is {0} ", a); Console.ReadLine ();
Basic application of If statement in C #