Conditional Statements |
|
A conditional expression is an expression that produces a true or false result. You can then 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.
Consider the following program: using namespace System; int main() { __wchar_t TypeOfHome; Console::WriteLine(L"What Type of House Would you Like to Purchase?"); Console::WriteLine(L"S - Single Family"); Console::WriteLine(L"T - Town House"); Console::WriteLine(L"C - Condominium"); Console::Write(L"Your Choice? "); TypeOfHome = __wchar_t::Parse(Console::ReadLine()); Console::WriteLine(L"\nType of Home: {0}", TypeOfHome); Console::WriteLine(); return 0; } Here is an example of running the program: What Type of House Would you Like to Purchase? S - Single Family T - Town House C - Condominium Your Choice? S Type of Home: S 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 namespace System; int main() { __wchar_t TypeOfHome; Console::WriteLine(L"What Type of House Would you Like to Purchase?"); Console::WriteLine(L"S - Single Family"); Console::WriteLine(L"T - Town House"); Console::WriteLine(L"C - Condominium"); Console::Write(L"Your Choice? "); TypeOfHome = __wchar_t::Parse(Console::ReadLine()); Console::Write(L""); if(TypeOfHome == L'S') Console::WriteLine(L"\nType of Home: Single Family"); Console::WriteLine(); return 0; } Here is an example of running the program: What Type of House Would you Like to Purchase? S - Single Family T - Town House C - Condominium Your Choice? S Type of Home: Single Family 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 namespace System; int main() { __wchar_t TypeOfHome; Console::WriteLine(L"What Type of House Would you Like to Purchase?"); Console::WriteLine(L"S - Single Family"); Console::WriteLine(L"T - Town House"); Console::WriteLine(L"C - Condominium"); Console::Write(L"Your Choice? "); TypeOfHome = __wchar_t::Parse(Console::ReadLine()); if(TypeOfHome == L'S') Console::WriteLine(L"\nType of Home: Single Family"); Console::WriteLine(); return 0; } You can also write the Statement on its own line 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 namespace System; int main() { __wchar_t TypeOfHome; Console::WriteLine(L"What Type of House Would you Like to Purchase?"); Console::WriteLine(L"S - Single Family"); Console::WriteLine(L"T - Town House"); Console::WriteLine(L"C - Condominium"); Console::Write(L"Your Choice? "); TypeOfHome = __wchar_t::Parse(Console::ReadLine()); if(TypeOfHome == L'S') { Console::Write(L"\nType of Home: "); Console::WriteLine(L"Single Family"); } Console::WriteLine(); return 0; } 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 namespace System; int main() { __wchar_t TypeOfHome; Console::WriteLine(L"What Type of House Would you Like to Purchase?"); Console::WriteLine(L"S - Single Family"); Console::WriteLine(L"T - Town House"); Console::WriteLine(L"C - Condominium"); Console::Write(L"Your Choice? "); TypeOfHome = __wchar_t::Parse(Console::ReadLine()); if(TypeOfHome == L'S') Console::WriteLine(L"\nType of Home: Single Family"); if(TypeOfHome == L'T') Console::WriteLine(L"\nType of Home: Town House"); if(TypeOfHome == L'C') Console::WriteLine(L"\nType of Home: Condominium"); Console::WriteLine(); return 0; } Here is an example of running the program: What Type of House Would you Like to Purchase? S - Single Family T - Town House C - Condominium Your Choice? C Type of Home: Condominium Press any key to continue . . . |
Practical Learning: Using the Simple if Condition |
#include "StoreItem.h" using namespace System; using namespace ElectronicsStore; CStoreItem ^ CreateStoreItem(); static void DescribeStoreItem(CStoreItem ^ %); int main() { String ^ strTitle = L"=-= Nearson Electonics =-=\n" L"******* Store Items ******"; CStoreItem ^ saleItem = CreateStoreItem(); Console::WriteLine(L""); DescribeStoreItem(saleItem, 0); Console::WriteLine(); return 0; } CStoreItem ^ CreateStoreItem() { CStoreItem ^ sItem = gcnew CStoreItem; int category; Console::WriteLine(L"To create a store item, enter its information"); Console::Write(L"Item Number: "); sItem->ItemNumber = long::Parse(Console::ReadLine()); Console::WriteLine(L"Category"); Console::WriteLine(L"1. Unknown/Miscellaneous"); Console::WriteLine(L"2. Cables and Connectors"); Console::WriteLine(L"3. Cell Phones and Accessories"); Console::WriteLine(L"4. Headphones"); Console::WriteLine(L"5. Digital Cameras"); Console::WriteLine(L"6. PDAs and Accessories"); Console::WriteLine(L"7. Telephones and Accessories"); Console::WriteLine(L"8. TVs and Videos - Plasma / LCD"); Console::WriteLine(L"9. Surge Protector"); Console::WriteLine(L"10. Instructional and Tutorials (VHS & DVD)TVs and Videos"); Console::Write(L"Your Choice? "); category = int::Parse(Console::ReadLine()); if( category == 1 ) sItem->Category = ItemsCategories::Unknown; if( category == 2 ) sItem->Category = ItemsCategories::CablesAndConnectors; if( category == 3 ) sItem->Category = ItemsCategories::CellPhonesAndAccessories; if( category == 4 ) sItem->Category = ItemsCategories::Headphones; if( category == 5 ) sItem->Category = ItemsCategories::DigitalCameras; if( category == 6 ) sItem->Category = ItemsCategories::PDAsAndAccessories; if( category == 7 ) sItem->Category = ItemsCategories::TelephonesAndAccessories; if( category == 8 ) sItem->Category = ItemsCategories::TVsAndVideos; if( category == 9 ) sItem->Category = ItemsCategories::SurgeProtectors; if( category == 10 ) sItem->Category = ItemsCategories::Instructional; Console::Write(L"Make "); sItem->Make = Console::ReadLine(); Console::Write(L"Model: "); sItem->Model = Console::ReadLine(); Console::Write(L"Unit Price: "); sItem->UnitPrice = double::Parse(Console::ReadLine()); return sItem; } void DescribeStoreItem(CStoreItem ^ %item) { Console::WriteLine(L"Store Item Description"); Console::WriteLine(L"Item Number: {0}", item->ItemNumber); Console::WriteLine(L"Category: {0}", item->Category); Console::WriteLine(L"Make {0}", item->Make); Console::WriteLine(L"Model: {0}", item->Model); Console::WriteLine(L"Unit Price: {0:C}", item->UnitPrice); } |
To create a store item, enter its information Item Number: 237875 Category 1. Unknown/Miscellaneous 2. Cables and Connectors 3. Cell Phones and Accessories 4. Headphones 5. Digital Cameras 6. PDAs and Accessories 7. Telephones and Accessories 8. TVs and Videos - Plasma / LCD 9. Surge Protector 10. Instructional and Tutorials (VHS & DVD)TVs and Videos Your Choice? 4 Make Tritton Model: AX360 Unit Price: 145.85 Store Item Description Item Number: 237875 Category: Headphones Make Tritton Model: AX360 Unit Price: $145.85 Press any key to continue . . . |
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 namespace System; int main() { Byte Stories; Console::Write(L"What's the maximum number of levels you want? "); Stories = Byte::Parse(Console::ReadLine()); if(Stories == 1 ) Console::WriteLine(L"\nMaximum Levels: Single Level"); else Console::WriteLine(L"\nMaximum Levels: Any Number"); Console::WriteLine(); return 0; }
Here is an example of running the program:
What's the maximum number of levels you want? 3 Maximum Levels: Any Number Press any key to continue . . .
Here is another example of running the program:
What's the maximum number of levels you want? 1 Maximum Levels: Single Level Press any key to continue . . .
Practical Learning: Using the if...else Condition |
#pragma once using namespace System; . . . namespace ElectronicsStore { public ref class CStoreItem { public: . . . private: long nbr; ItemsCategories cat; String ^ mk; String ^ mdl; String ^ nm; double price; public: . . . property double UnitPrice { double get() { return price; } void set(double p) { if( p <= 0 ) this->price = 0.00; else this->price = p; } } }; } |
#include "StoreItem.h" using namespace System; using namespace ElectronicsStore; CStoreItem ^ CreateStoreItem(); static void DescribeStoreItem(CStoreItem ^ %); static void DescribeStoreItem(CStoreItem ^ %, const int); int main() { String ^ strTitle = L"=-= Nearson Electonics =-=\n" L"******* Store Items ******"; CStoreItem ^ saleItem = CreateStoreItem(); Console::WriteLine(L""); if( saleItem->Category == ItemsCategories::Unknown ) DescribeStoreItem(saleItem, 0); else DescribeStoreItem(saleItem); Console::WriteLine(); return 0; } CStoreItem ^ CreateStoreItem() { CStoreItem ^ sItem = gcnew CStoreItem; int category; Console::WriteLine(L"To create a store item, enter its information"); Console::Write(L"Item Number: "); sItem->ItemNumber = long::Parse(Console::ReadLine()); Console::WriteLine(L"Category"); Console::WriteLine(L"1. Unknown/Miscellaneous"); Console::WriteLine(L"2. Cables and Connectors"); Console::WriteLine(L"3. Cell Phones and Accessories"); Console::WriteLine(L"4. Headphones"); Console::WriteLine(L"5. Digital Cameras"); Console::WriteLine(L"6. PDAs and Accessories"); Console::WriteLine(L"7. Telephones and Accessories"); Console::WriteLine(L"8. TVs and Videos - Plasma / LCD"); Console::WriteLine(L"9. Surge Protector"); Console::WriteLine(L"10. Instructional and Tutorials (VHS & DVD)TVs and Videos"); Console::Write(L"Your Choice? "); category = int::Parse(Console::ReadLine()); // If the user specifies that the type is not known // then we need only the name/description of the item if( category == 1 ) { sItem->Category = ItemsCategories::Unknown; Console::Write(L"Enter the item name or description: "); sItem->Name = Console::ReadLine(); } else { if( category == 2 ) sItem->Category = ItemsCategories::CablesAndConnectors; if( category == 3 ) sItem->Category = ItemsCategories::CellPhonesAndAccessories; if( category == 4 ) sItem->Category = ItemsCategories::Headphones; if( category == 5 ) sItem->Category = ItemsCategories::DigitalCameras; if( category == 6 ) sItem->Category = ItemsCategories::PDAsAndAccessories; if( category == 7 ) sItem->Category = ItemsCategories::TelephonesAndAccessories; if( category == 8 ) sItem->Category = ItemsCategories::TVsAndVideos; if( category == 9 ) sItem->Category = ItemsCategories::SurgeProtectors; if( category == 10 ) sItem->Category = ItemsCategories::Instructional; // If the user selected a category other than Unknown // then ask the make and model of the item Console::Write(L"Make "); sItem->Make = Console::ReadLine(); Console::Write(L"Model: "); sItem->Model = Console::ReadLine(); } Console::Write(L"Unit Price: "); sItem->UnitPrice = double::Parse(Console::ReadLine()); return sItem; } // This function is used when an item is specified by its make and model void DescribeStoreItem(CStoreItem ^ %item) { Console::WriteLine(L"Store Item Description"); Console::WriteLine(L"Item Number: {0}", item->ItemNumber); Console::WriteLine(L"Category: {0}", item->Category); Console::WriteLine(L"Make {0}", item->Make); Console::WriteLine(L"Model: {0}", item->Model); Console::WriteLine(L"Unit Price: {0:C}", item->UnitPrice); } // This function is used when an item is specified by its name void DescribeStoreItem(CStoreItem ^ %item, const int) { Console::WriteLine(L"Store Item Description"); Console::WriteLine(L"Item Number: {0}", item->ItemNumber); Console::WriteLine(L"Category: Miscellaneous/Accessories"); Console::WriteLine(L"Name: {0}", item->Name); Console::WriteLine(L"Unit Price: {0:C}", item->UnitPrice); } |
To create a store item, enter its information Item Number: 204006 Category 1. Unknown/Miscellaneous 2. Cables and Connectors 3. Cell Phones and Accessories 4. Headphones 5. Digital Cameras 6. PDAs and Accessories 7. Telephones and Accessories 8. TVs and Videos - Plasma / LCD 9. Surge Protector 10. Instructional and Tutorials (VHS & DVD)TVs and Videos Your Choice? 5 Make Kodak Model: Easyshare CD33 Unit Price: 69.95 Store Item Description Item Number: 204006 Category: DigitalCameras Make Kodak Model: Easyshare CD33 Unit Price: $69.95 Press any key to continue . . . |
To create a store item, enter its information Item Number: 268240 Category 1. Unknown/Miscellaneous 2. Cables and Connectors 3. Cell Phones and Accessories 4. Headphones 5. Digital Cameras 6. PDAs and Accessories 7. Telephones and Accessories 8. TVs and Videos - Plasma / LCD 9. Surge Protector 10. Instructional and Tutorials (VHS & DVD)TVs and Videos Your Choice? 1 Enter the item name or description: VoIP Startup Kit Unit Price: 88.85 Store Item Description Item Number: 268240 Category: Miscellaneous/Accessories Name: VoIP Startup Kit Unit Price: $88.85 Press any key to continue . . . |
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|