From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,db9560332371a98b X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.190.104 with SMTP id gp8mr19496644pbc.4.1340123674716; Tue, 19 Jun 2012 09:34:34 -0700 (PDT) MIME-Version: 1.0 Path: l9ni69238pbj.0!nntp.google.com!news1.google.com!news2.google.com!news.glorb.com!news-in-01.newsfeed.easynews.com!easynews!core-easynews-01!easynews.com!en-nntp-15.dc1.easynews.com.POSTED!ron-shepard From: Ron Shepard Newsgroups: comp.lang.fortran,comp.lang.ada Subject: Re: does modern Fortran have membership test similar to this Ada 2012 feature? Organization: little, if any References: User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Message-ID: X-Complaints-To: abuse@easynews.com X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly. Date: Tue, 19 Jun 2012 11:34:31 -0500 X-Received-Bytes: 2839 Date: 2012-06-19T11:34:31-05:00 List-Id: In article , "Nasser M. Abbasi" wrote: > In previous versions of Ada, membership tests allowed one > to see whether a value was in a range or in a subtype, thus > we could write either of > > if D in 1 .. 30 then > > if D in Days_In_Month then > > but we could not write something like > > if D in 1 | 3 | 5 | 6 ..10 then > > This is now rectified and following "in" we can now have one > or more of a value, a range, or a subtype or any > combination separated by vertical bars. Moreover, they do > not have to be static. This is basic functionality that is available in essentially every high level programming language. However, the syntax is different. If D is a scalar, then the above would be done in fortran as if ( (D>=1) .and. D(<=30) ) then If Days_In_Month is an array, then you could do if ( any(D==Days_in_Month) ) then There are also lower-level constructs that could be used, including looping over the elements of Days_In_Month(:) and testing each one individually. Sometimes that is actually the best approach, depending on what you need to do after the test. In other cases, the best approach is something like if ( D==1 .or. D==3 ) then ... elseif ( D==2 ) then ... endif or if the cases really are static you can do things like select case (D) case(1,3,5,6:10) ... case(2) ... end select case The string of if-then-else tests requires effort proportional to the number of tests, so you generally want to order the most common cases first in the sequence. The select case construct can be done by setting up a branch table, so the effort does not depend on the number of tests. In fortran, the select-case construct requires static tests. However, in many situations you can map a set of dynamic comparisons to a fixed set of static cases, in which case you get the advantages of the branch table algorithm for your dynamic case. $.02 -Ron Shepard