Академический Документы
Профессиональный Документы
Культура Документы
)
1. BRANCHING 2. LOOPS
BRANCHING
If Then Statement
Syntax: If (condition) Then (reaction) End If If there is only one reaction to the condition, the statement can also be expressed without the End If
Monday, February 18, 2013 2
Syntax: If condition Then statement Else other statement End If If the original condition is met, then all the code within the first statement is executed. The optional Else section specifies an alternative statement that will be executed if the condition is false.
Monday, February 18, 2013 3
If..Then..Else Statement
The If-Else statement can be extended to the following form: (called nested if) If condition Then statement ElseIf condition Then other statement ElseIf condition Then other statement ... Else condition Then another statement End If
Monday, February 18, 2013 4
Select Case
If you have a lot of conditional statements, using If..Then..Else could be very messy. For multiple conditional statements, it is better to use Select Case The data type specified in expression must match that of Case values. (see syntax below)
Select Case expression Case value1 Block of one or more VB statements Case value2 Block of one or more VB Statements Case value3 . . . Case Else Block of one or more VB Statements End Select
Monday, February 18, 2013 6
Example 1: Select Case grade Case "A" result.Caption="High Distinction" Case B+" result.Caption="Distinction" Case "B" result.Caption="Credit" Case "C" result.Caption="Pass" Case Else result.Caption="Fail" End Select *Please note that grade is a string, so all the case values such as "A" are of String Monday, February 18, 2013 data type.
Example 2: Select Case mark Case Is >= 85 comment.Caption = "Excellence" Case Is >= 70 comment.Caption = "Good" Case Is >= 60 comment.Caption = "Above Average"
Case Is >= 50 comment.Caption = "Average" Case Else comment.Caption = "Need to work harder" End Select * Note we use the keyword Is here to impose the conditions. This is generally used for numeric data.
LOOPS
Visual Basic allows a procedure to be repeated as many times as long as the processor could support. This is generally called looping
Visual Basic has two main types of loops: (i) for next loops (ii) Do loops
Monday, February 18, 2013 10
For..Next Loops
The syntax of a For..Next loop has three components: a counter, a range, and a step. A basic for..next loop appears as follows:
For counter=start_value to end_value (Step increment)
start_value and end_value can be any numbers, variables or expressions that produce suitable value.
Monday, February 18, 2013 11
Example: (a) For counter=1 to 10 display.Text=counter Next (b) For counter=1 to 1000 step 10 counter=counter+1 Next (c) For counter=1000 to 5 step -5 counter=counter-10 Next
Monday, February 18, 2013 12
Do Loops
Do loops are a bit more flexible then For loops, but should generally only be used when necessary. Do loops come in the following formats: Do while Do until Loop while Loop until
Monday, February 18, 2013 13
While loops (both do while and loop while) will continue to execute as long as a certain conditional is true. An Until loop will loop as long as a certain condition is false, on the other hand. The only difference between putting either While or Until in the Do section or the Loop section, is that Do checks when the loop starts, and Loop check when the loops ends.
14
15
Do WhileLoop
This loop has a condition at the beginning. The statements are repeated as long as the condition is met. If the condition is not met at the very beginning then the statements inside the loop are never executed. Do While condition Block of one or more VB statements Loop
Monday, February 18, 2013 16
17
Syntax: Do Until condition Block of one or more VB statements Loop Example: X =1 Do Until X > 5 Print X X=X+1 Loop
Monday, February 18, 2013 18
Do Untilloop
DoLoop Until
This loop has a condition at the end and the statements are repeated until the condition is met. Since the check is at the end the statements are at least executed once Do Block of one or more VB statements Loop Until condition
Monday, February 18, 2013 19
20
DoLoop While
Syntax: Do Block of one or more VB statements Loop While condition Example X =1 Do Print X X=X+1 Loop while X <= 5
Monday, February 18, 2013 21
22
Syntax: Do Block of one or more VB statements (condition) Exit Do (end condition) Block of one or more VB statements Loop
23
24
NESTED LOOPS
This is when one or more loops are inside the other. They may of the same kind or different. Example:
For i = 1 to 10 For j = 1 to 10 k = i*j print k Next j Next i
Monday, February 18, 2013 25