0

Why does the j index start from 2 not 1 in this double For loop

For[i = 1, i <= 2, i += 1, For[j = 1, j <= 2, j += 1; Print[{i, j}]]]

{1,2}

{1,3}

{2,2}

{2,3}

MMA13
  • 4,664
  • 3
  • 15
  • 21

1 Answers1

4

The syntax for a For loop is:

For[start,test,incr,body]

But you wrote for "j":

For[start,test,incr; body]

note the ";". If you replace ; by , it works:

For[i = 1, i <= 2, i += 1, For[j = 1, j <= 2, j += 1, Print[{i, j}]]]

enter image description here

Daniel Huber
  • 51,463
  • 1
  • 23
  • 57