java while循环_Java while循环

java while循环

Java while loop is used to execute a block of statements continuously till the given condition is true. Earlier we looked into java for loop.

Java while循环用于连续执行语句块,直到给定条件成立为止。 之前我们研究过java for loop

Java while循环 (Java while loop)

While loop in java syntax is as follows.

Java语法中的while循环如下。

while (expression) {
    // statements
}

The expression for while loop must return boolean value, otherwise it will throw compile time error.

while循环的expression必须返回布尔值,否则将抛出编译时错误。

While循环Java流程图 (While loop java flow diagram)

Java while循环示例 (Java while loop example)

Here is a simple java while loop example to print numbers from 5 to 10.

这是一个简单的java while循环示例,可打印5到10之间的数字。

package com.journaldev.javawhileloop;

public class JavaWhileLoop {

	public static void main(String[] args) {

		int i = 5;
		while (i <= 10) {
			System.out.println(i);
			i++;
		}
	}
}

Notice that I am increasing value of i inside while loop, otherwise the while loop will never terminate and we will have an infinite loop. The only way to terminate the program running in infinite loop is to manually quit it or when the JVM runs out of memory.

注意,我在while循环中增加i的值,否则while循环将永远不会终止,而我们将有一个无限循环。 终止以无限循环运行的程序的唯一方法是手动退出程序,或者在JVM内存不足时退出。

Also note that if boolean expression returns false then statements inside while loop won’t execute. So there is a chance that statement inside while loop will never execute.

还要注意,如果布尔表达式返回false,则while循环中的语句将不会执行。 因此,while循环内的语句将永远不会执行。

Java While循环与迭代器 (Java while loop with Iterator)

Java while loop is used a lot with iterator in java. Let’s see a short example of iterating over an ArrayList using while loop.

Java while循环与java中的迭代器经常使用。 让我们来看一个使用while循环迭代ArrayList的简短示例。

List<String> veggies = new ArrayList<>();
veggies.add("Spinach");
veggies.add("Potato");
veggies.add("Tomato");

Iterator<String> it = veggies.iterator();

while(it.hasNext()) {
	System.out.println(it.next());
}

It will print output as below.

它将打印如下输出。

Spinach
Potato
Tomato

而真正的java (while true java)

Sometimes we intentionally want our while loop to run infinitely. In that case we can use while true loop. An example could be looking for a file at specific location continuously and if found then process it. Below is a pseudo code example of while true loop in java.

有时我们有意地希望while循环无限运行。 在这种情况下,我们可以使用while true循环。 一个示例可能是连续在特定位置查找文件,如果找到该文件,则对其进行处理。 以下是Java中while真正循环的伪代码示例。

package com.journaldev.javawhileloop;

public class WhileTrueJava {

	public static void main(String[] args) {
		while(true) {
			System.out.println("Start Processing");
			// look for a file at specific directory
			// if found then process it, say insert rows into database
			System.out.println("End Processing");
			
			//  wait for 10 seconds and look again
			try {
				Thread.sleep(10*1000);
			} catch (InterruptedException e) {
				System.out.println("Thread Interrupted, exit now");
				System.exit(0);
			}
		}
	}

}

If we run above program, we will have to manually quit it using Ctrl+C on terminal. If you are using Eclipse then there is a red button to terminate the current running program.

如果运行上述程序,则必须在终端上使用Ctrl+C手动退出它。 如果使用的是Eclipse,则有一个红色按钮可以终止当前正在运行的程序。

Let’s say you have written some code inside a class and it’s work in progress so you don’t want to get it executed. Can we just wrap it inside while false loop so that it will not execute at all?

假设您已经在类中编写了一些代码,并且该代码正在进行中,所以您不想使其执行。 我们可以将它包装在false循环中而根本不执行吗?

Well java compiler is smart enough to give us compilation error that code is unreachable. Below are images from Eclipse editor as well as when we are trying to compile the program from terminal.

Java编译器非常聪明,足以给我们带来代码无法到达的编译错误。 下面是Eclipse编辑器的图像,以及当我们尝试从终端编译程序时的图像。

But there is another way to achieve this, use a dummy boolean variable and the compiler error will be gone.

但是还有另一种方法可以实现,使用虚拟布尔变量,编译器错误将消失。

boolean mutex = false;
while(mutex) {
	System.out.println("incomplete code");
}

That’s all about java while loop, I hope you get clear idea how and when to use while loop in java.

这就是关于Java while循环的全部内容,希望您能清楚地知道如何以及何时在java中使用while循环。

Reference: Oracle Documentation

参考: Oracle文档

翻译自: https://www.journaldev.com/16510/java-while-loop

java while循环

  • 4
    点赞
  • 0
    评论
  • 9
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值