I'm wondering if a SELECT CASE statement full of GOTOs isn't the best
way to go. This is effectively what a computed GOTO does anyway,
right? I would hope that good compilers would notice branch-to-branch
and snap them, as well as branch-around-branch and snap that too
(inverting the sense of the first branch).
Granted, it's not as concise as a computed goto, but it seems to model
the intent, better than all the if/then constructs I've seen, and also
seems to leave plenty of room for comments.
Indeed, a possibly oldish compiler on an oldish system
redwing.zko.dec.com> uname -a; f95 -version
OSF1 redwing.zko.dec.com V5.1 1885 alpha
HP Fortran V5.5A-3548
HP Fortran Compiler V5.5A-3548-48D88
manages to turn the subroutine below
redwing.zko.dec.com> f95 -O -g3 baz.f90
into a jump-through-table directly to the targets.
subroutine xxx(index)
integer :: index
select case (index)
case (10) ; goto 110
case (9) ; goto 109
case (8) ; goto 108
case (7) ; goto 107
case (6) ; goto 106
case (5) ; goto 105
case (4) ; goto 104
case (3) ; goto 103
case (2) ; goto 102
case (1) ; goto 101
end select
print *, "oops"
110 print *, "110"
109 print *, "109"
108 print *, "108"
107 print *, "107"
106 print *, "106"
105 print *, "105"
104 print *, "104"
103 print *, "103"
102 print *, "102"
101 print *, "101"
return
end
program baz
read *, i
call xxx(i)
stop
end
|