LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-22-2011, 08:55 AM   #1
kaiserkarl13
LQ Newbie
 
Registered: Jan 2010
Distribution: Fedora
Posts: 28

Rep: Reputation: 5
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:

Code:
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):

Code:
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, 12:55 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

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):
Code:
exec:     iffable
        | SDO end_spec label opt_comma dospecw
                {
                if($3->labdefined)
                        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:
Code:
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 01:35 PM.
 
1 members found this post helpful.
Old 11-22-2011, 09:41 PM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

In Modern Fortran Explained, Metcalf et al, page 60, 4.4 The do construct:
Quote:
... [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

http://www.amazon.com/Explained-Nume...2019575&sr=8-1
 
1 members found this post helpful.
  


Reply

Tags
control, fortran, variables



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 09:58 AM
[SOLVED] Fortran How to open a file in loop? parekhharsh_j Programming 3 06-29-2011 03:37 PM
Using a variable to control a BASH for loop JohnE1 Programming 15 07-04-2009 03:37 AM
does gcc 4.x accept hardware register variables? Any restrictions? biffle Linux - Software 3 07-25-2008 04:12 PM
sets the environment variables, fortran terrence Linux - Software 3 10-30-2005 02:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:19 AM.

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