100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Array Operations and Slicing

Whole-array arithmetic, array sections, and the WHERE/FORALL constructs that let Fortran express vectorized operations without explicit loops.

Arrays & NumericsIntermediate9 min readJul 10, 2026
Analogies

Whole-Array Expressions

Fortran 90 and later allow arithmetic on entire arrays as single expressions: if A and B are conformable arrays, C = A + B adds them elementwise without an explicit DO loop, and C = A * 2.0 scales every element. This is called array syntax, and it both shortens code and gives the compiler freedom to vectorize or parallelize the operation using SIMD instructions, since the elementwise independence is explicit in the source rather than hidden inside a loop the compiler must first prove is safe to vectorize.

🏏

Cricket analogy: Applying a blanket net-run-rate adjustment to every team's points column in one spreadsheet operation, rather than editing each team's cell individually, is the elementwise C = A + B pattern applied to a whole table at once.

Array Sections (Slicing)

A subscript triplet lower:upper:stride extracts a section of an array without copying loops by hand. For a vector V(1:100), V(10:20) is the section from index 10 to 20, V(1:100:2) takes every other element, and V(20:10:-1) walks backward. On multidimensional arrays, A(2, :) selects the entire second row and A(:, 3) selects the third column, and these sections can appear on either side of an assignment, so A(:, 1) = 0.0 zeroes out an entire column in one statement.

🏏

Cricket analogy: Extracting overs 10 through 20 of an innings from a ball-by-ball log with one slice, rather than looping through every delivery, mirrors V(10:20) pulling a contiguous section from a vector.

Masked Operations with WHERE

The WHERE construct applies an operation only to array elements satisfying a logical mask, without an explicit IF inside a loop: WHERE (temperature < 0.0) temperature = 0.0 clamps every negative element to zero in one statement, and WHERE...ELSEWHERE lets you specify a second operation for elements that fail the mask. This reads declaratively -- describing what condition triggers what transformation -- rather than procedurally stepping through indices, and it composes naturally with whole-array expressions on the right-hand side.

🏏

Cricket analogy: A rule that resets any player's strike rate below zero (due to a scoring error) to zero across the entire scorecard in one pass mirrors WHERE (rate < 0.0) rate = 0.0.

Array-Valued Intrinsics with Sections

Array sections combine naturally with intrinsic functions: SUM(A(:, 2)) totals the second column, MAXVAL(A(1:10)) finds the largest value in the first ten elements, and DOT_PRODUCT(V(1:5), W(1:5)) computes a dot product over a subrange. Because a section is itself treated as an array expression, it can be passed directly to any procedure or intrinsic expecting an array argument, which avoids writing a temporary copy loop just to isolate a subset of data before processing it.

🏏

Cricket analogy: Calling SUM on just the fourth column of a batting-average table computes a batsman's total boundary count directly, without first copying that column into a separate list.

fortran
program array_ops_demo
  implicit none
  real :: a(6), b(6), c(6)
  real :: matrix(4, 4)
  integer :: i

  a = [1.0, -2.0, 3.0, -4.0, 5.0, -6.0]
  b = 10.0

  ! Whole-array arithmetic
  c = a + b
  print *, 'c =', c

  ! WHERE masked assignment: clamp negatives to zero
  where (a < 0.0)
    c = 0.0
  elsewhere
    c = a
  end where
  print *, 'clamped c =', c

  ! Array section: every other element, reversed
  print *, 'strided reverse =', a(5:1:-2)

  ! Build a matrix and slice a column
  do i = 1, 4
    matrix(:, i) = real(i)
  end do
  matrix(:, 1) = 0.0        ! zero out the first column
  print *, 'column sum =', sum(matrix(:, 2))
end program array_ops_demo

Array sections that are not contiguous in memory (for example A(1:10:3) or A(2,:) selecting a row) may be copied into a temporary array when passed to a procedure, which can hurt performance for large arrays inside hot loops. Prefer contiguous sections (whole columns for column-major arrays) when performance matters.

  • Whole-array arithmetic like C = A + B operates elementwise on conformable arrays without an explicit loop.
  • Subscript triplets lower:upper:stride extract array sections, including reversed sections with a negative stride.
  • WHERE applies masked, conditional assignment to array elements declaratively, with ELSEWHERE for the complementary case.
  • Array sections can be passed directly to intrinsic functions like SUM, MAXVAL, and DOT_PRODUCT.
  • A(:, i) selects an entire column and A(i, :) selects an entire row of a 2-D array.
  • Non-contiguous sections may trigger hidden temporary array copies when passed to procedures.
  • Array syntax gives the compiler explicit information for vectorization that a hand-written loop may obscure.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#FortranStudyNotes#ArrayOperationsAndSlicing#Array#Operations#Slicing#Whole#DataStructures#StudyNotes#SkillVeris