https://craftofcoding.wordpress.com/2022/02/11/what-fortran-does-better-than-c-like-languages/
Skip to content
The Craft of Coding
Musings on programming
Menu
* Home
* About
* FAQ
* IMAGE PROCESSING
* LANGUAGES
* Learning to Program
* PRACNIQUES (programming case studies)
* PROGRAMMING MATTER
* Recursion
* Usability, Design and Web
* Useful snippets of code
What Fortran does better than C-like languages
11/02/2022
C-like languages (C, C++, Java) can do many things, but over the
decades nothing much has changed with the inadequacies of some of
their control structures. Fortran on the other hand, has evolved.
Here are some things that just make implementing some algorithms
easier. (Yes there are work arounds in C, but they are not as
elegant).
Exiting from a nested loop
In C-like languages exiting from a deeply nested loop isn't exactly
trivial (without the use of goto, so don't even go there). A break
statement in C will only exist the loop the break resides in. If the
break appears inside a nested loop, the break only leaves the loop it
is in. There is no single break statement in C that will break out of
more than one level of nesting. It is possible to add an extra
condition, for example in C:
exitloop = 0
for (i=1; i<=10 && !exitloop; i=i+1)
for (j=1; j<=10 && !exitloop; j=j+1)
for (k=1; k<=10 && !exitloop; k=k+1)
if (x[i][j][k] == 0)
exitloop = 1;
Fortran however allows loops to be named, so exiting nested loops is
as simple as naming the loop to be exited.
loop1: do i = 1, 10
loop2: do j = 1, 10
loop3: do k = 1, 10
if (x(i,j,k) == 0) exit loop1
end do loop3
end do loop2
end do loop1
Ranges in a case statement
C doesn't do ranges in switch... okay so some compilers, like gnu C
offer it as an extension, but it's not part of the spec. Fortran
allows it as part of the standard.
integer :: temp_c
! Fujita Scale for wind velocity (mph)
select case (windS)
case (40:72)
write (*,*) 'F0 : light-weak'
case (73:112)
write (*,*) 'F1 : moderate-weak'
case (113:157)
write (*,*) 'F2 : significant-strong'
case (158:206)
write (*,*) 'F3 : severe-strong'
case (207:260)
write (*,*) 'F4 : devastating-violent'
end select
Array slicing
If you deal with arrays, you know how important it is to have array
slicing in a language - because it makes manipulating arrays super
easy. Why write loops when you don't have to? Here are two array
declarations in Fortran.
integer, dimension(100) :: vec
integer, dimension(1:6,1:6) :: arr2d
Here are some things you can do:
print *, size(vec) ! print the size of the array
print *, vec(40:50) ! print elements 40 to 50
print *, vec(40:50:2)! print elements 40 to 50, step size 2, ie. 40,42,...
add2d = 1 ! set all elements of the array to the value 1
arr2d(3:4,3:4) = 2 ! set elements in row 3 and 4, col 3 and 4 to value 2
write (*,*) arr2d ! print out the whole array (a bit messy)
Arrays in 2D can be messy when printed using the single statement
above. It is possible to use a single loop, and slicing to make
things look nicer. The loop controls the row, and the columns are all
included using the ":" symbol.
do i = 1,size(arr2d,1)
write(*,'(6i4)') arr2d(i,:)
end do
There is no array slicing mechanism in C.
Arrays with any indices you want
Fortran by default indexes arrays at 1. But, you are not restricted
to that, you can specify the index range of any array. Some examples
are shown below:
integer, dimension(100) :: v ! indices-> 1..100
integer, dimension(0..99) :: w ! indices-> 0..99
real, dimension(-5:5) :: z ! indices-> -5,-4,...0,...4,5
real, dimension(3,4) :: a ! indices-> x: 1..3 and y: 1..4
real, dimension(-1:1,4) :: a ! indices-> x: -1,0,1 and y: 1..4
Makes it much easier to bend a language to an algorithm, rather than
the other way around.
No dangling else
Regardless of what people say, dangling else does cause problems in
languages like C. The fact that all Fortran control structures are
terminated puts a stop to that. There is also no need for { and } or
similar to signify the begin and end of a control block.
Few if any dangerous things
Fortran is cool because it is easy to learn, hard to make
catastrophic errors with, and for numerical computation there is
likely no faster language. Sure, it may lack some of the low-level
features of C, but not every language has to be so close to the
system.
Share this:
* LinkedIn
* Pinterest
* Email
* Facebook
* Twitter
*
Like this:
Like Loading...
Related
Posted in: C, coding Fortran, Fortran, programming
Post navigation
Coding Ada: get vs. get_line (ii) - some caveats
Leave a Reply Cancel reply
Enter your comment here...
[ ]
Fill in your details below or click an icon to log in:
*
*
*
*
*
Gravatar
Email (required) (Address never made public)
[ ]
Name (required)
[ ]
Website
[ ]
WordPress.com Logo
You are commenting using your WordPress.com account. ( Log Out /
Change )
Google photo
You are commenting using your Google account. ( Log Out / Change )
Twitter picture
You are commenting using your Twitter account. ( Log Out / Change )
Facebook photo
You are commenting using your Facebook account. ( Log Out / Change )
Cancel
Connecting to %s
[ ] Notify me of new comments via email.
[ ] Notify me of new posts via email.
[Post Comment]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
D[ ]
This site uses Akismet to reduce spam. Learn how your comment data is
processed.
Search for: [ ] [Search]
Recent Posts
* What Fortran does better than C-like languages
* Coding Ada: get vs. get_line (ii) - some caveats
* Coding Ada: Using multiple packages
* Coding Ada: get vs. get_line (i) - the basics
* Ada is defensive by default
Archives
* February 2022
* January 2022
* December 2021
* November 2021
* October 2021
* September 2021
* July 2021
* June 2021
* May 2021
* April 2021
* March 2021
* February 2021
* January 2021
* December 2020
* November 2020
* October 2020
* September 2020
* August 2020
* July 2020
* June 2020
* May 2020
* April 2020
* March 2020
* February 2020
* January 2020
* December 2019
* November 2019
* October 2019
* September 2019
* August 2019
* July 2019
* June 2019
* May 2019
* April 2019
* March 2019
* February 2019
* January 2019
* December 2018
* November 2018
* October 2018
* September 2018
* August 2018
* July 2018
* June 2018
* May 2018
* April 2018
* March 2018
* February 2018
* January 2018
* December 2017
* November 2017
* October 2017
* September 2017
* August 2017
* July 2017
* June 2017
* May 2017
* April 2017
* March 2017
* February 2017
* January 2017
* December 2016
* November 2016
* October 2016
* September 2016
* August 2016
* July 2016
* June 2016
* May 2016
* April 2016
* March 2016
* February 2016
* January 2016
* December 2015
* November 2015
* October 2015
* September 2015
* June 2015
* May 2015
* April 2015
* March 2015
* January 2015
* December 2014
* November 2014
* June 2014
* May 2014
* April 2014
* March 2014
* January 2014
* December 2013
* November 2013
* October 2013
* September 2013
* July 2013
* June 2013
Meta
* Register
* Log in
* Entries feed
* Comments feed
* WordPress.com
Create a free website or blog at WordPress.com.
[Close and accept] Privacy & Cookies: This site uses cookies. By
continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie
Policy
* Follow Following
+ [wpcom-] The Craft of Coding
Join 290 other followers
[ ]
Sign me up
+ Already have a WordPress.com account? Log in now.
*
+ [wpcom-] The Craft of Coding
+ Customize
+ Follow Following
+ Sign up
+ Log in
+ Copy shortlink
+ Report this content
+ View post in Reader
+ Manage subscriptions
+ Collapse this bar
Send to Email Address [ ] Your Name
[ ] Your Email Address [ ]
[ ]
loading [Send Email] Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.
%d bloggers like this:
[b]