![]() |
Conditional Statements: if |
|
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. |
class Program
{
static void Main()
{
HouseType type = HouseType.Unknown;
int choice;
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);
}
}
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
}
class Program
{
static void Main()
{
HouseType type = HouseType.Unknown;
int choice;
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);
}
}
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
}
class Program
{
static void Main()
{
HouseType type = HouseType.Unknown;
int choice;
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);
}
}
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
}
class Program
{
static void Main()
{
HouseType type = HouseType.Unknown;
int choice;
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);
}
}
}
If you omit the brackets, only the statement that immediately follows the condition would be executed. Just as you can write one if condition, you can write more than one. Here are examples: using System;
public enum HouseType
{
Unknown,
SingleFamily,
Townhouse,
Condominium
}
class Program
{
static void Main()
{
HouseType type = HouseType.Unknown;
int choice;
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);
}
}
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 . . .
class Program
{
static void Main()
{
HouseType type = HouseType.Unknown;
int choice;
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");
}
}
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
}
class Program
{
static void Main()
{
HouseType type = HouseType.Unknown;
int choice;
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");
}
}
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 . . .
|
|
|
||
| Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|
|
||