5 Replies - 336 Views - Last Post: 04 April 2011 - 03:38 PM Rate Topic: -----

#1 JakeDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-April 11

Question on how to code this for Visual Basic

Posted 01 April 2011 - 01:08 PM

View PostJakeDay, on 01 April 2011 - 12:50 PM, said:

I am trying to code a procedure that displays the squares of the even integers from 2 through 12 in a label control. I have to display each square on a separate line in the control using the For...Next statement.


 Private Sub displayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displayButton.Click
        ' calculates the squares of even integers from 2 through 12
        Dim num As Integer = 2
        Dim numSquared As Integer
        Do While num <= 12
            numSquared = num * num
            num = num + 2

            squaresLabel.Text = squaresLabel.Text & Convert.ToString(numSquared) & ControlChars.NewLine
        Loop
    End Sub

End Class


This is the code I figured out but it is using the Do...Loop Statement. This might be ok but if anyone knows how to put it in a For...Next statement please reply to this. Thank you

This post has been edited by JakeDay: 01 April 2011 - 01:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Question on how to code this for Visual Basic

#2 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Question on how to code this for Visual Basic

Posted 01 April 2011 - 01:21 PM

This should work:


      For num As Integer = 2 To 12 Step 2
            Dim numSquared As Integer
            numSquared = num * num
            squaresLabel.Text = squaresLabel.Text & Convert.ToString(numSquared) & ControlChars.NewLine
      Next


This post has been edited by Jack Eagles1: 01 April 2011 - 01:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Question on how to code this for Visual Basic

Posted 01 April 2011 - 01:23 PM

In for loop it looks like this:

 Private Sub displayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displayButton.Click
        ' calculates the squares of even integers from 2 through 12
        
        Dim numSquared,num As Integer
        For  num = 2 To 12
          if num%2 = 0 then
            numSquared = num * num
            squaresLabel.Text = squaresLabel.Text & Convert.ToString(numSquared) & ControlChars.NewLine
          endif
        Next
    End Sub


This post has been edited by smohd: 01 April 2011 - 01:25 PM

Was This Post Helpful? 1
  • +
  • -

#4 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Question on how to code this for Visual Basic

Posted 01 April 2011 - 01:34 PM

Smohd what is the %2 for at the end of that num? They'll just give errors won't they?
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Question on how to code this for Visual Basic

Posted 01 April 2011 - 01:41 PM

OK Im sorry, I mixed things, thanks for reminding me. I was required to say mod
Was This Post Helpful? 1
  • +
  • -

#6 JakeDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-April 11

Re: Question on how to code this for Visual Basic

Posted 04 April 2011 - 03:38 PM

Thanks #5 smohd. I really appreciate it.

This post has been edited by JakeDay: 04 April 2011 - 03:38 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1