In this code I am looping over all indices in a 3D domain, and printing the "diagonal" part as
for (i, j, k) in {0..9, 0..9, 0..9}
{
if i == j == k // (1)
//if (i == j) && (j == k) // (2) -> gives expected result
{
writeln((i, j, k));
}
}
My expected result is something like
(0, 0, 0)
(1, 1, 1)
(2, 2, 2)
(3, 3, 3)
(4, 4, 4)
(5, 5, 5)
(6, 6, 6)
(7, 7, 7)
(8, 8, 8)
(9, 9, 9)
which is obtained with Line (2) above. But if I use Line (1), it gives an unexpected result like
(0, 0, 1)
(0, 1, 0)
(0, 2, 0)
...
(9, 7, 0)
(9, 8, 0)
(9, 9, 1)
So I am wondering if I am erroneously using i == j == k
?
(FYI, the above code is motivated by some Python code like
for i in range(10):
for j in range(10):
for k in range(10):
if i == j == k:
print( i, j, k )
which gives (0, 0, 0), (1, 1, 1), ...)
i == j == k
will be the same as either(i == j) == k
ori == (j == k)
? In other words, you compare the bool result of one comparison with a non-bool value. Different programming languages do different things. Always read the documentation and learn the language basics, especially when it comes to things like precedence and associativity of operators.