原来对continue的理解停留在表面,粗略的以为就是
在执行这条语句之后返回到迭代的头部(自认为是进口处),没想到还有巨大的细节问题。
先举个例子:
public
class
Test
...
{
/** */ /**
* @param args
*/
public static void main(String[] args) ... {
// TODO Auto-generated method stub
int k = 0 ;
do ... {
k ++ ;
if (k < 10 ) ... {
System.out.println( " do-while: " + k);
continue ;
}
} while (k > 10 );
while (k < 10 ) ... {
System.out.println( " while: " + k);
if (k < 10 ) ... {
k ++ ;
continue ;
}
}
for ( int i = 0 ;i < 10 ;i ++ ) ... {
System.out.println( " for: " + i);
if (i < 10 ) ... {
continue ;
}
}
}
}
/** */ /**
* @param args
*/
public static void main(String[] args) ... {
// TODO Auto-generated method stub
int k = 0 ;
do ... {
k ++ ;
if (k < 10 ) ... {
System.out.println( " do-while: " + k);
continue ;
}
} while (k > 10 );
while (k < 10 ) ... {
System.out.println( " while: " + k);
if (k < 10 ) ... {
k ++ ;
continue ;
}
}
for ( int i = 0 ;i < 10 ;i ++ ) ... {
System.out.println( " for: " + i);
if (i < 10 ) ... {
continue ;
}
}
}
}
返回结果:
do-while:1
while:1
while:2
while:3
while:4
while:5
while:6
while:7
while:8
while:9
for:0
for:1
for:2
for:3
for:4
for:5
for:6
for:7
for:8
for:9
可见:
do-while (exc):
do -->...-->continue-(跳过部分代码)->while(exc)-->...
while(exc):
while(exc)-->...-->continue-(直接)->while(exc)-->...
for(a;b;c):
a-->b-->...-->continue-(直接)->c-->b-->...
continue语句只能使用在循环语句内部,功能是跳过该次循环,继续执行下一次循环结构。在while和do-while语句中continue语句 跳转到循环条件处开始继续执行,而在for语句中continue语句跳转到迭代语句处开始继续执行。