Conditional Statements |
|
if a Condition is True |
Introduction |
A conditional statement is an expression that produces a true or false result. You can use that result as you see fit. To create the expression, you use the Boolean operators we studied in the previous lesson. In the previous lesson, we saw only how to perform the operations and how to get the results, not how to use them. To use the result of a Boolean operation, the C# programming language provides some specific conditional operators. |
public class Exercise { static int Main() { var type = HouseType.Unknown; var choice = 0; Console.WriteLine("Enter the type of house you want to purchase"); Console.WriteLine("1. Single Family"); Console.WriteLine("2. Townhouse"); Console.WriteLine("3. Condominium"); Console.Write("You Choice? "); choice = int.Parse(Console.ReadLine()); Console.WriteLine("\nDesired House Type: {0}", type); return 0; } } Here is an example of running the program: Enter the type of house you want to purchase 1. Single Family 2. TownHouse 3. Condominium You Choice? 3 Desired House Type: Unknown Press any key to continue . . . To check if an expression is true and use its Boolean result, you can use the if operator. Its formula is: if(Condition) Statement; The Condition can be the type of Boolean operation we studied in the previous lesson. That is, it can have the following formula: Operand1 BooleanOperator Operand2 If the Condition produces a true result, then the compiler executes the Statement. If the statement to execute is short, you can write it on the same line with the condition that is being checked. Here is an example: using System; public enum HouseType { Unknown, SingleFamily, TownHouse, Condominium } public class Exercise { static int Main() { var type = HouseType.Unknown; var choice = 0; Console.WriteLine("Enter the type of house you want to purchase"); Console.WriteLine("1. Single Family"); Console.WriteLine("2. Townhouse"); Console.WriteLine("3. Condominium"); Console.Write("You Choice? "); choice = int.Parse(Console.ReadLine()); if (choice == 1) type = HouseType.SingleFamily; Console.WriteLine("\nDesired House Type: {0}", type); return 0; } } Here is an example of running the program: Enter the type of house you want to purchase 1. Single Family 2. Townhouse 3. Condominium You Choice? 1 Desired House Type: SingleFamily Press any key to continue . . . If the Statement is too long, you can write it on a different line than the if condition. Here is an example: using System; public enum HouseType { Unknown, SingleFamily, TownHouse, Condominium } public class Exercise { static int Main() { var type = HouseType.Unknown; var choice = 0; Console.WriteLine("Enter the type of house you want to purchase"); Console.WriteLine("1. Single Family"); Console.WriteLine("2. Townhouse"); Console.WriteLine("3. Condominium"); Console.Write("You Choice? "); choice = int.Parse(Console.ReadLine()); if (choice == 1) type = HouseType.SingleFamily; Console.WriteLine("\nDesired House Type: {0}", type); return 0; } } You can also write the Statement on its own line even if the statement is short enough to fit on the same line with the Condition. Although the (simple) if statement is used to check one condition, it can lead to executing multiple dependent statements. If that is the case, enclose the group of statements between an opening curly bracket "{" and a closing curly bracket "}". Here is an example: using System; public enum HouseType { Unknown, SingleFamily, TownHouse, Condominium } public class Exercise { static int Main() { var type = HouseType.Unknown; var choice = 0; Console.WriteLine("Enter the type of house you want to purchase"); Console.WriteLine("1. Single Family"); Console.WriteLine("2. Townhouse"); Console.WriteLine("3. Condominium"); Console.Write("You Choice? "); choice = int.Parse(Console.ReadLine()); if (choice == 1) { type = HouseType.SingleFamily; Console.WriteLine("\nDesired House Type: {0}", type); } return 0; } } If you omit the brackets, only the statement that immediately follows the condition would be executed. As an alternative to create an if conditional statement, right-click the section where you want to add the code and click Code Snippet... Double-click Visual C#. In the list, double-click if:
Just as you can create one if condition, you can write more than one. Here are examples: using System; public enum HouseType { Unknown, SingleFamily, Townhouse, Condominium } public class Exercise { static int Main() { var type = HouseType.Unknown; var choice = 0; Console.WriteLine("Enter the type of house you want to purchase"); Console.WriteLine("1. Single Family"); Console.WriteLine("2. Townhouse"); Console.WriteLine("3. Condominium"); Console.Write("You Choice? "); choice = int.Parse(Console.ReadLine()); if (choice == 1) type = HouseType.SingleFamily; if (choice == 2) type = HouseType.Townhouse; if (choice == 3) type = HouseType.Condominium; Console.WriteLine("\nDesired House Type: {0}", type); return 0; } } Here is an example of running the program: Enter the type of house you want to purchase 1. Single Family 2. Townhouse 3. Condominium You Choice? 3 Desired House Type: Condominium Press any key to continue . . .
public class Exercise { static int Main() { var type = HouseType.Unknown; var choice = 0; Console.WriteLine("Enter the type of house you want to purchase"); Console.WriteLine("1. Single Family"); Console.WriteLine("2. Townhouse"); Console.WriteLine("3. Condominium"); Console.Write("You Choice? "); choice = int.Parse(Console.ReadLine()); if (choice == 1) type = HouseType.SingleFamily; if (choice == 2) type = HouseType.Townhouse; if (choice == 3) type = HouseType.Condominium; Console.WriteLine("\nDesired House Type: {0}", type); if (type == HouseType.SingleFamily) Console.WriteLine("\nDesired House Matched"); return 0; } } If you use an if condition to perform an operation and if the result is true, we saw that you could execute the statement. As we saw in the previous section, any other result would be ignored. To address an alternative to an if condition, you can use the else condition. The formula to follow is: if(Condition) Statement1; else Statement2; Once again, the Condition can be a Boolean operation like those we studied in the previous lesson. If the Condition is true, then the compiler would execute Statement1. If the Condition is false, then the compiler would execute Statement2. Here is an example: using System; public enum HouseType { Unknown, SingleFamily, Townhouse, Condominium } public class Program { static int Main() { var type = HouseType.Unknown; var choice = 0; Console.WriteLine("Enter the type of house you want to purchase"); Console.WriteLine("1. Single Family"); Console.WriteLine("2. Townhouse"); Console.WriteLine("3. Condominium"); Console.Write("You Choice? "); choice = int.Parse(Console.ReadLine()); if (choice == 1) type = HouseType.SingleFamily; if (choice == 2) type = HouseType.Townhouse; if (choice == 3) type = HouseType.Condominium; Console.WriteLine("\nDesired House Type: {0}", type); if (type == HouseType.SingleFamily) Console.WriteLine("Desired House Matched"); else Console.WriteLine("No House Desired"); return 0; } } Here is an example of running the program: Enter the type of house you want to purchase 1. Single Family 2. Townhouse 3. Condominium You Choice? 1 Desired House Type: SingleFamily Desired House Matched Press any key to continue . . . Here is another example of running the program: Enter the type of house you want to purchase 1. Single Family 2. Townhouse 3. Condominium You Choice? 2 Desired House Type: Townhouse No House Desired Press any key to continue . . .
If you have a condition that can be checked as an if situation with one alternate else, you can use the ternary operator that is a combination of ? and :. Its formula is: Condition ? Statement1 : Statement2; The compiler would first test the Condition. If the Condition is true, then it would execute Statement1, otherwise it would execute Statement2. When you request two numbers from the user and would like to compare them, the following program would do find out which one of both numbers is higher. The comparison is performed using the conditional operator: using System; public class Exercise { static int Main() { var Number1 = 0; var Number2 = 0; var Maximum = 0; var Num1 = ""; var Num2 = ""; Console.Write("Enter first numbers: "); Num1 = Console.ReadLine(); Console.Write("Enter second numbers: "); Num2 = Console.ReadLine(); Number1 = int.Parse(Num1); Number2 = int.Parse(Num2); Maximum = (Number1 < Number2) ? Number2 : Number1; Console.Write("\nThe maximum of "); Console.Write(Number1); Console.Write(" and "); Console.Write(Number2); Console.Write(" is "); Console.WriteLine(Maximum); Console.WriteLine(); return 0; } } Here is an example of running the program; Enter first numbers: 244 Enter second numbers: 68 The maximum of 244 and 68 is 244
Remember that, when declaring a variable of a primitive type, you can add a question mark to the data type to specify that the variable can have a nullable value. Here is an example: using System;
public class Exercise
{
public static int Main()
{
double? distance = null;
Console.WriteLine();
return 0;
}
}
At one time, you may want to assign the value of such a variable to another variable. If the variable is holding null, it means it does not have a value, so assigning to another variable would be meaningless. A solution is to first check whether the variable is currently holding null or an actual value. That is, you can ask the compiler to check whether the variable is currently null or it holds a value:
To support this, the C# language provides the null-coalescent operator: "??". The formula to use it is: TargetVariable = OriginalVariable ?? AlternateValue; You must have declared the OriginalVariable and it must be able to hold null, which is done by adding ? to it. You can first declare the TargetVariable or declare it when initializing it. Of course, both variables must be compatible. Here is an example of using the ?? operator: using System;
public class Exercise
{
public static int Main()
{
double? distance = null;
double? fromTo = null;
Console.WriteLine("Distance 1: {0}", distance);
Console.WriteLine("Distance 2: {0}", fromTo);
fromTo = distance ?? 135.85;
Console.WriteLine("Distance 1: {0}", distance);
Console.WriteLine("Distance 2: {0}", fromTo);
Console.WriteLine();
return 0;
}
}
The fromTo = distance ?? 135.85; means that:
The above code would produce: Distance 1: Distance 2: Distance 1: Distance 2: 135.85 Press any key to continue . . . Here is another version of the program: using System; public class Exercise { public static int Main() { double? distance = null; double? fromTo = null; Console.WriteLine("Distance 1: {0}", distance); Console.WriteLine("Distance 2: {0}", fromTo); Console.WriteLine("---------------------"); fromTo = distance ?? 135.85; Console.WriteLine("Distance 1: {0}", distance); Console.WriteLine("Distance 2: {0}", fromTo); Console.WriteLine("---------------------"); distance = 8284.26; fromTo = distance ?? 135.85; Console.WriteLine("Distance 1: {0}", distance); Console.WriteLine("Distance 2: {0}", fromTo); Console.WriteLine(); return 0; } } This time, the program would produce: Distance 1: Distance 2: --------------------- Distance 1: Distance 2: 135.85 --------------------- Distance 1: 8284.26 Distance 2: 8284.26 Press any key to continue . . . If you use an if...else conditional statement, you can process only two statements. In some cases, you may deal with more than two conditions. In this case, you can use an if...else if condition. Its formula is: if(Condition1) Statement1; else if(Condition2) Statement2; The compiler would first check Condition1. If Condition1 is true, then Statement1 would be executed. If Condition1 is false, then the compiler would check Condition2. If Condition2 is true, then the compiler would execute Statement2. Any other result would be ignored. Here is an example: using System; public enum HouseType { Unknown, SingleFamily, Townhouse, Condominium } public class Exercise { static int Main() { var type = HouseType.Unknown; var choice = 0; var garage = ""; Console.WriteLine("Enter the type of house you want to purchase"); Console.WriteLine("1. Single Family"); Console.WriteLine("2. Townhouse"); Console.WriteLine("3. Condominium"); Console.Write("You Choice? "); choice = int.Parse(Console.ReadLine()); if (choice == 1) type = HouseType.SingleFamily; else if (choice == 2) type = HouseType.Townhouse; Console.Write("Does the house have an indoor garage (1=Yes/0=No)? "); var answer = int.Parse(Console.ReadLine()); if (answer == 1) garage = "Yes"; else garage = "No"; Console.WriteLine("\nDesired House Type: {0}", type); Console.WriteLine("Has indoor garage? {0}", garage); return 0; } } Here is an example of running the program: Enter the type of house you want to purchase 1. Single Family 2. Townhouse 3. Condominium You Choice? 1 Does the house have an indoor garage (1=Yes/0=No)? 1 Desired House Type: SingleFamily Has indoor garage? Yes Press any key to continue . . . Here is another example of running the program: Enter the type of house you want to purchase 1. Single Family 2. Townhouse 3. Condominium You Choice? 2 Does the house have an indoor garage (1=Yes/0=No)? 6 Desired House Type: Townhouse Has indoor garage? No Press any key to continue . . . Notice that only two conditions are evaluated. Any condition other than these two is not considered. Because there can be other alternatives, the C# language provides an alternate else as the last resort. Its formula is:
The compiler will check the first condition. If Condition1 is true, it executes Statement1. If Condition1 is false, then the compiler will check the second condition. If Condition2 is true, it will execute Statement2. When the compiler finds a Condition-n to be true, it will execute its corresponding statement. It that Condition-n is false, the compiler will check the subsequent condition. This means you can include as many conditions as you see fit using the else if statement. If after examining all the known possible conditions you still think that there might be an unexpected condition, you can use the optional single else. Here is an example: using System; public enum HouseType { Unknown, SingleFamily, Townhouse, Condominium } public class Exercise { public static int Main() { var type = HouseType.Unknown; var choice = 0; var garage = ""; Console.WriteLine("Enter the type of house you want to purchase"); Console.WriteLine("1. Single Family"); Console.WriteLine("2. Townhouse"); Console.WriteLine("3. Condominium"); Console.Write("You Choice? "); choice = int.Parse(Console.ReadLine()); if (choice == 1) type = HouseType.SingleFamily; else if (choice == 2) type = HouseType.Townhouse; else if (choice == 3) type = HouseType.Condominium; else type = HouseType.Unknown; Console.Write("Does the house have an indoor garage (1=Yes/0=No)? "); var answer = int.Parse(Console.ReadLine()); if (answer == 1) garage = "Yes"; else garage = "No"; Console.WriteLine("\nDesired House Type: {0}", type); Console.WriteLine("Has indoor garage? {0}", garage); return 0; } } Here is an example of running the program: Enter the type of house you want to purchase 1. Single Family 2. Townhouse 3. Condominium You Choice? 3 Does the house have an indoor garage (1=Yes/0=No)? 0 Desired House Type: Condominium Has indoor garage? No Press any key to continue . . .
|
|||||||||||||||||||||||||||||||||
|
||
Previous | CCopyright © 2010-2016, FunctionX | Next |
|