开发者

How to add Or to a While Loop in VB?

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:

开发者_如何学Python
    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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