The if statement is a control statement that executes a block of code if an expression evaluates to true. It takes the form:
if (expression) statement1 [else statement2]
If expression is true, statement1 is executed. If the optional else clause exists and expression evaluates to false, statement2 is executed. After executing the if statement, control is transferred to the next statement.
If any of the two results of the if statement (true or false) results in executing more than one statement, multiple statements can be conditionally executed by including them into blocks.
The statement(s) to be executed upon testing the condition can be of any kind, including another if statement nested into the original if statement. In nested if statements, the else clause belongs to the last if that does not have a corresponding else. For example:
if (x > 10) if (y > 20) Console.Write("Statement_1"); else Console.Write("Statement_2");
In this example, Statement_2
will be displayed if the condition (y > 20)
evaluates to false. However, if you want to associate Statement_2
with the condition (x >10)
, use braces:
if (x > 10) { if (y > 20) Console.Write("Statement_1"); } else Console.Write("Statement_2");
In this case, Statement_2
will be displayed if the condition (x > 10)
evaluates to false
In this example, you enter a character from the keyboard and the program checks if the input character is an alphabetic character. If so, it checks if it is lowercase or uppercase. In each case, the proper message is displayed.
// if-else example using System; public class IfTest { public static void Main() { Console.Write("Enter a character: "); char c = (char) Console.Read(); if (Char.IsLetter(c)) if (Char.IsLower(c)) Console.WriteLine("The character is lowercase."); else Console.WriteLine("The character is uppercase."); else Console.WriteLine("The character is not an alphabetic character."); } }
Run #1:
Enter a character: 2 The character is not an alphabetic character.
Run #2:
Enter a character: A The character is uppercase.
Run #3:
Enter a character: h The character is lowercase.
It is also possible to extend the if statement to handle multiple conditions using the following else-if arrangement:
if (Condition_1) Statement_1; else if (Condition_2) Statement_2; else if (Condition_3) Statement_3; ... else Statement_n;
This example checks if the input character is lowercase, uppercase, or a number. Otherwise, it is not an alphanumeric character. The program makes use of the else-if ladder.
// else-if using System; public class IfTest { public static void Main() { Console.Write("Enter a character: "); char c = (char) Console.Read(); if (Char.IsUpper(c)) Console.WriteLine("The character is uppercase."); else if (Char.IsLower(c)) Console.WriteLine("The character is lowercase."); else if (Char.IsDigit(c)) Console.WriteLine("The character is a number."); else Console.WriteLine("The character is not alphanumeric."); } }
Run #1:
Enter a character: E The character is uppercase.
Run #2:
Enter a character: e The character is lowercase.
Run #3:
Enter a character: 4 The character is a number.
Run #4:
Enter a character: $ The character is not alphanumeric.