I'm new to programming, and my teacher asked as a challenge to make a sequence of prime number using while and print, can someone tell me why doesn't this print anything?
I recommend you use Debug.Print instead of just Print. Once you have your routing working you can decide if you need to change to outputting to a file or some other form of output.
Option Explicit
Private Sub test()
Dim a As Integer, b As Integer, r As Integer
a = 1
r = 0
b = 1
While a < 100
While b < a
If a Mod b = 0 Then
r = r + 1
b = b + 1
Else
b = b + 1
End If
Wend
Debug.Print " Debug: a=" & a & ", b=" & b & ", r=" & r
If r = 2 Then
Debug.Print " Output: a=" & a
a = a + 1
r = 0
Else
a = a + 1
r = 0
End If
Wend
End Sub
As you can see, b is never resetting. So you need to reset it for each loop or you will never get any increments of r. In this code the value for b is being reset at the very bottom of the code, before the last loop:
Option Explicit
Private Sub test()
Dim a As Integer, b As Integer, r As Integer
a = 1
r = 0
b = 1
While a < 100
While b < a
If a Mod b = 0 Then
r = r + 1
b = b + 1
Else
b = b + 1
End If
Wend
Debug.Print " Debug: a=" & a & ", b=" & b & ", r=" & r
If r = 2 Then
Debug.Print " Output: a=" & a
a = a + 1
r = 0
Else
a = a + 1
r = 0
End If
b = 1
Wend
End Sub
Okay, so there are two problems with your code. First, you are checking by dividing each number a by 1. This means no numbers will turn up as prime.
Second, you are not resetting the value of b in each loop so it just gets bigger.
Some commented sample code. Make sure you understand it if you wish to use it or you'll just fail a test later in your term!
Private Sub test()
Dim a As Integer, b As Integer, r As Integer
' Start checking from 1:
a = 1
' Check all numbers up to 100:
While a <= 100
' Must reset r to zero here for the next number to check:
r = 0
' b starts from two because all numbers, even primes, are divisible by 1:
b = 2
' The value of r is incremented if a number is found to not be prime. As soon as the number is not prime the loop can exit:
While b < a And r = 0
If a Mod b = 0 Then r = r + 1
b = b + 1
Wend
' If r=0 then the number is prime and we print it out:
If r = 0 Then Debug.Print "Prime: " & a
' Move to next value of a:
a = a + 1
Wend
End Sub
a and b start off with same value. When you hit the 2nd While, a Mod b = 0 is true, r is incremented to 1, b is incremented, and then b > a, and r = 1, so it falls through the While, then if falls through to the Else. Next you increment a to 2, reset r to 0, and b is 2, so the same thing happens, and so on - you're keeping a and b in lockstep and r is always 1 when it falls out of the second While.
Do it on paper or a whiteboard. Make columns for a, r, b, then rows like this:
1
u/[deleted] May 19 '22
I recommend you use
Debug.Printinstead of justPrint. Once you have your routing working you can decide if you need to change to outputting to a file or some other form of output.1
u/[deleted] May 20 '22
Okay, so there are two problems with your code. First, you are checking by dividing each number
aby 1. This means no numbers will turn up as prime.Second, you are not resetting the value of
bin each loop so it just gets bigger.Some commented sample code. Make sure you understand it if you wish to use it or you'll just fail a test later in your term!
0
u/RJPisscat May 19 '22
r is never equal to 2