Flow Control Constructs


Normally in a Managed C++ program, statements are executed sequentially from beginning to end. There will be times when a program is going to execute a portion of code only if certain conditions are true. To handle conditional execution of code, Managed C++ provides two flow control constructs: if and switch.

if Statement

The if statement enables the conditional execution of code based on the evaluated value of some condition. An if statement in its simplest form is as follows:

 if ( condition ) {     statements; } 

The condition can be any expression, but to make more sense it should evaluate to a Boolean value of either true or false. It is perfectly valid to evaluate to a zero (false) or nonzero (true) condition, as well.

Obviously, it is possible to execute a block of code when a condition is not true, as shown here:

 if ( ! condition ) {     statements; } 

What if you want a block of code to execute when a condition is true and some other block of code to execute when the condition is false? You could write two if statements, one for the true condition and one for the false condition, or you could use the if-else statement, which looks like this:

 if ( condition ) {     statements; } else  // ! condition (the comment is optional) {     statements; } 

There is one more construct for if statements. What if you want different blocks of code to be executed based on mutually exclusive conditions? You could write a stream of if conditions, one for each condition, but then each condition would have to be checked, which is a waste of time. Instead, you should use the if-else if-else or nested if construct, which exits the if construct once it matches a condition. The nested if construct looks like this:

 if ( condition1 )  // first mutually exclusive condition {     statements; } else if ( condition2 )  // second mutually exclusive condition {     statements; } else  // optional catch the rest condition {     statements; } 

This example will display a different string depending on the value of the animal variable:

 enum Creature {Dog, Cat, Eagle}; Creature animal; // assign a value to animal animal = Cat; if ( animal == Dog ) {     Console::WriteLine (S"The animal is a dog"); } else if ( animal == Cat ) {     Console::WriteLine (S"The animal is a cat"); } else  // animal is not a dog or cat {     Console::WriteLine (S"Maybe the animal is a bird"); } 

switch Statement

The switch statement is a multiple-choice flow-control construct. It functions in a very similar manner to the nested if construct, except that it only works for integer value types or expressions that evaluate to integers. Basically, the switch statement works like this: The switch expression is checked against each case constant. If a case constant matches the expression, then its associated statements are executed. If no case constant matches the expression, then the default statements are executed. Then the switch statement is exited.

A switch statement looks like this:

 switch ( expression ) {      case constant1:          statements1;          break;      case constant2:          statements2;          break; default:     statements3; } 

You can write the preceding nested if statement as a switch statement, like this:

 switch ( animal ) {     case Dog:          Console::WriteLine (S"The animal is a dog");          break;     case Cat:          Console::WriteLine (S"The animal is a cat");          break;     default:         Console::WriteLine (S"Maybe the animal is a bird"); } 

The first thing you may notice is that each case ends with a break statement. This break statement tells the switch that it is finished. If you fail to place a break statement at the end of a case, then the following case will also be executed. This may sound like a mistake, but there are times when this falling through to the next case is exactly what you will want. For example, this case statement executes the same code for lower- and uppercase characters:

 switch ( keypressed ) {     case  'A':     case 'a':         Console::WriteLine (S"Pressed the A key");         break;     case 'B':     case 'b':          Console::WriteLine (S"Pressed the B key");          break;     default:         Console::WriteLine (S"Pressed some other key"); } 

Caution

A missing break statement is a very common and difficult error to debug, because often the error caused by it does not occur until later in the program.




Managed C++ and. NET Development
Managed C++ and .NET Development: Visual Studio .NET 2003 Edition
ISBN: 1590590333
EAN: 2147483647
Year: 2005
Pages: 169

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net