views:

35

answers:

1

Currently, lets say that I have this code:

     While r <> "HI"
        r = RandomStringGenerator(1)
        time = time + 1
        Console.WriteLine(r)
    End While

how can I make it so that i can say basically this:

    While r <> "HI" Or While r <> "BY"
        r = RandomStringGenerator(1)
        time = time + 1
        Console.WriteLine(r)
    End While

I've alreadly tried:

    While r <> "HI" Or r <> "BY"
        r = RandomStringGenerator(1)
        time = time + 1
        Console.WriteLine(r)
    End While

But it still doesn't work! What's up with this?

+2  A: 

The way you've written your conditional it will always be true and the loop will go infinitely.

r <> "HI" Or r <> "BY"

This essentially says

If r is not equal to "Hi" or r is not equal to "By"

This will be true for every single string in existence. It sounds like you want to continue if the string is neither "hi" or "by". If so try the following

While r <> "HI" AndAlso r <> "BY"
    r = RandomStringGenerator(1)
    time = time + 1
    Console.WriteLine(r)
End While

One minor final point is you should prefer OrElse to plain Or and AndAlso to plain And. The former versions are both short circuiting operations while the latter or not.

JaredPar
Thanks!!!!!!!!!!!!!!!!!!!!!!!!