Selection Statements


A selection statement evaluates an expression to determine what code is executed next. Based on the expression, a selection statement transfers control to either the next or some other statement.

An if statement evaluates a Boolean expression. If the expression is true, control is transferred to the next statement_block. If the expression is false, execution is transferred to the first statement after the statement_block.

Here's an example of an if statement:

 if(boolean_expression) {statement_block} 

When combined with an else condition, the if statement has true_statement_block and false_statement_block. The false_statement_block immediately follows the else statement. When the boolean_expression is true, you are transferred to the true_statement_block. If it is false, control is transferred to the false_statement_block. If nested, the else statement belongs to the nearest if statement, and each else statement must have a matching if statement.

Here is the syntax:

 if(boolean_expression)      true_statement; else      false_statement; 

An alternative to nested if and else statements is the else if clause, which is particularly useful in evaluating choices. The else if statement can also be combined with an else statement.

The syntax appears here:

 if(boolean_expression1)      true_statement; else if(boolean_expression2)      true_statement2; else if(boolean_expressionn)      true_statementn; else      false_statement; 

The following code is typical:

 static void Main() {     Console.WriteLine("Enter command:");     string menuChoice=(Console.ReadLine()).ToLower();     if(menuChoice=="a")         Console.WriteLine("Doing Task A");     else if(menuChoice=="b")         Console.WriteLine("Doing Task B");     else if(menuChoice=="c")         Console.WriteLine("Doing Task C");     else         Console.WriteLine("Bad choice"); } 

A switch statement, which is a better solution to the preceding code, jumps to the switch label that matches a switch expression. The switch expression must resolve to an integral, char, enum, or string type. The switch label is a constant or literal and must have the same underlying type as the switch expression.

Switch statement:

 switch(switch_expression) {     case switch_label1:         switch_statement1;     case case_labeln:         switch_statementn;     default:         default_statement; } 

A switch statement has a switch expression that is followed by a switch block, which should contain one or more case statements. A case statement identifies a switch label. After the switch expression is evaluated, control is transferred to the matching switch label. A matching label has the same value as the switch expression. Each switch label must be unique. If no switch label matches the switch expression, control is transferred to the default label or to the first statement after the switch statement if a default label is not provided.

Unlike C and C++, cascading between case statements is not allowed—you cannot "crash the party" of another case statement. A break, goto, return, or throw are some of the ways to preclude falling into the next case. There is one exception to this rule, however: You can fall through cases that have no statements.

This is an alternative to the if code presented earlier:

 static void Main() {     Console.WriteLine("Enter command:");     string resp=(Console.ReadLine()).ToLower();     switch(resp) {         case "a":             Console.WriteLine("Doing Task A");             break;         case "b":             Console.WriteLine("Doing Task B");             break;         case "c":             Console.WriteLine("Doing Task C");             break;         default:             Console.WriteLine("Bad choice");             break;     } } 

Any object, value, or reference type that is convertible to an integral, char, enum, or string type is acceptable as the switch_expression, which is demonstrated in the following code. It must be a one-step conversion to one of the acceptable types.

 class Employee {     public Employee(string f_Emplid) {         m_Emplid=f_Emplid;     }     static public implicit operator string(Employee f_this) {         return f_this.m_Emplid;     }     private string m_Emplid; } class Starter {     static void Main() {         Employee newempl=new Employee("1234");         switch(newempl) {             case "1234":                 Console.WriteLine("Employee 1234");                 return;             case "5678":                 Console.WriteLine("Employee 5678");                 return;             default:                 Console.WriteLine("Invalid employee");                 return;         }     } } 




Programming Microsoft Visual C# 2005(c) The Language
Microsoft Visual Basic 2005 BASICS
ISBN: 0619267208
EAN: 2147483647
Year: 2007
Pages: 161

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