Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 253
Wannabe Avatar asked Mar 14 '11 21:03

Wannabe


People also ask

What is do while loop example?

The do while loop is an exit controlled loop, where even if the test condition is false, the loop body will be executed at least once. An example of such a scenario would be when you want to exit your program depending on the user input.

What is Do and do while loop?

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Do-while vs while loop?

do while loop is similar to while loop with the only difference that it checks for the condition after executing the statements, and therefore is an example of Exit Control Loop.


2 Answers

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. :-)

like image 67
Aasmund Eldhuset Avatar answered Oct 20 '22 01:10

Aasmund Eldhuset


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

like image 30
Femaref Avatar answered Oct 20 '22 00:10

Femaref