Java中continue和break
Posted petewell
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java中continue和break相关的知识,希望对你有一定的参考价值。
- 本文中概念,代码均出自《Thinking in Java》
代码示范如下: · for循环中应用:
class Test
public static void print(String s)
System.out.println(s);
public static void main(String args[])
int i = 0;
outer:
for (;true ; )
inner:
for (;i < 10 ; i++)
print("i = " + i);
if (i == 2)
print("continue");
continue;
if (i == 3)
print("break");
i++;
break;
if(i == 7)
print("continue outer");
i++;
continue outer;
if (i == 8)
print("break outer");
break outer;
for (int k = 0; k < 5; k++)
if (k == 3)
print("continue inner");
continue inner;
/* Output:
i = 0
continue inner
i = 1
continue inner
i = 2
continue
i = 3
break
i = 4
continue inner
i = 5
continue inner
i = 6
continue inner
i = 7
continue outer
i = 8
break outer
*/
· while循环中应用
class Test
public static void main(String args[])
int i = 0;
outer:
while (true)
System.out.println("Outer while loop");
while (true)
i++;
System.out.println("i = " + i);
if (i == 1)
System.out.println("continue");
continue;
if (i == 3)
System.out.println("continue outer");
continue outer;
if (i == 5)
System.out.println("break");
break;
if (i == 7)
System.out.println("break outer");
break outer;
/* Output:
Outer while loop
i = 1
continue
i = 2
i = 3
continue outer
Outer while loop
i = 4
i = 5
break
Outer while loop
i = 6
i = 7
break outer
*/
以上是关于Java中continue和break的主要内容,如果未能解决你的问题,请参考以下文章