Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uflorida!novavax!twwells!bill From: bill@twwells.uucp (T. William Wells) Newsgroups: comp.misc Subject: Re: The "evil" GOTO (Was: 25 Years of BASIC) Message-ID: <912@twwells.uucp> Date: 11 May 89 00:26:31 GMT References: <1791@ubu.warwick.UUCP> <1436@onion.reading.ac.uk> <1814@ubu.warwick.UUCP> <698@occrsh.ATT.COM> <852@umecs.cs.umu.se> Reply-To: bill@twwells.UUCP (T. William Wells) Organization: None, Ft. Lauderdale Lines: 51 Summary: Expires: Sender: Followup-To: Distribution: Keywords: In article <852@umecs.cs.umu.se> christer@rachel.UUCP (Christer Ericson) writes: : Rubin states the following problem: : "Let X be an N x N matrix of integers. Write a program that will print the : number of the first all-zero row of X, if any." : : Along with this he presents the following program with a goto [also two other : programs w/o gotos that are disgusting to look at]: : : [BTW, I reindented the program a bit] : : for i:=1 to n do begin : for j:=1 to n do : if x[i,j]<>0 then goto reject; : writeln('The first all-zero row is ',i); : break; : reject: : end; Why don't I use Pascal anymore? Because C says it much better. (NB: array origins have been made 0 and no declarations are shown.) for (i = 0; i < N; ++i) { for (j = 0; X[i][j] == 0; ) { if (++j == N) { printf("The first all-zero row is %d\n", i); return; } } } And if one insists on not putting this into its own function (making the return improper): for (i = 0; i < N; ++i) { for (j = 0; X[i][j] == 0; ) { if (++j == N) { printf("The first all-zero row is %d\n", i); break; } } if (j == N) { break; } } One could, of course, turn the first break into a goto, but eliminating the possible cost of the j == n test isn't worth the loss of clarity. --- Bill { uunet | novavax } !twwells!bill