Re:The History, Controversy, and Evolution of the Goto State...
by gjm11 on Monday November 19, 2018 @08:49AM
(#18486349)
An observation just for fun: if you really want to, you can solve the "average of integers, terminated by 99999" problem without needing any of { goto, break, duplicated code }. Here's one way:
int sum = -99999;
int count = -1;
int last = 0;
do {
scanf("%d", &last); // scanf is kinda evil but is what article uses
sum += last;
count += 1;
} while (last != 99999);
Or you can initialize sum and count to 0 and correct them after the loop instead of pre-correcting before as the code above does. This results in code that is a little longer, very marginally slower, and somewhat easier to read.
(There is still a little bit of duplication there: the magic number 99999 appears twice. For this reason I would actually use break, unless required not to like some of the students in the experiment.)
(There is still a little bit of duplication there: the magic number 99999 appears twice. For this reason I would actually use break, unless required not to like some of the students in the experiment.)