How to use Do While Loop and a For Loop Counter in VB.Net

In this tutorial, we'll be working on a real example on how to use Do...While Loop and a For...Loop Counter in VB.Net.

Example Description:
1. A customer will be asked to enter the prices of 4 items purchased using an input box (create a counter loop).
2. A tax rate of 7% will be a constant.
3. Subtotal, tax rate, and total will be displayed in labels.
4. An accumulator inside the loop will be used to calculate the subtotal.
5. A pre-test post-test do while loop shall be used to control the limit of entering price.


CODE:
Public Class Form1
    Private Sub btnEnterPrices_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterPrices.Click
        Dim Price As String
        Dim SubTotal As Double, Tax As Double, Total As Double
        Dim limit As String, newLimit As String
        Dim i As Integer, n As Integer, response As Integer

        'Prompts the user the input the number of prices to be calculated
        limit = InputBox("Please enter the number of prices to calculate")

        'Check user input for a valid number
        Do While IsNumeric(limit) = False
            limit = InputBox("Please enter the number of prices to calculate")
        Loop

        'Start the counter up to the limit
        For i = 1 To limit
            'Prompts for the price
            Price = InputBox("Please enter Price #" & i & ":", "Price # " & i)

            'Check if the Price entered is valid
            If IsNumeric(Price) = False Then
                'Display an error message until the user enters a valid price
                Do While IsNumeric(Price) = False
                    MsgBox("Please input another a valid Price", vbExclamation, "Invalid Price")
                    Price = InputBox("Please enter Price #" & i & ":", "Price # " & i)
                Loop
            Else
                'Calculate sub-total
                SubTotal = SubTotal + Price
            End If

            If i = limit Then

                'If the limit is reached, 
                'prompt the user whether to enter additional prices to calculate
                response = MsgBox("Do you want to enter additional Price?", vbYesNo)

                'If the user clicked Yes
                If response = vbYes Then

                    'Prompt a new limit 
                    newLimit = InputBox("Please enter number of additional Price to Calculate")

                    'Validate user input
                    Do While IsNumeric(newLimit) = False
                        newLimit = InputBox("Please enter number of additional Price to Calculate")
                    Loop

                    'Start a new counter for the new limit
                    For n = 1 To newLimit

                        'Prompt for the Price
                        Price = InputBox("Please enter Price #" & i + n & ":", "Price # " & i + n)

                        'Validate User Input
                        If IsNumeric(Price) = False Then

                            'Display a message while the price is not a valid number
                            Do While IsNumeric(Price) = False
                                MsgBox("Please input another a valid Price", vbExclamation, "Invalid Price")
                                Price = InputBox("Please enter Price #" & i + n & ":", "Price # " & i + n)
                            Loop
                        Else
                            'Calculate Subtotal
                            SubTotal = SubTotal + Price
                        End If
                    Next n
                End If
            End If
        Next i

        'Calculate Tax and the Total Price
        Tax = Format(SubTotal * 0.07, "###.##")
        Total = Format(SubTotal + Tax, "###.##")

        'Display results inside labels
        lblSubTotal.Text = "$" & SubTotal
        lblTax.Text = "$" & Tax
        lblTotal.Text = "$" & Total

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class

Comments

Popular posts from this blog

How to Create a Configuration.INI Files in VB6

How to Set Windows Form Always on Top of Other Applications in VB6

How to Minimize/Send an App Icon to System Tray in Visual Basic 6