或者
continue labelName;
以下代码显示
Java
中使用continue语句的示例:
public class Main {
public static void main(String[] argv) {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0)
continue;
System.out.println(i + " ");
1 2 4 5 7 8 10
continue 在
循环结构
用用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次循环。
注:
注意在 php 中 switch 语句被认为是可以使用 continue 的一种循环结构。
continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。
<?php
while (list ($key, $value) = each($arr)) {
if (!($key % 2)) { // skip odd members
continue;
do_something_odd($value);
$i = 0;
while ($i++ < 5) {
echo "Outer<br />/n";
while (1) {
echo " Middle<br />/n";
while (1) {
echo " Inner<br />/n";
continue 3;
echo "This never gets output.<br />/n";
echo "Neither does this.<br />/n";
省略 continue 后面的
分号
会导致混淆。以下例子示意了不应该这样做。
<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue;
print "$i/n";
希望得到的结果是:
可实际的输出是:
因为
print()
调用的返回值是 int(1),看上去作为了上述可选的数字参数。