Is it a good practice to declare variables inside loops in Fortran?
Related C++ question:
Is it a good practice to declare variables inside loops in Fortran?
Related C++ question:
If I am not wrong you cannot, unless you use a block.
In C++ it is common to declare the iteration count inside the for
loop, as in
for (int i = 0; i < 10; i++) {
// ...
}
Fortran differs in this respect, in that you declare the iteration count in the specification section of a program unit, before your executable statements.
(edit) You can however declare the integer type for the iteration count of a do concurrent
(Intel Fortran compiler documentation) loop:
do concurrent(integer :: i = 1:n)
! ...
end do
I’m assuming that for nested loops, you need to repeat the type-spec in the concurrent header part, i.e.
do concurrent (integer :: i = 1:n, integer :: j = 1:k)
! ...
A feature which arrived in Fortran 2018 is the possibility to declare the integer iteration count in an implied-do loop that can be handy when used within array constructors or data statements:
b = [(a(i,i), integer :: i = 1, n)]
This is very useful for initializing arrays in a module scope without “polluting” your module with several integer variables.
For other variables you’d want to use within the body of a loop, the short answer is it depends. If you have a long function or subroutine with many executable statements before your loop, you may want to create a local scope just before (or within) the loop body using a block
construct. This way your specifications are closer to the point of use, and you don’t have to scroll up or down while writing or editing your code.
For derived types it can be one or the other. In some instances, you’ll want to initialize an instance only once; then you should do so outside of the loop. But in some cases, you might want to re-initialize the derived type instance with each loop iteration by declaring it within a block contained in the loop. This applies to derived types with components that are default-initialized.
Older, statically typed languages like Fortran and C benefit from explicit declaration of variables (I’m a bit confused whether implicit typing like back in the old days falls under explicit declaration… in a sense it is). The specification section gives you a place to tell the compiler what type each variable has and how much storage to allocate for it.
Back in the old days it was common to have single-pass compilers which would interleave parsing, analysis, and code generation. Pascal and C, and I believe also Fortran were designed around this limitation. Quoting from the book “Crafting Intepreters”:
At the time, memory was so precious that a compiler might not even be able to hold an entire source file in memory, much less the whole program. This is why Pascal’s grammar requires type declarations to appear first in a block.
(edit: thanks @FortranFan for the correction)
Fortran 2018 revision, not 202X.
The only new feature here is being able to specify the KIND of integer that the loop control variable should have. It always has to be some kind of integer. You’re not really declaring it, as it is a construct-scope variable.