Wannabe
Wannabe

Reputation: 503

Do..While...Loop

My study book makes the following statement about the code below:

**"The computer evaluates the loop condition in the Do...Loop statment to determine whether the loop instructions should be processed. In this case, the inputsales <> String.Empty condition compares the contenst of the input sales variable to the String.Empty value. As you know the String.Empty value represents a zero length, or empty, string if the inputsales variable is empty, the loop condition evaluates to True and the computer process the loop instructions. *If on the other hand the inputsales variable is not empty, the loop condition evaluates to false and the computer skips over the loop instructions.

Based on the code I think it is the opposite: ...that while the inputsales value is not empty it should evaluate to true and process the loop and if it is empty it should evaluate to false and skip the loop? Please see below. Thanks so much for the help!

Option Explicit On
Option Strict On

Imports System.Globalization


Public Class SalesForm

    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub

    Private Sub calcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calcButton.Click
        Const prompt As String = "Enter a sales amount. Click cancel to end."
        Const title As String = "Sales Entry"
        Dim inputsales As String
        Dim sales As Decimal
        Dim salesCounter As Integer
        Dim salesaccumulator As Decimal
        Dim salesAverage As Decimal
        Dim isconverted As Boolean

        inputsales = InputBox(prompt, title, "0")

        **Do While inputsales <> String.Empty
            isconverted = Decimal.TryParse(inputsales, NumberStyles.Currency, NumberFormatInfo.CurrentInfo, sales)

            If isconverted = True Then
                salesCounter = salesCounter + 1
                salesaccumulator = salesaccumulator + sales
            Else
                MessageBox.Show("Please re-entere the sales amount.", "sales Express", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            inputsales = InputBox(prompt, title, "0")
        Loop**

        If salesCounter > 0 Then
            salesAverage = salesaccumulator / Convert.ToDecimal(salesCounter)
            averageLabel.Text = salesAverage.ToString("C2")
            Label2.Text = salesCounter.ToString
        Else
            averageLabel.Text = "0"
        End If
    End Sub
End Class

Upvotes: 8

Views: 2413

Answers (2)

Aasmund Eldhuset
Aasmund Eldhuset

Reputation: 37950

You are definitely correct, and the book is wrong (hopefully, the author just reversed the true/false by accident; otherwise, I'd get another book). My suggested correction (with a few additions):

As you know, the String.Empty value represents a zero length, or empty, string. If the inputsales variable is not empty, the loop condition evaluates to True, and the computer processes the loop instructions (and then jumps back to the top of the loop and reevaluates the condition). If, on the other hand, the inputsales variable is empty, the loop condition evaluates to False, and the computer skips over the loop instructions (and continues with the first statement after the loop).

As @xanatos said: Congratulations on catching your first bug in someone else's text. So +1 for the question, and I'd say that this seems promising for your programming career. :-)

Upvotes: 6

Femaref
Femaref

Reputation: 61437

Yes, you are correct. The loop will be executed when inputsales is non-empty. The description is wrong.

Upvotes: 1

Related Questions