10 Replies - 1075 Views - Last Post: 19 October 2013 - 11:21 AM Rate Topic: -----

#1 littleone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-October 13

While loop even odd code issue

Posted 18 October 2013 - 03:39 PM

I have been working on this problem for days. I am trying to create a Do while loop that will separate even and odd number into 2 separate list boxes from 2 user input boxes ( upper(Limit1) and lower(Limit2) limit). I have it working but it will not except all the number that I input like upper 500 lower 200. I know that I has to do with the equality but I cannot figure out what to put in so that it will except any value that is entered. Here is what I have any suggestions would be appreciated.

Dim I As Integer
Dim txtBoxLimit1String As Double
Dim txtBoxLimit2string As String

lstBoxEvens.Items.Clear()
lstBoxOdds.Items.Clear()

txtBoxLimit1String = txtBoxLimit1.Text 'txtBoxLimit1 user input
txtBoxLimit1String = CType(txtBoxLimit1.Text, Integer)


txtBoxLimit2string = txtBoxLimit2.Text 'txtBoxLimit2 user input
txtBoxLimit2string = CType(txtBoxLimit2.Text, Integer)



' Do...While Pre-test
I = txtBoxLimit1String
Do While I <= txtBoxLimit2string


If I Mod 2 <> 0 Then
lstBoxEvens.Items.Add(I)
Else

lstBoxOdds.Items.Add(I)

End If
I += 1
Loop


End Sub

Is This A Good Question/Topic? 0
  • +

Replies To: While loop even odd code issue

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3809
  • View blog
  • Posts: 13,520
  • Joined: 12-December 12

Re: While loop even odd code issue

Posted 18 October 2013 - 03:48 PM

That code works for me. Is it the exact code you are using, and do you receive any error messages?
Was This Post Helpful? 0
  • +
  • -

#3 littleone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-October 13

Re: While loop even odd code issue

Posted 18 October 2013 - 03:59 PM

Yes that is the exact code I am using. I do not get an error message, if I use input like Upper limit 100 and lower limit 200 I get the correct output in both list boxes but if I use Upper limit 500 and lower limit 200 I do not get any out put at all. No error message just not out put I assume it has to do with this line "Do While I <= txtBoxLimit2string" but I cannot find any other way to code it that it will continue to work so that it will take any input from both box and function.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3809
  • View blog
  • Posts: 13,520
  • Joined: 12-December 12

Re: While loop even odd code issue

Posted 18 October 2013 - 04:10 PM

Quote

if I use input like Upper limit 100 and lower limit 200 I get the correct output in both list boxes but if I use Upper limit 500 and lower limit 200

Logically the upper limit should be bigger than the lower-limit. If they are not then, no, you won't see any output, because your while-loop will never be entered. You've got your boxes the wrong way round, as the quote confirms.

This post has been edited by andrewsw: 18 October 2013 - 04:13 PM

Was This Post Helpful? 0
  • +
  • -

#5 littleone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-October 13

Re: While loop even odd code issue

Posted 18 October 2013 - 04:26 PM

Andrew my problem is that this is a homework assignment. What I have to work with is an exe. demo that I have to duplicate. There are several section to this assignment but this part is to create a while loop that display even and odds in 2 list box from 2 user input boxes and on a test on the demo it can do 500-200, 100-200. The code above it the code that I developed from what I assumed would work for the demo and it works with the exception of the 500-200 and I cannot figure out any way to code the loop that would make it work. I truly do appreciate your assistance on this issue.
Was This Post Helpful? 0
  • +
  • -

#6 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: While loop even odd code issue

Posted 18 October 2013 - 05:09 PM

if that's what you really want to do. then you will have to check first which 1 of the 2 textboxes is greater than the other by adding if else statement into your code.
you do something like this.
I = txtBoxLimit1String

'check which 1 of the 2 values from textboxes is smaller that the other 1
        If I <= txtBoxLimit2string Then
            Do While I <= txtBoxLimit2string
                If I Mod 2 <> 0 Then
                    lstBoxEvens.Items.Add(I)
                Else

                    lstBoxOdds.Items.Add(I)

                End If
                I += 1
            Loop
        Else
            'here txtBoxLimit2string is the min value so we will have to loop it this way until it reaches the equality to the max value
            Do While txtBoxLimit2string <= I
                If txtBoxLimit2string Mod 2 <> 0 Then
                    lstBoxEvens.Items.Add(txtBoxLimit2string)
                Else

                    lstBoxOdds.Items.Add(txtBoxLimit2string)

                End If
                'increment the min value
                txtBoxLimit2string += 1
            Loop
        End If


