TechCSE: VB Control Statement

Sunday, 30 October 2011

VB Control Statement


Control Statement

Control statement help determine the behavior of an program.
If…Then :
In the If…Then statement,the condition follows the If and the code follows Then. The code
executed only when if the condition is true.
Ex:
Dim val As integer
val = 5
If TextBox1.Text = val Then
MsgBox(“You are right.”)
End If
Else If :
If your program consists more than one option then you can add a else if or else in your code.
Ex:
If TextBox1.Text = “arka” Then
MsgBox(“Hi Arka”)
ElseIf TextBox1.Text = “Supriya” Then
MsgBox(“Hi Supriya”)
Else
MsgBox(“Welcome”)
End If
Iff :
The Iff function evaluates a condition & returns a value if the condition is true and another
value if the condition is false.
Iff(condition, TrueValue, FalseValue)
label1.Caption = Iff(Text1.Text = “Arka”)
Select Case :
By this you can select only one single expression.
Select Case strCountry
Case “USA” , “Canada”
lblRegion.Caption = “North America”
Case “Germany” , “England”
lblRegion.Caption = “Europe”
Case Else
lblRegion.Caption = “Other”                                                          End Select
Do Statement :
At time you want to do some task repeatedly until a condition is met, like this situation best is
Do statement.
Do
[code]
Loop [{while | until} condition]
Ex :
Do
intCounter = intCounter + 1
Loop while intCounte < 10
' using while
Do
intCounter = intCounter + 1
Loop until intCounte = 10                                                                 ' using untill
For...Next :
Just like Do it also perform a task repeatedly until the condition is true.
For counter = start to end [ Step step]
[code block ]
Next counter
Ex:
lblGraeating.Text = “Hello”
For intCounter = intStart To intEnd
lblGreating.Text = lblGreating.Text & “ “
lblGreating.Text = lblGreating.Text &
strAnswer (intCounter)
Next intCounter
Each...Next :
This is a copy of For...Next but it used for array purpose.
For Each element In Array
[code]
Next element
Ex:   
For Each vntAnswer In strAns
lblGreating.Text = lblGreating.Text & “ “
lblGreating.Text = lblGreating.Text &
Next vntAns
Exit Do :
Its just like the break statement in C/C++. To stop the working of Do statement we use Exit Do
statement,
Ex:                                                                                                 Do
c=c+1
If c = 10 Then
Exit Do
End If
Loop until c = 20
Exit For :
Its same as Exit Do. Here instead of Do is For.
Ex:
For c = 1 to 20
If c = 10 Then
Exit For
End If
Next c

Home
Copyright © TechCSE Urang-kurai