本文概要
语句在循环控制结构使用时你需要立即跳转到循环的下一次迭代的继续。它可以与循环或while循环中使用。
Java的continue语句用来继续循环。它继续程序的电流流动,并跳过在指定的条件中的其余代码。在内环的情况下,它会继续仅内部循环。
我们可以使用Java继续在所有类型的循环如for循环语句,while循环和do-while循环。
句法:
jump-statement;
continue;
资源下载continue语句例
例:
//Java Program to demonstrate the use of continue statement
//inside the for loop.
public class ContinueExample {
public static void main(String[] args) {
//for loop
for(int i=1;i<=10;i++){
if(i==5){
//using continue statement
continue;//it will skip the rest statement
}
System.out.println(i);
}
}
}
输出:
1
2
3
4
6
7
8
9
10
正如可以在上面的输出中看到,5不被打印在控制台上。这是因为当它达到5继续循环。
资源下载继续与嵌套声明
它继续仅当你使用继续内环内声明内环。
例:
//Java Program to illustrate the use of continue statement
//inside an inner loop
public class ContinueExample2 {
public static void main(String[] args) {
//outer loop
for(int i=1;i<=3;i++){
//inner loop
for(int j=1;j<=3;j++){
if(i==2&&j==2){
//using continue statement inside inner loop
continue;
}
System.out.println(i+" "+j);
}
}
}
}
输出:
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
资源下载continue语句标示循环
我们可以用continute语句的标签。因为JDK 1.5此功能介绍。所以,我们现在可以继续在Java中的任何循环无论是外循环或内。
例:
//Java Program to illustrate the use of continue statement
//with label inside an inner loop to continue outer loop
public class ContinueExample3 {
public static void main(String[] args) {
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
//using continue statement with label
continue aa;
}
System.out.println(i+" "+j);
}
}
}
}
输出:
1 1
1 2
1 3
2 1
3 1
3 2
3 3
java的继续while循环语句
例:
//Java Program to demonstrate the use of continue statement
//inside the while loop.
public class ContinueWhileExample {
public static void main(String[] args) {
//while loop
int i=1;
while(i<=10){
if(i==5){
//using continue statement
i++;
continue;//it will skip the rest statement
}
System.out.println(i);
i++;
}
}
}
输出:
1
2
3
4
6
7
8
9
10
资源下载继续在do-while循环语句
例:
//Java Program to demonstrate the use of continue statement
//inside the Java do-while loop.
public class ContinueDoWhileExample {
public static void main(String[] args) {
//declaring variable
int i=1;
//do-while loop
do{
if(i==5){
//using continue statement
i++;
continue;//it will skip the rest statement
}
System.out.println(i);
i++;
}while(i<=10);
}
}
输出:
1
2
3
4
6
7
8
9
10
评论前必须登录!
注册