Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-22-2011, 09:55 AM   #1
LQ Newbie
Registered: Jan 2010
Distribution: Fedora
Posts: 23

Rep: Reputation: 4
Fortran do loop variables: restrictions on control variable memberships?

Is there a restriction that the control variable of a DO loop cannot be an element of an array? For example, the following code does not compile on both compilers I've checked:

program loop
  implicit none
  integer :: n(2)
  do n(1) = 1, 10
    do n(2) = 1, 20
    end do
  end do
end program
However, the following code DOES compile (which makes very little sense):

program loop
  implicit none
  integer :: n(2)
  associate ( m=>n(1), p=>n(2) )
  do m = 1, 10
    do p = 1, 20
    end do
  end do
  end associate
end program
Clearly these two loops are precisely the same thing in memory. All ASSOCIATE does is create another name for the same variable. Is there a rule somewhere in the standard that says control variables for DO loops must be declared scalars and not part of a larger object (except, evidently, through association)?
Old 11-22-2011, 01:55 PM   #2
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 705

Rep: Reputation: 425Reputation: 425Reputation: 425Reputation: 425Reputation: 425

I think it is a grammatical restriction. If you take a look at the source code of `f2c' you will find that control variable should be a `name' (excerpt from src/gram.exec):
exec:     iffable
        | SDO end_spec label opt_comma dospecw
                        execerr("no backward DO loops", CNULL);
                $3->blklevel = blklevel+1;
                exdo($3->labelno, NPNULL, $5);
        | SDO end_spec opt_comma dospecw
                exdo((int)(ctls - ctlstack - 2), NPNULL, $4);
                NOEXT("DO without label");
dospec:   name SEQUALS exprlist
                { $$ = mkchain((char *)$1, $3); }
dospecw:  dospec
        | SWHILE {westart(0);} SLPAR expr SRPAR
                { $$ = mkchain(CNULL, (chainp)$4); }
`name' is further defined in gram.head:
name:     SNAME
                { $$ = mkname(token); }
Finally SNAME is a lexer symbol, i.e. a word in your program. So loop counter can only be a variable, at least in f2c.

In theory this restriction could be easily thrown, but for some reasons it is not. Semantically, using an array element as loop counter makes sense and in some languages (in C for example) is allowed.

Last edited by firstfire; 11-22-2011 at 02:35 PM.
1 members found this post helpful.
Old 11-22-2011, 10:41 PM   #3
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 730

Rep: Reputation: 75

In Modern Fortran Explained, Metcalf et al, page 60, 4.4 The do construct:
... [the index] variable is a named scalar integer variable ...
I find the book to be pricey, but worth it if you are going investigate or seriously write to the standards ... cheers, makyo
1 members found this post helpful.


control, fortran, variables

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why Perl's foreach loop can't use an array element as the control variable? password636 Programming 1 07-22-2011 10:58 AM
[SOLVED] Fortran How to open a file in loop? parekhharsh_j Programming 3 06-29-2011 04:37 PM
Using a variable to control a BASH for loop JohnE1 Programming 15 07-04-2009 04:37 AM
does gcc 4.x accept hardware register variables? Any restrictions? biffle Linux - Software 3 07-25-2008 05:12 PM
sets the environment variables, fortran terrence Linux - Software 3 10-30-2005 03:08 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:04 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration