什么时候有条件地发生这个分配?

When in a conditional does this assignment occur?

我正在将一些 Java 代码移植到 Xojo,它没有与 Java 相同的语言结构。

我在端口中有一个错误,我想我已经将它缩小到 Java 代码的这一点:

int maxIndex = 0;
int n = vertices.length; // vertices is an array
double max = dot(vertices[0]), candidateMax; // I'm assuming `candidateMax` is being initialised to 0 here.

if (max < (candidateMax = vector.dot(vertices[1]))) {
  // Search to the right
  do {
    max = candidateMax;
      maxIndex++;
  } while ((maxIndex + 1) < n && max < (candidateMax = vector.dot(vertices[maxIndex + 1])));

} else if ( max < (candidateMax = vector.dot(vertices[n - 1])) ) {
  maxIndex = n;
  // Search to the left
  do {
    max = candidateMax;
     maxIndex--;
  } while (maxIndex > 0 && max <= (candidateMax = vector.dot(vertices[maxIndex - 1])));
}
return maxIndex;

我已将其移植到此代码(Xojo - 比上面的代码更冗长):

Var maxIndex As Integer = 0
Var n As Integer = Vertices.Count
Var max As Double = vector.Dot(Vertices(0))
Var candidateMax As Double

candidateMax = vector.Dot(Vertices(1))
If max < candidateMax Then
  // Search to the right.
  Do
    max = candidateMax
    maxIndex = maxIndex + 1

    // Exit?
    If maxIndex + 1 >= n Then
      Exit
    Else
      candidateMax = vector.Dot(Vertices(maxIndex + 1))
      If max > candidateMax Then Exit
    End If
  Loop
Else
  candidateMax = vector.Dot(Vertices(n - 1))
  If max < candidateMax Then
    maxIndex = n

    // Search to the left.
    Do
      max = candidateMax
      maxIndex = maxIndex - 1

      // Exit?
      If maxIndex <= 0 Then
        Exit
      Else
        candidateMax = vector.Dot(Vertices(maxIndex - 1))
        If max > candidateMax Then Exit
      End If
    Loop
  End If
End If

Return maxIndex

我假设 while 循环条件:

if (max < (candidateMax = vector.dot(this.vertices[1]))) {
  // Search to the right
  do {
    max = candidateMax;
      maxIndex++;
  } while ((maxIndex + 1) < n && max < (candidateMax = vector.dot(vertices[maxIndex + 1])));

翻译为:"Do the contents of the loop at least once. After each iteration of the loop, check to see if maxIndex + 1 is less than n. If it's not then exit the loop. If maxIndex + 1 is greater than n then assign the result of the vector.dot method to candidateMax and check to see if max is less than candidateMax. If it is then keep iterating".

这是正确的吗?我认为我误解了评估 while 条件的顺序。

我相信你弄错了循环退出条件。

原文:

while (maxIndex > 0 && max <= (candidateMax = vector.dot(vertices[maxIndex - 1])))

在 Xojo 中的意思大致是:

  ...
  if maxIndex > 0 then candidateMax = vector.Dot(Vertices(maxIndex - 1))
while (maxIndex > 0) and (max <= candidateMax)

一般来说,您可以将Java/C的do ... while (b)翻译成Xojo的do ... loop until not (b)

这意味着,在您的情况下:

  ...
  if maxIndex > 0 then candidateMax = vector.Dot(Vertices(maxIndex - 1))
loop until not ( (maxIndex > 0) and (max <= candidateMax) )