Controlling the Conditional Statements |
|
Consider the following program: using namespace System; int main() { int TypeOfHome; do { Console::WriteLine(L"What Type of House Would you Like to Purchase?"); Console::WriteLine(L"1 - Single Family"); Console::WriteLine(L"2 - Town House"); Console::WriteLine(L"3 - Condominium"); Console::Write(L"Your Choice? "); TypeOfHome = int::Parse(Console::ReadLine()); } while( (TypeOfHome < 1) || (TypeOfHome > 3) ); if( TypeOfHome == 1 ) Console::WriteLine(L"\nType of Home: Single Family"); else if( TypeOfHome == 2 ) Console::WriteLine(L"\nType of Home: Town House"); else if( TypeOfHome == 3 ) Console::WriteLine(L"\nType of Home: Condominium"); Console::WriteLine(); return 0; } This is used to request one of the numbers 1, 2, or 3 from the user. Any number below 1 or above 3 is not accepted. Here is an example of running the program: What Type of House Would you Like to Purchase? 1 - Single Family 2 - Town House 3 - Condominium Your Choice? 8 What Type of House Would you Like to Purchase? 1 - Single Family 2 - Town House 3 - Condominium Your Choice? 6 What Type of House Would you Like to Purchase? 1 - Single Family 2 - Town House 3 - Condominium Your Choice? 3 Type of Home: Condominium Press any key to continue . . . If the user enters an invalid value, the question is simply being asked again. It would be professional to let the user know why the request came back even though what appears as a normal number was entered. To solve this and other types of problems, you can write one conditional statement inside of another. This is referred to as nesting. To create a conditional statement inside of another, simply proceed as we have done so far to create them. Here is an example: using namespace System; int main() { int TypeOfHome; do { Console::WriteLine(L"What Type of House Would you Like to Purchase?"); Console::WriteLine(L"1 - Single Family"); Console::WriteLine(L"2 - Town House"); Console::WriteLine(L"3 - Condominium"); Console::Write(L"Your Choice? "); TypeOfHome = int::Parse(Console::ReadLine()); if( (TypeOfHome < 1) || (TypeOfHome > 3) ) Console::WriteLine(L"Invalid Choice: Please try againe"); } while( (TypeOfHome < 1) || (TypeOfHome > 3) ); if( TypeOfHome == 1 ) Console::WriteLine(L"\nType of Home: Single Family"); else if( TypeOfHome == 2 ) Console::WriteLine(L"\nType of Home: Town House"); else if( TypeOfHome == 3 ) Console::WriteLine(L"\nType of Home: Condominium"); Console::WriteLine(); return 0; } Here is another example of running the program: What Type of House Would you Like to Purchase? 1 - Single Family 2 - Town House 3 - Condominium Your Choice? 0 Invalid Choice: Please try againe What Type of House Would you Like to Purchase? 1 - Single Family 2 - Town House 3 - Condominium Your Choice? 6 Invalid Choice: Please try againe What Type of House Would you Like to Purchase? 1 - Single Family 2 - Town House 3 - Condominium Your Choice? 2 Type of Home: Town House Press any key to continue . . . The break statement is used to stop a loop for any reason or condition when necessary. The formula of the break statement is: break; Although made of only one word, the break statement is a complete statement; therefore, it can (and should always) stay on its own line (this makes the program easy to read). The break statement applies to the most previous conditional statement to it; provided that previous statement is applicable. The break statement can be used in a while condition, in a do…while or a for loops to stop an ongoing action. Here is an example that is used to count the levels of a house from 1 to 12 but it is asked to stop at 3: using namespace System; int main() { for(int Stories = 1; Stories <= 12; Stories++) { Console::WriteLine("Story {0}", Stories); if( Stories == 3 ) break; } Console::WriteLine(); return 0; } This would produce: Story 1 Story 2 Story 3 Press any key to continue . . . Consider an example we used for the switch statement: using namespace System; int main() { int TypeOfHome; Console::WriteLine(L"What Type of House Would you Like to Purchase?"); Console::WriteLine(L"1 - Single Family"); Console::WriteLine(L"2 - Town House"); Console::WriteLine(L"3 - Condominium"); Console::Write(L"Your Choice? "); TypeOfHome = int::Parse(Console::ReadLine()); switch(TypeOfHome) { case 1: Console::WriteLine(L"\nType of Home: Single Family"); case 2: Console::WriteLine(L"\nType of Home: Town House"); case 3: Console::WriteLine(L"\nType of Home: Condominium"); default: Console::WriteLine(L"\nType of Home:: Unknown"); } Console::WriteLine(); return 0; } Here is an example of running the program: What Type of House Would you Like to Purchase? 1 - Single Family 2 - Town House 3 - Condominium Your Choice? 1 Type of Home: Single Family Type of Home: Town House Type of Home: Condominium Type of Home:: Unknown Press any key to continue . . . Notice that, although the user would have entered 1, the other cases would have executed as well. The break statement can be used to handle the cases in a switch statement. For example, it can be used to stop execute when a valid case has been found. To use it, before the end of a case, execute the break statement. The above program can better be written as follows: using namespace System; int main() { int TypeOfHome; Console::WriteLine(L"What Type of House Would you Like to Purchase?"); Console::WriteLine(L"1 - Single Family"); Console::WriteLine(L"2 - Town House"); Console::WriteLine(L"3 - Condominium"); Console::Write(L"Your Choice? "); TypeOfHome = int::Parse(Console::ReadLine()); switch(TypeOfHome) { case 1: Console::WriteLine(L"\nType of Home: Single Family"); break; case 2: Console::WriteLine(L"\nType of Home: Town House"); break; case 3: Console::WriteLine(L"\nType of Home: Condominium"); break; default: Console::WriteLine(L"\nType of Home:: Unknown"); } Console::WriteLine(); return 0; } Here is an example of executing the program: What Type of House Would you Like to Purchase? 1 - Single Family 2 - Town House 3 - Condominium Your Choice? 1 Type of Home: Single Family Press any key to continue . . .
The continue statement uses the following formula: continue; When processing a loop, if the statement finds a false value, you can use the continue statement inside of a while, a do…while or a for conditional statements to ignore the subsequent statement or to jump from a false Boolean value to the subsequent valid value, unlike the break statement that would exit the loop. Like the break statement, the continue keyword applies to the most previous conditional statement and should stay on its own line. Here is an example when a program is supposed to count the levels of a house from 1 to 6: using namespace System; int main() { for(int Stories = 1; Stories <= 6; Stories++) { if( Stories == 3 ) continue; Console::WriteLine("Story {0}", Stories); } Console::WriteLine(); return 0; } This would produce: Story 1 Story 2 Story 4 Story 5 Story 6 Press any key to continue . . . Notice that, when the compiler gets to 3, it ignores it. The goto statement allows a program execution to jump to another section of the function in which it is being used. In order to use the goto statement, insert a name on a particular section of your function so you can refer to that name. The name, also called a label, is made of one word and follows the rules we have learned about C++ names (the name can be anything), then followed by a colon. Here is an example where the program is supposed to count the levels of a 14 story building: using namespace System; int main() { for(int Stories = 1; Stories <= 14; Stories++) { if( Stories == 4 ) goto CountUpTo3; Console::WriteLine("Story {0}", Stories); } CountUpTo3: Console::WriteLine(L"Our homes have only up to 3 levels\n"); return 0; } This would produce: Story 1 Story 2 Story 3 Our homes have only up to 3 levels Press any key to continue . . . |
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|