Home > Archive > Fortran > October 2006 > variable case expressions??
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
variable case expressions??
|
|
| Mark Morss 2006-10-23, 7:01 pm |
| I've been working with Fortran 95 for a short while. My compiler, g95,
does not appear to accept variable case expressions. For example
CASE(1:k) where k is an integer?
And this be right? It would be simple enough to construct IF blocks to
deal with this, but I thought that CASE constructions were supposed to
facilitate this.
| |
| highegg 2006-10-23, 7:01 pm |
|
Mark Morss wrote:
> I've been working with Fortran 95 for a short while. My compiler, g95,
> does not appear to accept variable case expressions. For example
> CASE(1:k) where k is an integer?
>
> And this be right? It would be simple enough to construct IF blocks to
> deal with this, but I thought that CASE constructions were supposed to
> facilitate this.
I think case selectors must be initialization expressions. So, unless k
is a parameter,
g95 is correct.
| |
| Gordon Sande 2006-10-23, 7:01 pm |
| On 2006-10-23 11:32:27 -0300, "highegg" <highegg@gmail.com> said:
>
> Mark Morss wrote:
>
> I think case selectors must be initialization expressions. So, unless k
> is a parameter,
> g95 is correct.
A standard compiler diagnostic is overlapping case values. Which case
should happen then?
| |
| Mark Morss 2006-10-23, 7:01 pm |
|
Gordon Sande wrote:
> On 2006-10-23 11:32:27 -0300, "highegg" <highegg@gmail.com> said:
>
>
> A standard compiler diagnostic is overlapping case values. Which case
> should happen then?
My source (Chapman) says that cases "must be" mutually exclusive,
without saying whether this is a compile-time or a run-time error.
Evidently it is a compile-time error, since Fortran appears to need to
know the case values at compilation time, and will not accept variable
expressions. Thise does appear to limit the utility of the "case"
construction, however.
| |
|
| > My source (Chapman) says that cases "must be" mutually exclusive,
> without saying whether this is a compile-time or a run-time error.
> Evidently it is a compile-time error, since Fortran appears to need to
> know the case values at compilation time, and will not accept variable
> expressions. Thise does appear to limit the utility of the "case"
> construction, however.
SELECT CASE is not supposed to be a direct replacement for the IF construct.
It is best considered to be an efficient/elegant way of selecting an item
from an list of literals or constants. It will also work with strings (at
least it does in my compiler - IVF9.1), but I'm not sure if this is
standards compliant. Is a string literal a scalar item?
The constuct works particularly well with enumerated types. Enumerations are
new to F2003, but some of us with a Pascal backgraound have been using
manually generated enumerated constants for many years. It is a great shame
that proper enumeration support was not introduced in F90 along with the
SELECT CASE construct. In fact even the F2003 enumerations are rather
half-hearted compared with Pascal type safe enumerators - I guess the
standards committee is not particularly enthusiastic about them. This has
been discussed several times before on this board and I don't particularly
want to reignite the debate.
Paul Holden
| |
| Michael Metcalf 2006-10-30, 7:15 pm |
|
"PJH" <abc@hotmail.com> wrote in message
news:12k17phd3pjf53a@corp.supernews.com...
>
>
> SELECT CASE is not supposed to be a direct replacement for the IF
> construct.
Indeed, it stands in its own right, and is a replacement for the computed
GOTO (where the branching code can be generated efficiently at compile
time).
Regards,
Mike Metcalf
|
|
|
|
|