In nested For-loops, I want the second For-loop to be evaluated only once in each cyle

 In nested For loops, I want the second For loop to be evaluated only once in each cyle

I want to print

A[0, 0, 1], A[0, 0, 2], A[1, 1, 1], A[1, 1, 2], A[2, 2, 1], A[2, 2, 2] 

using the following code.

For[t = 0, t < 3, t = t + 1, 
  For[z = 0, z < 3, z = z + 1, 
    For[i = 1, i < 3, i++, Print[A[t, z, i]]]]]

3 Answers

Why exactly you need a loop if you don’t want to loop is completely beyond my understanding, but maybe you have processor time to kill or something like that.. Anyway

For[t = 0, t < 3, t++,
 For[z = 0, z < 7, z++,
  For[i = 1, i < 3, i++,
   If[t == z,
    Print[A[t, z, i]
     ]
    ]
   ]
  ]
 ]
(*
A[0,0,1]

A[0,0,2]

A[1,1,1]

A[1,1,2]

A[2,2,1]

A[2,2,2]
*)

Let’s block ads! (Why?)

Recent Questions – Mathematica Stack Exchange