Is there an efficient way to run nested for loops, avoiding any of the counters having the same value. Obviously, I could run some if statements, as below, but is there a more efficient way?
for i = 1 to 20:
for j = 1 to 20:
if (i == j):
continue
else:
for k = 1 to 20:
if (i == k) or (j == k):
continue
else:
do something useful with these different numbers
EDIT: The variables are not interchangeable, so [2, 1, 0] is different to [0, 1, 2]. The “do something useful” will be about 6 numerical checks on the numbers, involving adding, squaring, and square rooting them.
Thanks, and sorry about the possibly unusual pseudocode (and the constant editing).
That looks like the most efficient way you can do it. What’s wrong with how you have it?
I’m just going to ignore the part where I can’t think of any reason to ever need to do this…. so if you have a specific case please share? Unless your trying to compare an item to all other items in a list that isn’t itself I guess?
which could be done as
That way you dont repeat comparisons between things you have already compared.
For three nests