1. Home
  2. Computing & Technology
  3. Visual Basic

Why GoTo is responsible for the bug

By Dan Mabbutt, About.com

The intent of this program is to insert numbers into an array (arrNumber) in random sequence so that no number is duplicated. It all looks logical enough. (Well ... It does to me. But then my mind was warped at an early age by too much Cobol programming.) In words, here's what it does.

First, a loop to fill in 9 elements of the array is started. A postive integer (intNumber) is randomly generated. Another loop to see if any of the elements match the newly generated integer is started. On the first match, both loops are exited (using the GoTo statement) and the whole process starts over to see if another randomly generated number can be added to the array. So it is logical to assume that once both loops finish, the array is filled and no number matches any other in it. Simple, Yes?

No.

Before I go any further, Graham's many friends and loved ones should be reassured that he didn't think up this horrible code himself. It was actually an example in a VB tutorial he found on a web page. I won't tell you which one (certainly nothing I had anything to do with) out of professional courtesy.

As it turns out, the code works the first time. It also works for N (see the random number generation statement) greater than or equal to 10. But it goes into a hard, infinite loop when N is less than 9.

Why? Before you read the next paragraph, why not try to figure it out for yourself? Chewing on problems like this is one of the best ways to understand programming.

----------------------------

Finished? OK ... here's what I wrote to Graham.

On the second invocation of the code, the array values are still present in the array. The test for a match always works and executes the GoTo, which restarts the For Next loop which gets a match which restarts the For Next loop which gets a match which restarts the For Next loop which gets a match which restarts the For Next loop which gets a match which restarts the For Next loop which gets a match which ...

Well, -- I trust you see what I mean.

Could this code have been written correctly and still use a GoTo? Of course! Like I said, Cobol and Fortran programmers did it for years and years. But I maintain that the real culprit is the GoTo. My buddy Edsger Dijkstra and I maintain that this makes it too hard to keep the actual conditions present when the loop is restarted in mind. The fact that this program only crashes when using specific values for N and only on the second invocation are key things to think about.

For another of Graham's totally interesting bugs, check out this next article about Disappearing Label Text.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Opinions and Humor
  5. OOP and GoTo

©2009 About.com, a part of The New York Times Company.

All rights reserved.