Can anyone tell me what this code does.

Private Sub MysteryCode_Click(ByVal sender As _
         System.Object, ByVal e As System.EventArgs) _
         Handles MysteryCode.Click

        ' Determines MYSTERVALUE base on number entered by user
        Dim Y As Integer, X As Integer, MYSTERYVALUE As Integer

        ' initialization phase
        X = 1 'prepare to loop
        MYSTERYVALUE = 0 'set MYSTERVALUE To 0

        ' processing phase
        While X <= 10 ' loop 10 times
            Y = X ^ 2 ' X raised to the power of 2
            MYSTERYVALUE = MYSTERYVALUE + Y ' add MYSTERVALUE to Y
            X = X + 1 ' add 1 to X
        End While

        ' display MYSTERYVALUE 
        Text = "Value is " & MYSTERYVALUE
    End Sub

Recommended Answers

All 2 Replies

looks like it should add the squares of the numbers 1 through 10. Should return 385.

as jeff said your code result is 385.
i add message box to show the result. see the red one.

Private Sub MysteryCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MysteryCode.Click
        
        ' Determines MYSTERVALUE base on number entered by user
        Dim Y As Integer, X As Integer, MYSTERYVALUE As Integer

        ' initialization phase
        X = 1 'prepare to loop
        MYSTERYVALUE = 0 'set MYSTERVALUE To 0

        ' processing phase
        While X <= 10 ' loop 10 times
            Y = X ^ 2 ' X raised to the power of 2
            MYSTERYVALUE = MYSTERYVALUE + Y ' add MYSTERVALUE to Y
            X = X + 1 ' add 1 to X
        End While

        ' display MYSTERYVALUE 
        Text = "Value is " & MYSTERYVALUE
        MsgBox(Text)
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.