Answer to Question #294926 in Visual Basic for JRD

Question #294926

The following code is supposed to take input from the user and print out each entry and add them together with a final print out of the total of all input numbers. The code does exactly that, but I don't get how it prints each input without the writeline being inside the loop. If I move the writeline into the loop, it double prints the input. Any help understanding this would be greatly appreciated. Thanks


Sub Main()

    Dim sum As Integer

    Dim inValue As Integer

    Console.WriteLine("Enter a number: ")


    Do

      inValue = Console.ReadLine()

      If inValue <> -1 Then

        sum += inValue

      End If

    Loop Until inValue = -1


    Console.WriteLine(sum)


    Console.WriteLine("press enter to exit")

    Console.ReadLine()

  End Sub


1
Expert's answer
2022-02-07T15:40:14-0500
Module Module1




    Sub Main()
        Dim sum As Integer
        Dim inValue As Integer
        Do
            Console.Write("Enter a number: ")
            inValue = Console.ReadLine()
            If inValue <> -1 Then
                sum += inValue
            End If
        Loop Until inValue = -1
        Console.WriteLine("Sum: {0}", sum)


        Console.WriteLine("press enter to exit")


        Console.ReadLine()


    End Sub


End Module

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS