Beruflich Dokumente
Kultur Dokumente
Outline
Introduction Essentials of Counter-Controlled Repetition For/Next Repetition Structure Examples Using the For/Next Structure Select Case Multiple-Selection Structure Do/Loop While Repetition Structure Do/Loop Until Repetition Structure Using the Exit Keyword in a Repetition Structure Logical Operators Structured Programming Summary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
' Fig. 5.2: ForCounter.vb ' Using the For/Next structure to demonstrate counter-controlled ' repetition. Module modForCounter
Control variable Sub Main() initialized to 2 Dim counter As Integer
' initialization, repetition condition and ' incrementing are included in For structure For counter = 2 To 10 Step 2 Console.Write(counter & " ") Next End Sub ' Main
Next marks end of loop
2 4 6 8 10
Executes until control variable greater (or less) than final value
For keyword
To keyword
Step keyword
Fig. 5.3
MessageBoxIcon.Exclamation
Icon containing an exclamation point. Typically used to caution the user against potential problems. Icon containing the letter "i." Typically used to display information about the state of the application. Icon containing a question mark. Typically used to ask the user a question. Icon containing an in a red circle. Typically used to alert the user of errors or critical situations.
MessageBoxIcon.Information
MessageBoxIcon.Question MessageBoxIcon.Error
Fig. 5.6
MessageBoxButton.OK MessageBoxButton.OKCancel
OK button. Allows the user to acknowledge a message. Included by default. OK and Cancel buttons. Allow the user to either continue or cancel an operation. Yes and No buttons. Allow the user to respond to a question Yes, No and Cancel buttons. Allow the user to respond to a question or cancel an operation. Retry and Cancel buttons. Typically used to allow the user to either retry or cancel an operation that has failed. Abort, Retry and Ignore buttons. When one of a series of operations has failed, these butons allow the user to abort the entire sequence, retry the failed operation or ignore the failed operation and continue..
MessageBoxButton.YesNo MessageBoxButton.YesNoCancel
MessageBoxButton.RetryCancel
MessageBoxButton.AbortRetry-
Ignore
Fig. 5.7
Fig. 5.7
Newline character (vbCrLf) to start the next output on the next line.
Program Output
Currency. Precedes the number with $, separates every three digits with commas and sets the number of decimal places to two. Scientific notation. Displays one digit to the left of the decimal and six digits to the right of the decimal, followed by the character E and a three-digit integer representing the exponent of a power of 10. For example, 956.2 is formatted a 9.562000E+002.. Fixed point. Sets the number of decimal places to two. General. Visual Basic chooses either E or F for you, depending on which representation generates a shorter string. Decimal. Displays an integer as a whole number in standard base-10 format. Number. Separates every three digits with a comma and sets the number of decimal places to two. Hexadecimal integer. Displays the integer in hexadecimal (base-16) notation. We discuss hexidecimal notation in Appendix B.
F G D N X Fig. 5.9
Either 0 or any value in the range 10 to 59, inclusive matches this Case.
Program Output
Case a action(s)
Case b
false . . .
true
Case b action(s)
Case z
false
true
Case z action(s)
Begins with keyword Do Ends with keywords Loop While followed by condition
action(s)
1 2 3 4 5
action(s)
Exit For
Executed in For structures
Exit While
Executed in While structures
Logical Operators
To handle multiple conditions more efficiently, Visual Basic provides logical operators that can be used to form complex conditions by combining simple ones. The logical operators are AndAlso, And, OrElse, Or, Xor and Not. We consider examples that use each of these operators.
Logical Operators
1. Logical operator with short-circuit evaluation: Execute only until truth or
falsity is known:
AndAlso operator
Returns true if and only if both conditions are true
OrElse operator
Returns true if either or both of two conditions are true
Logical Operators
2. Logical Operators without short-circuit evaluation:
And and Or
Similar to AndAlso and OrElse respectively Always execute both of their operands Used when an operand has a side effect
Condition makes a modification to a variable Should be avoided to reduce subtle errors
Xor
Returns true if and only if one operand is true and the other false
Logical Operators
Normally, there is no compelling reason to use the And and Or operators instead of AndAlso and OrElse. However, some programmers make use of them when the right operand of a condition produces a side effect (such as a modification of a variables value) or if the right operand includes a required method call, as in the following program segment:
Console.WriteLine("How old are you?") If (gender = "F" And Console.ReadLine() >= 65) Then Console.WriteLine("You are a female senior citizen.") End If
Here, the And operator guarantees that the condition Console.ReadLine() >= 65 is evaluated, so ReadLine is called regardless of whether the overall expression is true or false. It would be better to write this code as two separate statementsthe first would store the result of Console.ReadLine() in a variable, then the second would use that variable with the AndAlso operator in the condition.
Logical Operators
Logical Negation
Not
Used to reverse the meaning of a condition Unary operator
Requires one operand
Logical Operators
exp ression1
exp ression2
Fig. 5.17 Truth table for the AndAlso (logical AND) operator.
Logical Operators
exp ression1
exp ression2
Fig. 5.18 Truth table for the OrElse (logical OR) operator.
Logical Operators
exp ression1 exp ression2 exp ression1 Xor exp re ssio n2
Fig. 5.19 Truth table for the boolean logical exclusive OR (Xor) operator.
exp ression
False True
Fig. 5.20
True False
Truth ta b le fo r op e ra to r Not (lo g ic a l NOT).
Program Output
Logical Operators
Op era tors Assoc ia tivity Typ e
() ^ + * / \
left to right left to right left to right left to right left to right left to right left to right left to right left to right left to right left to right left to right left to right
Integer division
modulus additive concatenation relational and equality logical NOT boolean logical AND boolean logical inclusive OR boolean logical exclusive OR
Mod
+ & < <= > >= = <>
Repetition
While structure sufficient to provide any form of repetition
T F
. . .
. . .
T F
T F
F T F
T F T
T F
Conclusion
The For/Next repetition structure handles the details of counter-controlled repetition. The required To keyword specifies the initial value and the final value of the control variable. The optional Step keyword specifies the increment. When supplying four arguments to method MessageBox.Show, the first two arguments are strings displayed in the dialog and the dialogs title bar. The third and fourth arguments are constants representing buttons and icons, respectively.
Conclusion
Visual Basic provides the Select Case multipleselection structure to test a variable or expression separately for each value that the variable or expression might assume. The Select Case structure consists of a series of Case labels and an optional Case Else. Each Case contains statements to be executed if that Case is selected.
Conclusion
The logical operators are AndAlso (logical AND with short-circuit evaluation), And (logical AND without short-circuit evaluation), OrElse (logical inclusive OR with short-circuit evaluation), Or (logical inclusive OR without short-circuit evaluation), Xor (logical exclusive OR) and Not (logical NOT, also called logical negation).