java声明_Java继续声明

java声明

Java continue statement is used to skip the current iteration of a loop. Continue statement in java can be used with for, while and do-while loop.

Java Continue语句用于跳过循环的当前迭代。 Java中的Continue语句可以与forwhiledo-while循环一起使用。

Java继续声明 (Java continue statement)

When continue statement is used in a nested loop, it only skips the current execution of the inner loop. Java continue statement can be used with label to skip the current iteration of the outer loop too. Let’s have a look at some continue java statement examples.

在嵌套循环中使用continue语句时,它仅跳过内部循环的当前执行。 Java Continue语句也可以与label一起使用,以跳过外部循环的当前迭代。 让我们看一些继续的Java语句示例。

Java继续循环 (Java continue for loop)

Let’s say we have an array of integers and we want to process only even numbers, here we can use continue loop to skip the processing of odd numbers.

假设我们有一个整数数组 ,并且只想处理偶数,这里我们可以使用continue循环跳过对奇数的处理。

package com.journaldev.java;

public class JavaContinueForLoop {

	public static void main(String[] args) {
		int[] intArray = { 1, 2, 3, 4, 5, 6, 7 };

		// we want to process only even entries
		for (int i : intArray) {
			if (i % 2 != 0)
				continue;
			System.out.println("Processing entry " + i);
		}
	}

}

Java继续while循环 (Java continue while loop)

Let’s say we have an array and we want to process only index numbers divided by 3. We can use java continue statement here with while loop.

假设我们有一个数组,我们只想处理索引数除以3。我们可以在此处使用带有while循环的java continue语句。

package com.journaldev.java;

public class JavaContinueWhileLoop {

	public static void main(String[] args) {
		int[] intArray = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
		int i = 0;
		while (i < 10) {

			if (i % 3 != 0) {
				i++;
				continue;
			}
			System.out.println("Processing Entry " + intArray[i]);
			i++;
		}
	}

}

Java继续执行do-while循环 (Java continue do-while loop)

We can easily replace above while loop code with do-while loop as below. Result and effect of continue statement will be same as above image.

我们可以轻松地将上述while循环代码替换为do-while循环,如下所示。 Continue语句的结果和效果与上图相同。

do {

	if (i % 3 != 0) {
		i++;
		continue;
	}
	System.out.println("Processing Entry " + intArray[i]);
	i++;
} while (i < 10);

Java继续标签 (Java continue label)

Let’s have a look at java continue label example to skip the outer loop processing. We will use two dimensional array in this example and process an element only if all the elements are positive numbers.

让我们看一下Java继续标签示例,以跳过外循环处理。 在此示例中,我们将使用二维数组 ,并且仅当所有元素均为正数时才处理元素。

package com.journaldev.java;

import java.util.Arrays;

public class JavaContinueLabel {

	public static void main(String[] args) {

		int[][] intArr = { { 1, -2, 3 }, { 0, 3 }, { 1, 2, 5 }, { 9, 2, 5 } };

		process: for (int i = 0; i < intArr.length; i++) {
			boolean allPositive = true;
			for (int j = 0; j < intArr[i].length; j++) {
				if (intArr[i][j] < 0) {
					allPositive = false;
					continue process;
				}
			}
			if (allPositive) {
				// process the array
				System.out.println("Processing the array of all positive ints. " + Arrays.toString(intArr[i]));
			}
			allPositive = true;
		}

	}

}

Java继续重点 (Java continue important points)

Some important points about java continue statement are;

关于Java continue语句的一些重要要点是:

  1. For simple cases, continue statement can be easily replaced with if-else conditions but when we have multiple if-else conditions then using continue statement makes our code more readable.

    对于简单的情况,continue语句可以轻松地用if-else条件替换,但是当我们有多个if-else条件时,则使用continue语句会使我们的代码更具可读性。
  2. continue statement comes handy incase of nested loops and to skip a particular record from processing.

    在嵌套循环的情况下,continue语句很方便,并且可以跳过处理中的特定记录。

I have made a short video explaining java continue statement in detail, you should watch it below.

我制作了一段简短的视频,详细解释了Java继续语句,您应该在下面观看。

演示地址

Reference: Oracle Documentation

参考: Oracle文档

翻译自: https://www.journaldev.com/983/java-continue-statement

java声明

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值