Teach Yourself Visual Studio® .NET 2003 in 21 Days [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Teach Yourself Visual Studio® .NET 2003 in 21 Days [Electronic resources] - نسخه متنی

Jason Beres

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Using Decision Structures


Now that you have an understanding of the common data types and common operators, you can dig into using decision structures. Decision structures are program elements that control the flow of your application based on decisions made about the value of variables or events fired by the user.

Table 8.5 lists the decision structures by group in Visual Basic .NET and C# and gives the keywords that you'll use when implementing decision structures in your code.




































Table 8.5. Summary of Decision Structures in Visual Basic .NET and C#

Type


Visual Basic .NET


C#


Selection


Select Case


switch


Decision


If...Then


if...else


Looping


While


Do


Do

Do Until

Loop While

Loop Until


While


Looping structure or collections


For

For Each


for

foreach

We'll go through each of these decision structures so that you can see how to implement them and learn the syntactical difference between Visual Basic .NET and C# when using decision structures.

Using Select Case and Switch


The Select Case statement in Visual Basic .NET and the switch statement in C# are the most commonly used decision structures when you have to perform a logical test on multiple variables and act on the true or false returned by the test.

The syntax for the switch statement is


switch (expression)
{
case constant-expression:
statement
jump-statement
[default:
statement
jump-statement]
}

In the switch statement, the expression being evaluated is compared to the constant expressions in the case clauses. Within each case clause, if the expression being evaluated matches the constant expression, you can use a jump statement to transfer control outside of the switch statement or inside the switch statement. Valid jump statements in C# are listed here:


  • break
    Terminates the closest loop or conditional statement in which it appears. When a break statement is hit, control passes to the next valid statement in the expression.


  • continue
    Passes control to the next iteration of the enclosing iteration statement. This is used in looping and for each statements.


  • default
    If the expression being evaluated in a switch statement doesn't match any of the corresponding case constant expressions, the default label is used. If there's no default label, execution continues after the switch statement.


  • goto
    Transfers control to a label within the looping or switch statement.


  • return
    Terminates execution of the executing method and passes control back to the original caller.



Although every type of jump statement isn't always used in a switch statement, they can be. The commented code in Listing 8.6 uses the switch statement to check a value of a variable.

Listing 8.6 Using the switch Statement to Evaluate an Expression



string strName = "Gates";
switch(strName)
{
case "Ballmer":
// perform an action if the last name is Ballmer
break;
case "Jobs":
// perform an action if the last name is Jobs
break;
case "Gates":
// perform an action if the last name is Gates
MessageBox.Show("Gates has been entered");
break;
default:
MessageBox.Show("No Valid Names Entered");
break;
}

In Listing 8.6, the MessageBox prompt is displayed when the case "Gates" is hit because the value of the strName variable is set to "Gates".

The Select Case statement in Visual Basic .NET works the same way as the switch statement in C#. The syntax for Select Case is


SelectStatement ::=
Select [ Case ] Expression StatementTerminator
[ CaseStatement+ ]
[ CaseElseStatement ]
End Select StatementTerminator
CaseStatement ::=
Case CaseClauses StatementTerminator
[ Block ]
CaseClauses ::=
CaseClause |
CaseClauses , CaseClause
CaseClause ::=
[ Is ] ComparisonOperator Expression |
Expression [ To Expression ]
ComparisonOperator ::= = | < > | < | > | = > | = <
CaseElseStatement ::=
Case Else StatementTerminator
[ Block ]

You can see that Select Case has a few more options than switch. These options give you additional flexibility when comparing values and ranges of values. In Listing 8.7, the Select Case statement is used to compare different numeric values.

Listing 8.7 Using Select Case to Evaluate an Expression



Dim intX As Integer = 50
Select Case intX
Case Is > 51
' perform
Case 43 Or 57 Or 98
' perform an action
Case 59
MessageBox.Show("You hit 50")
Case Else
' this is the default if no value is hit
End Select

In Listing 8.7, you can see the different uses of Case Is and Or within a Case statement. Based on the value of the expression, the corresponding code in the Case statement executes. When an expression evaluates to true and the code for the Case statement executes, the next line of code that executes is the line immediately following the End Select statement. Like default in the switch statement, Case Else is the optional catchall statement if none of the Case expressions evaluates to true.

Using If...Then Statements


You can use If...Then statements to execute blocks of code based on the Boolean value of a condition. If...Then statements are similar to switch and Select Case statements, but are better used when the available expressions being evaluated is limited. The syntax for If...Then statements in Visual Basic .NET is


If condition [ Then ]
[ statements ]
[ ElseIf elseifcondition [ Then ]
[ elseifstatements ] ]
[ Else
[ elsestatements ] ]
End If

The corresponding C# syntax is


if (expression)
statement1
[else
statement2]

Notice that the C# code doesn't actually use the Then statement. The Then statement is unique to Visual Basic .NET. Visual Basic .NET also uses ElseIf as one word, whereas C# just evaluates another if statement with the else keyword.

If the expression in an If or Else statement evaluates to true, the code block executes and control is passed to the line of code immediately following the end of the If block. Listing 8.8 gives an example of equivalent If...Then statements in Visual Basic .NET and C#.

Listing 8.8 Using an If...Then Statement to Evaluate an Expression



Dim intX As Integer = 10
If intX > 11 Then
MessageBox.Show("intX > 11")
ElseIf intX < 5 Then
MessageBox.Show("intX < 5")
Else
MessageBox.Show("Fallback code block")
End If



int intX = 10;
if (intX > 11)
MessageBox.Show("intX > 11");
else if (intX < 5)
MessageBox.Show("intX < 5");
else
MessageBox.Show("Fallback code block");

In the C# code, notice that the expression being evaluated in the if statement must be enclosed in parentheses. Each else statement is followed by another if statement enclosed in parentheses, and each code block is a statement ending with a semicolon. In Visual Basic .NET, you don't need to use parentheses to evaluate the expression in the If statement. If statements can also be nested in both Visual Basic .NET and C#, so if an expression evaluates to true, you can have additional nested If statements within the code block that executes the statement. There's no limit to nesting If statements, but for readability, you don't want to have too many levels of nested Ifs. As I mentioned earlier, if you have many expressions to evaluate, you should consider using a Select Case or switch statement, which is easier to understand and debug.

Using Looping Structures


Looping structures enable you to perform an action any number of times based on a predetermined variable or until a condition is met. The different looping structures and implementation differences can be broken down as follows:


  • While in Visual Basic .NET and while in C# execute a block of statements while a condition is true.


  • Do...Loop structures in Visual Basic .NET and the do structure in C# execute a block of statements until a condition is satisfied.


  • For...Next structures in Visual Basic .NET and the for structure in C# execute a block of statements a specified number of times.


  • For...Each structures in Visual Basic .NET and the foreach structure in C# execute a block of statements for each item in a collection.



Listing 8.9 demonstrates the While, Do, and For Next loops in Visual Basic .NET and C#. Read the commented code to get an idea of what's happening, and why, when the loop structures execute.

Listing 8.9 Using Looping Structures in Visual Basic .NET and C#



' Use a while loop to perform an action while a condition is true
Dim intX As Integer = 1
While intX < 6
' Perform this code while the value of intX is < 6
intX += 1
End While
' Use a do loop until the while condition is met
Dim intX As Integer
Dim intY As Integer
Do
' perform this code block while the value of intY < 10
intX = intY + 1
Loop While intY < 10
' Use a for loop to repeat the same code block
' until a value is hit, in this case 5
Dim intX As Integer
For intX = 1 To 5
Console.WriteLine(intX)
Next intX



// Use a while loop to perform an action while a condition is true
int intX = 1;
while (intX < 6)
{
// Perform this code while the value of intX is < 6
intX++;
}
// Use a do loop until the while condition is met
int intX;
int intY;
do
{
// perform this code block while the value of intY < 10
intX = intY++;
}
while(intY < 10);
// Use a for loop to repeat the same code block
// until a value is hit, in this case 5
for (int intX = 1; intX <= 5; intX++)
Console.WriteLine(intX);

In several lessons this week, you'll use the various looping structures as you learn more about Visual Studio .NET and the language features of Visual Basic .NET and C#.

/ 270