Showing posts with label loop. Show all posts
Showing posts with label loop. Show all posts

Sunday, 22 July 2012

do until loop in vb.net

In this tutorial, we cover the third and last loop, the Do Until loop. All of the loops are similar, but the Do until loop will execute a piece of code until a condition is true. When condition is true enter in the and execute the code in do until loop block.

Copy and paste below Code:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim num As Integer = 1

        Do Until num = 5
            MessageBox.Show("The value of num is: " & num)
            num = num + 1
        Loop

    End Sub

End Class

do until loop in vb.net

In this tutorial, we cover the third and last loop, the Do Until loop. All of the loops are similar, but the Do until loop will execute a piece of code until a condition is true. When condition is true enter in the and execute the code in do until loop block.

Code

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim num As Integer = 1

        Do Until num = 5
            MessageBox.Show("The value of num is: " & num)
            num = num + 1
        Loop

    End Sub

End Class

do while loop in vb.net

This tutorial will show you how to use the Do While loop in Visual Basic .NET. The Do While loop will allow you to execute a piece of code while a condition is true, sort of like an If Statement. Do while loop execute when the given condition is true, when condition is true entering the loop and increase the number and again execute

Copy and paste below Code:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim num1 As Integer = 1

        Do While num1 < 10
            MessageBox.Show("The value of num1 is :" & num1)
            num1 = num1 + 1
        Loop

    End Sub

End Class

For loop in vb.net

This tutorial will introduce you to one of the first intermediate parts of the language, the For Loop. This loop will allow you to execute any code a certain amount of times. There are other types of loops which will be covered later on in the series. For loop is used to execute the statement given amount of time (given between two limits like for i=0 to 10) then the statement given in for loop execute 10 times. This is illustrate in below.

Copy and paste below Code:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim i As Integer = 0

        For i = 0 To 10
            MessageBox.Show("The value of i is: " & i)
        Next

    End Sub

End Class

Monday, 18 June 2012

How to use FOR NEXT loop in vb.net


For next is used for programming code to be repeat a task for more than one times or repeat a task till you reach a condition, use loop statements to achieve your desired results.
FOR NEXT Loop, FOR EACH Loop, WHILE Loop and DO WHILE Loop are the commonly used loops
FOR NEXT Loop:
The FOR NEXT Loop , execute the loop body the source code within For Next code block to a fixed number of times.
For  variable  = [start_value] to [End_value] step
‘Body of the loop
Next [variable]
Here
Variable  à The counter for the loop to repeat the steps.
Star_Value à The starting value assign to counter variable .
End_Value : When the counter variable reach end value the Loop will stop .
Body  of the loopà The source code between loop body
 

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim variable As Integer
        Dim start_Val As Integer
        Dim end_Val As Integer
        start_Val = 1
        end_Val = 5
        For variable = start_Val To end_Val
            MsgBox("Message Box Shows " & variable & " Times ")
        Next variable
    End Sub
End Class

When you execute this program, It will show message box five time and each time it shows the counter value.
If you want to Exit from FOR NEXT Loop even before completing the loop Visual Basic.NET provides a keyword Exit to use within the loop body.

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim variable As Integer
        Dim start_Val As Integer
        Dim end_Val As Integer
        start_Val = 1
        end_Val = 5
        For variable = start_Val To end_Val
            MsgBox("Message Box Shows " & variable & " Times ")
            If variable = 3 Then
                Exit For

            End If
        Next variable
    End Sub
End Class



When you execute this program, it will show message box three times and it will exit the for loop