Help With Loops

Page 1 of 1

1 Replies - 359 Views - Last Post: 12 November 2014 - 10:40 PM Rate Topic: -----

#1 BeesKnees  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-November 14

Help With Loops

Posted 12 November 2014 - 10:09 PM

Hi, I'm new to the forum. I wrote this program that checks for primes and it ouputs the answer into 1 of two listboxes either primes or not primes. This works good, even for integer 1 it puts into not prime. But I need it to output the next 9 integers in order and sort them into prime/not prime. So I tried the Do Until Loop, but I'm lost at this portion.


I tried

Do Until IntInput = IntInput + 9

Loop

But the program freezes and doesn't work. Please direct me in the right way what am I doing wrong?

Here is my code:



Option Strict On
Option Explicit On


Public Class Primes
    'Close out the program
    Private Sub btnExt_Click(sender As Object, e As EventArgs) Handles btnExt.Click
        Me.Close()
    End Sub

    'Clears all the fields
    Private Sub btnClr_Click(sender As Object, e As EventArgs) Handles btnClr.Click
        txtInput.Clear()
        listPrime.Items.Clear()
        listNotPrime.Items.Clear()
    End Sub

    Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
        If IsNumeric(txtInput.Text) And CInt(txtInput.Text) > 0 Then
            Dim Answer As Integer = CInt(IsPrime())

        Else
            txtInput.Clear()
            MessageBox.Show("Please enter an integer greater than 0")
        End If

    End Sub
    'Function for Prime
    Private Function IsPrime() As Boolean

        Dim t As Boolean 'T is True
        Dim IntInput, Test As Integer

       
        IntInput = CInt(txtInput.Text) 'IntInput is the input in the textbox
        t = True

        'MAKES 1 Not Prime
        If CInt(txtInput.Text) = 1 Then
            t = False
        End If
        Do Until IntInput = IntInput + 9 ' RIGHT HERE IS WHERE I PUT THE DO LOOP
            For Test = 2 To (IntInput - 1) Step 1
                If IntInput Mod Test = 0 Then
                    t = False

                    Exit For

                End If

            Next

            If t Then
                listPrime.Items.Add(IntInput & " is prime")
                Return True
            Else
                listNotPrime.Items.Add(IntInput & " is not prime")
                Return False
            End If
        Loop
    End Function




Is This A Good Question/Topic? 0
  • +

Replies To: Help With Loops

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12226
  • View blog
  • Posts: 45,301
  • Joined: 27-December 08

Re: Help With Loops

Posted 12 November 2014 - 10:40 PM

Please do not duplicate post. You already have an open thread.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1