Conditional Statements
If Then Statement
If Then Else Statement
If Then Else If Statement
Select Case Statement
Looping Statement in VB.NET
For Next Loop
While ..End While
Do while Loop
Sunday, December 19, 2010
Conditional Statements
If conditional expression is one of the most useful control structures which allows us to execute a expression if a condition is true and execute a different expression if it is False.:
Different type of Conditional Statements are there. They are
1. If…Then Statement
2. If…Then…Else Statement
3. If…Then…Elseif Statement
4. Select Case Statement
If Then Statement
The if-then statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true.
Syntax:
If <> Then
Block of the statement
End If
The above Syntax. If the Condition is TRUE the block of the statement will be executed. If if is FALSE nothing will be executed
The following example we have declared Integer type variable a,b and assigning the value for the variable a=10, b=10. Now the condition is true MsgBox(“A is Equal to B”) will be executed and display A is Equal to B.
Example1
Dim a, b As Integer
a=10
b=10
If a = b Then
MsgBox("A is Equal to B" )
End If
The following example we have declared Integer type variable a,b and assigning the value for the variable a=10, b=5. Now the condition is False.nothing will be executed.
Example2
Dim a, b As Integer
a=10
b=5
If a = b Then
MsgBox("A is Equal to B" )
The following example we have declared Integer type variable a,b and assigning the value for the variable a=10, b=10. Now the condition is true MsgBox(“A is Equal to B”) will be executed and display A is Equal to B.
Example1
Dim a, b As Integer
a=10
b=10
If a = b Then
MsgBox("A is Equal to B" )
End If
The following example we have declared Integer type variable a,b and assigning the value for the variable a=10, b=5. Now the condition is False.nothing will be executed.
Example2
Dim a, b As Integer
a=10
b=5
If a = b Then
MsgBox("A is Equal to B" )
End If
If Then Else Statement
The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false, the ELSE part will be executed.
Syntax:
If <> Then
Block of the statement1
Else
Block of the statement2
End If
If the condition is TRUE block of the statement1 will be executed. If the condition is evaluated FALSE block of the statement2 will be executed.
Example
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim a As String
a = InputBox("Enter text")
If a = "" Then
Me.Close()
Else
MsgBox("Welcome " & a)
End If
End Sub
End Class
If Then Else If Statement
We can test for more than one condition using the if..else if conditional statement. You use the following syntax in an if..else if statement:
Syntax:
If
Block of the statement1
Else If
Block of the statement2
Else If
Block of the statement3
Else
Block of the statement4
End If
Example
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim a, b, c As Integer
a = InputBox("Enter the value for A")
b = InputBox("Enter the value for B")
c = InputBox("Enter the value for C")
If a = b Then
MsgBox("A is Equal to B" )
ElseIf a > b Then
MsgBox("A is greater then B" )
ElseIf a <>
Select Case Statement
Executes one of several groups of statements, depending on the value of an expression. This statement is used to control the program flow the Select Case control structure is slightly different from the If....ElseIf control structure . The difference is that the Select Case control structure basically only make decision on one expression or dimension (for example the examination Grade) while the If ...ElseIf statement control structure may evaluate only one expression, each If....ElseIf statement may also compute entirely different dimensions. Select Case is preferred when there exist many different conditions because using If...Then..ElseIf statements might become disordered.
Syntax:
Select Case expression
Case value1 Block of one or more statements
Case value2 Block of one or more Statements
Case value3 . .
Case Else Block of one or more Statements
End Select
Example 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Grade As String
Grade = InputBox("Enter Grade")
Select Case Grade
Case "A"
Label1.Text = "High Distinction"
Case "B"
Label1.Text = "Credit"
Case "C"
Label1.Text = "Pass"
Case Else
Label1.Text = "Fail"
End Select
End Sub
Example 2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mark As Integer
mark = InputBox("Enter the Marks")
Select Case mark
Case Is >= 85
Label1.Text = "Excellence"
Case Is >= 70
Label1.Text = "Good"
Case Is >= 60
Label1.Text = "Above Average"
Case Is >= 50
Label1.Text = "Average"
Case Else
Label1.Text = "Need to work harder"
End Select
End Sub
Example 3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mark As Integer
mark = InputBox("Enter the Marks")
Select Case mark
Case 0 To 49
Label1.Text = "Need to work harder"
Case 50 To 59
Label1.Text = "Average"
Case 60 To 69
Label1.Text = "Above Average"
Case 70 To 84
Label1.Text = "Good"
Case Else
Label1.Text = "Excellence"
End Select
End Sub
End Class
Looping Statement in VB.NET
The execution of a particular block of statement repetitively. The repeation occurs untill the condition is evaluated to TRUE or FALSE.
The loops is supported by VB.NET are.
For Next Loop
The For Next Loop , execute the loop body to the fixed number of times.
For Countervariable=[starting Value] To [ending Value] [Step]
[loop of the Body]
Next [countervariable]
countervariable : The counter for the loop to repeat the steps.
startingValue : The starting value assign to counter variable .
endingValue : When the counter variable reach end value the Loop will stop .
loop of the Body : The source code between loop body
Lets take a simple real time example , If you want to show a messagebox 5 times and each time you want to see how many times the message box shows.
Dim a,b,i As Integer
1. a=1
2. b = 5
3. For i = a To b
4. MsgBox(“My God”)
5. Next i
Line 1: Loop starts value from 1
Line 2: Loop will end when it reach 5
Line 3: Assign the starting value to” i” and inform to stop when the “I” reach endingValalue(5)
Line 4: Execute the loop of the body
Line 5: Taking next step , if the counter not reach the endingValalue
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim a As Integer
Dim b As Integer
a = 1
b = 5
For i = a To b
MsgBox("Message Box Shows " & var & " Times ")
Next i
End Sub
End Class
When you execute this program , It will show messagebox five time and each time it shows the counter value.
If you want to Exit from FOR Loop even before completing the loop Visual Basic.NET provides a keyword Exit to use within the loop of the body.
Syntax:
For Countervariable=[starting Value] To [ending Value] [Step]
[ loop of the Body ]
Condition
[ Exit For ]
Next [ countervariable ]
Example for EXIT For Loop
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim a As Integer
Dim b As Integer
a = 1
b = 5
For i = a To b
MsgBox("Message Box Shows " & var & " Times ")
If i=3 Then
Exit For
End If
Next i
End Sub
End Class
While ..End While
While .. End While Loop execute the code until it meets the specified condition.
Syntax:
While [condition]
[loop of the body]
End While
Condition : The condition set by the user
Condition : The condition set by the user
1. counter = 1
2. While (counter <= 10)
3. Message
4. counter = counter + 1
5. End While
5. End While
Line 1: Counter start from 1
Line 2: While loop checking the counter if it is less than or equal to 10
Line 3: Each time the Loop execute the message and show
Line 4: Counter increment the value of 1 each time the loop execute
Line 5: End of the While End While Loop body
Example
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim counter As Integer
Counter=1
While(counter<=6) MsgBox(“Now Counter Value is :” & counter) Counter=counter+1 End While End Sub End Class
Do while Loop
Previous Next
The Loop Started with Do While Keyword. While loop to execute a block of code until a condition becomes False.
Syntax:
Do [ While Until ]
Statements
Exit Do
Statements
Loop
The Do While Statement will execute until the condition is False.
The Do Until Statement will execute until the condition is True.
Syntax:
Do
Statements
Exit Do
Statements
Loop [ While Until ]
Example for Do While
Dim a As Integer
a=1
do while a<=6 MsgBox(“ Now A value is” & a) a=a+1 Loop Example for Do Until
Dim a As Integer
a=1
do until a>=6
MsgBox(“ Now A value is” & a)
a=a+1
Loop
The above example Do while Loop will execute over and over until the variable “a” becomes greater than the value 7.
Sometimes, you may want to exit a While loop before the condition is False. We can exit a loop at any time by using the Exit Do Keyword.
Example
Dim a As Integer
a=1
do while a<=6 MsgBox(“ Now A value is” & a) a=a+1 if a=3 then Exit Do End If Loop
Monday, December 13, 2010
Subscribe to:
Posts (Atom)