For Programmers: Free Programming Magazines  


Home > Archive > Fortran > December 2005 > nesting









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 nesting
Al

2005-12-12, 7:16 pm

I'm trying to read some old fortran code.
I'm wondering about how to read then if/then/else/endif nesting.
In particular, the 'else if (c) then' below
(obviously this is not compilable code, i've taken most everything out to
distill to the question at hand.)

if (a) then
if (b) then
endif
else if (c) then
if (d) then
endif
else
if (e) then
if (f) then
endif
else
endif
endif

If i were to write this in C++/Java style, I would intrepret it like as
follows. Is this correct?

if (a)
{
if (b)
{
}
}
else if (c)
{
if (d)
{
}
}
else
{
if (e)
{
if (f)
{
}
}
else
{
}
}

Richard E Maine

2005-12-12, 7:16 pm

Al <allelopath@hotmail.com> wrote:

> I'm trying to read some old fortran code.
> I'm wondering about how to read then if/then/else/endif nesting.
> In particular, the 'else if (c) then' below
> (obviously this is not compilable code, i've taken most everything out to
> distill to the question at hand.)
>
> if (a) then
> if (b) then
> endif
> else if (c) then
> if (d) then
> endif
> else
> if (e) then
> if (f) then
> endif
> else
> endif
> endif
>
> If i were to write this in C++/Java style, I would intrepret it like as
> follows. Is this correct?


I'll pass on the C stuff because that's exactly one of the kinds of
thing I get wrong in C. :-( I'd be much more likely to ask the reverse
question of whether I'm correctly interpreting the C code as being
equivalent to the Fortran. So I'll stick to English, with smatterings of
Fortran.

The indentation that you used above correctly shows the nesting. Or, to
explain it in other terms, and endif always matches with the
lowest-level "if" that is currently open. Also, once you hit an endif,
that completely finishes the "if" that it matched; any "else" or elseif
matched with that "if" needs to be before the endif.

Thus the first endif above matches with the "if (b) then" and closes out
that level. The "elseif (c)" that you asked about matches with the "if
(a)".

Oh, I just remembered one important thing that might not be obvious to
you. There is a difference between

else
if (something) then

versus

else if (something) then

With the "else" and the "if" on separate lines (well, technically
separate statements - you could put them on the same line with a ";",
though I'd recommend against that), the "if" increases the nesting
level; it is nested inside of the "else" block.

With the "else if" on one line (and it doesn't matter whether or not
there are blanks between the "else" and "if"), the nesting level does
not increase. This is a way of constructiing something like a "case"
construct, but where the tests for each block are separate.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
Al

2005-12-12, 7:16 pm

so long story short, i think i've got it right. thanks.

i see from your words that there is "elseif", in addition to "else if"

It would have been clearer to me to do:
elseif (c) then
instead of
else if (c) then

it somehow connotes that another endif is not needed
(but another 'else' would be good to catch all cases)

Richard E Maine wrote:

> Al <allelopath@hotmail.com> wrote:
>
>
> I'll pass on the C stuff because that's exactly one of the kinds of
> thing I get wrong in C. :-( I'd be much more likely to ask the reverse
> question of whether I'm correctly interpreting the C code as being
> equivalent to the Fortran. So I'll stick to English, with smatterings of
> Fortran.
>
> The indentation that you used above correctly shows the nesting. Or, to
> explain it in other terms, and endif always matches with the
> lowest-level "if" that is currently open. Also, once you hit an endif,
> that completely finishes the "if" that it matched; any "else" or elseif
> matched with that "if" needs to be before the endif.
>
> Thus the first endif above matches with the "if (b) then" and closes out
> that level. The "elseif (c)" that you asked about matches with the "if
> (a)".
>
> Oh, I just remembered one important thing that might not be obvious to
> you. There is a difference between
>
> else
> if (something) then
>
> versus
>
> else if (something) then
>
> With the "else" and the "if" on separate lines (well, technically
> separate statements - you could put them on the same line with a ";",
> though I'd recommend against that), the "if" increases the nesting
> level; it is nested inside of the "else" block.
>
> With the "else if" on one line (and it doesn't matter whether or not
> there are blanks between the "else" and "if"), the nesting level does
> not increase. This is a way of constructiing something like a "case"
> construct, but where the tests for each block are separate.
>


Richard E Maine

2005-12-12, 7:16 pm

Al <allelopath@hotmail.com> wrote:

> i see from your words that there is "elseif", in addition to "else if"


Well, no, those are just two spellings for the same thing. Basically,
the blanks are optional and have no effect on the meaning. What is
different is that "else if", with or without the blank, is not the sam
ething as an "else" statement (on one line) followed by an "if"
statement on the next.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
Dick Hendrickson

2005-12-12, 7:16 pm



Al wrote:
> so long story short, i think i've got it right. thanks.
>
> i see from your words that there is "elseif", in addition to "else if"


Yes and no, spaces normally are not significant in
Fortran. So, "else if" and "elseif" mean the same
thing. True, in what's called "free source form"
spaces are significant, but there is a small list
of keywords that allow a blank in a natural place.
Things like "else&if", "go&to", etc., where the
& represents an optional space.

As a matter of style, especially if you're not sure
you understand everything completely ;), it's a
good idea to end every if - elseif block with
else
print *, "we shouldn't get here"
stop
endif
that way you can be sure that various if tests actually
covered everything.

I once worked on a large program where one of the
programmers was really concerned with efficiency. If
a variable could only have one of 3 possible values,
he'd code it as
if ( x = 1) then
blah blah blah
elseif ( x = 2) then
blah blah blah
else
blah blah blah
endif

Sure as anything, a year or two later someone would add
a fourth possible value for the variable. It was my
introduction to debugging ;(

Dick Hendrickson
>
> It would have been clearer to me to do:
> elseif (c) then
> instead of
> else if (c) then
>
> it somehow connotes that another endif is not needed
> (but another 'else' would be good to catch all cases)
>
> Richard E Maine wrote:
>
>
>
>


James Giles

2005-12-12, 7:16 pm

Al wrote:
> so long story short, i think i've got it right. thanks.
>
> i see from your words that there is "elseif", in addition to "else if"


They aren't actually different. In traditional fortran,
spaces had no meaning outside of character strings
(and hollerith, but you don't want to learn about
those). So, ELSE IF, ELSEIF, E L S E I F, and so on
all meant the same thing. Now, in modern Fortran
using free form syntax, spaces are significant as
delimiters between tokens, but the space between
the ELSE and the IF is optionally still allowed.

> It would have been clearer to me to do:
> elseif (c) then
> instead of
> else if (c) then
>
> it somehow connotes that another endif is not needed
> (but another 'else' would be good to catch all cases)


Yes, this makes more sense than to have each alternative
begin a whole new IF construct. That would require a
raft of ENDIF statements at the end of a sequence of
alternatives instead of just one.

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare


Al

2005-12-13, 7:04 pm

thanks for the fortran lesson, y'all
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com