for循环语句在for (init; condition; post). 这些部分用分号分隔。init 部分指定一个初始语句,条件是确定循环体是否被执行的条件,而 post 指定一个后循环语句。
你可以用while循环做同样的事情,除了它实际上是几个语句而不是单个语句。然而,while循环并不完全像for循环,因为continue语句及其行为方式是一个问题。稍后再谈。
一个提示是for语句的各个部分由分号分隔,分号也用于分隔 Java 中的语句。
考虑以下for循环源示例。
int i;
for (i = 0; i < 5; i++) {
  // for loop body
}
因此,您将在循环语句之前有一个 init 语句,i = 0然后是包含条件的循环语句本身,i < 5,并且作为闭花括号之前循环中的最后一行,您将放置循环后条件,i++.
由于评估条件的时间,这do while有点复杂。while在for循环和while循环中,都会评估条件,如果表达式不为真,则根本不执行循环体,它会被跳过。在for循环的情况下,执行 init 语句,然后评估条件以确定是否for应该执行循环体。由于while循环没有 init 语句作为语句的一部分,while因此评估条件,如果不为真,while则不执行循环体。
循环有一个do while条件,直到第一次通过do while循环体之后才会评估。所以do while循环中的语句总是至少执行一次。然后do while评估条件,如果为真,则执行返回到dois 和do while循环体再次执行的顶部。
一些循环变体的一些代码,其中 initi = 0和条件在哪里,i < 5post在哪里i++。在所有情况下,我都在i循环体之外定义了变量。在for循环的情况下,i在for语句中定义变量会导致变量的范围,即i它的可见性,被限制在for循环体中,而其他类型的循环不会出现这种情况。
int i;
for (i = 0; i < 5; i++) {
  // for loop body
}
int i = 0;
while (i < 5) {
  // while loop body
  i++;
}
int i = 0;
do {
  // do while loop body
  i++;
} while (i < 5);
我提到continue在比较这些形式的循环时,执行语句时发生的事情会有所不同。考虑它的方法是,当continue执行语句时,会跳转到包含该语句的循环语句的右大括号continue。所以这引入了一些需要考虑的事情。
看看上面的例子,但有一个continue声明。在下面的所有示例中,当变量值为 3时,有一条continue语句会导致执行跳到循环体的末尾。i
有了这个改变,for循环将继续增加变量i,因为它是 post,语句的第三部分,for在循环结束时执行。然而,对于while循环和do while循环,变量的递增i是循环体的一部分,因此当continue执行跳到循环结束时,变量的递增i也被跳过。
int i;
// first time init variable i to zero then evaluate condition
for (i = 0; i < 5; i++) {  //  evaluate condition and execute loop body if true
  // for loop body
  if (i == 3)
      continue;  // when i == 3 continue is executed to skip to end of loop
}  // at point of braces, post executed, condition evaluated
int i = 0;
while (i < 5) {     // evaluate condition and execute loop body if true
  // while loop body
  if (i == 3)
      continue;  // when i == 3 continue is executed to skip to end of loop
  i++;      // variable i only incremented when this statement is executed
}  // braces indicate end of loop so jump back to top of loop
int i = 0;
do {
  // do while loop body
  if (i == 3)
      continue;  // when i == 3 continue is executed to skip to end of loop
  // more statements which may be skipped by the continue
  i++;      // variable i only incremented when this statement is executed
} while (i < 5);  // evaluate condition and jump to top of loop if true
您可以对do while循环while条件进行更改,以通过在变量上使用预增量运算符 operator将变量的增量移动i到条件评估中,如下所示。我们使用预递增运算符是因为我们想在检查变量值之前递增变量。while++i
int i = -1;    // need to start at -1 since the while will increment at beginning of the loop
while (++i < 5) { // increment variable i, evaluate condition and body of loop if true
  // while loop body
  if (i == 3)
      continue;  // when i == 3 continue is executed to skip to end of loop
}  // braces indicate end of loop so jump back to top of loop
int i = 0;
do {
  // do while loop body
  if (i == 3)
      continue;  // when i == 3 continue is executed to skip to end of loop
  // more statements which may be skipped by the continue
} while (++i < 5);  // increment variable i, evaluate condition and jump to top of loop if true