The switch...case Statement

 

The if Statement

The if statement was originally called a branching statement , meaning that a decision had to be made so the program could branch one way or the other. There are only two branches for the if statement. If more than two branches are required, most programmers turn to the switch case statement (which is discussed at the end of this chapter). The switch case statement can branch any number of ways.

This is the if statement formulation:

 if( /*Statement that can be either true or false*/) {   If 'true', then execute these statements. } else {   If 'false', then execute these statements. } 

The else portion of the statement is optional. The statement within the if statement can be a compound statement:

 // IA030: if((strTarget == 'AA') && (intShotNumber < 7)) MessageBox.Show("Msg #3: Bull's Eye!"); else MessageBox.Show("Msg #4: Fix bayonets."); 

In a compound statement, both boolean statements must be true if the complete statement is true. If either one or both of the boolean statements is false, then the complete statement is false. This will be demonstrated in the demonstration program IfAndLoop that you will be asked to run at the end of the chapter.

Many for loops contain an if statement inside the loop, and it is the if statement that determines whether the focus should remain with the loop or break out and process the code that follows the loop.

 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

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