but you can no longer say that textbox1 is the max value and textbox2 is the min value.
this way we will have to let the program first check which one of the two control values is the min and max.
Hope that helped.
Happy coding.
Was This Post Helpful? 0
  • +
  • -

#7 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: While loop even odd code issue

Posted 18 October 2013 - 05:16 PM

you might also want to filter you textboxes to only accept numerical values by adding this code to the key_press event in your textboxes
    'check if the user pressed [backspace]
    If Asc(e.KeyChar) <> 8 Then
        If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
            e.Handled = True
        End If
    End If


this will avoid putting invalid values into your textboxes.

This post has been edited by jimzcoder: 18 October 2013 - 05:19 PM

Was This Post Helpful? 0
  • +
  • -

#8 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: While loop even odd code issue

Posted 18 October 2013 - 05:43 PM

you can also do it without using else if statement by doing like this
Dim txtBoxLimit1String As Double
        Dim txtBoxLimit2string As String

        lstBoxEvens.Items.Clear()
        lstBoxOdds.Items.Clear()

        txtBoxLimit1String = txtBoxLimit1.Text 'txtBoxLimit1 user input
        txtBoxLimit1String = CType(txtBoxLimit1.Text, Integer)


        txtBoxLimit2string = txtBoxLimit2.Text 'txtBoxLimit2 user input
        txtBoxLimit2string = CType(txtBoxLimit2.Text, Integer)



        ' Do...While Pre-test
        Dim minVal, maxVal As Integer
        'Here we will have to get the maximum and minimum value to be used in the loop
        minVal = Math.Min(CType(txtBoxLimit1String, Decimal), CType(txtBoxLimit2string, Decimal))
        maxVal = Math.Max(CType(txtBoxLimit1String, Decimal), CType(txtBoxLimit2string, Decimal))
        Do While minVal <= maxVal
            If minVal Mod 2 <> 0 Then
                lstBoxEvens.Items.Add(minVal)
            Else

                lstBoxOdds.Items.Add(minVal)

            End If
            'increment the min vlaue
            minVal += 1
        Loop


Was This Post Helpful? 0
  • +
  • -

#9 littleone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-October 13

Re: While loop even odd code issue

Posted 18 October 2013 - 05:51 PM

That worked, thank you very very much.
Was This Post Helpful? 0
  • +
  • -

#10 littleone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-October 13

Re: While loop even odd code issue

Posted 19 October 2013 - 11:05 AM

This a homework assignment. What I have to work with is an exe. demo that I have to duplicate. There are several section to this assignment but this part is to create a do until loop that display even and odds in 2 list box from 2 user input boxes and on a test on the demo it can do 200-500. The code here that I developed is what I assumed would work for the demo and it works with the exception of the 500-200 and I cannot figure out any way to code the loop that would make it work. I truly do appreciate your assistance on this issue.

        Dim I As Integer
        Dim txtBoxLimit1String As String
        Dim txtBoxLimit2string As String

        lstBoxEvens.Items.Clear()
        lstBoxOdds.Items.Clear()

        txtBoxLimit1String = txtBoxLimit1.Text 'txtBoxLimit1 user input
        txtBoxLimit1String = CType(txtBoxLimit1.Text, Integer)


        txtBoxLimit2string = txtBoxLimit2.Text 'txtBoxLimit2 user input
        txtBoxLimit2string = CType(txtBoxLimit2.Text, Integer)



        ' Do...Until Pre-test
        I = txtBoxLimit1String
        If I >= txtBoxLimit2string Then

            Do Until I > txtBoxLimit2string
                If I Mod 2 = 0 Then
                    lstBoxEvens.Items.Add(I)
                Else
                    lstBoxOdds.Items.Add(I)

                End If
                I -= 1
            Loop
        Else
            Do Until txtBoxLimit2string <= I

                If txtBoxLimit2string Mod 2 = 0 Then
                    lstBoxEvens.Items.Add(txtBoxLimit2string)
                Else
                    lstBoxOdds.Items.Add(txtBoxLimit2string)

                End If
                txtBoxLimit2string -= 1
            Loop


        End If

This post has been edited by andrewsw: 19 October 2013 - 11:18 AM
Reason for edit:: Use code tags

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3809
  • View blog
  • Posts: 13,520
  • Joined: 12-December 12

Re: While loop even odd code issue

Posted 19 October 2013 - 11:21 AM

Topics merged.

@OP Please do not start a new topic for the same question, and please use code-tags.

This post has been edited by andrewsw: 19 October 2013 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1