Skip to content

Nested Loops

A nested loop has its own init, so its first row is set again on every round of the outer loop.

Inside the nested loop, the outer state carries one more prime: r' is the current value of r, and count'' is the next value of count'.

def border_cells(size: num) -> num:
init: r = 0; count = 0
while r < size:
pragma acc_prime: sequential
init: c = 0
while c < size:
on_edge = r' == 0 or r' == size - 1 or c == 0 or c == size - 1
count'' = count' + 1 if on_edge else count'
c' = c + 1
r' = r + 1
return count
print(border_cells(5))
Run in playground

The outer loop uses sequential mode so the inner loop reads r' before the later assignment advances it.

Output
16