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 databaseSystem.out.println("End Processing");//  wait for 10 seconds and look againtry {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循环

java while循环_Java while循环相关推荐

  1. java collection 遍历_Java for循环对集合的遍历

    原标题:Java for循环对集合的遍历 Java集合类的使用可以说是无处不在,总的我们可以将之分为三大块,分别是从Collection接口延伸出的List.Set和以键值对形式作存储的Map类型集合 ...

  2. java for 递归_Java/For循环/递归函数循环

    首先:java的重点和难点,命名和缓存 这次咱们的内容主要用到: 命名规则:大小写字母,下划线,美元符号$,数字,且数字不能打头 变量的声明:数据类型划分内存空间,命名,赋值 方法的声明:修饰符列表, ...

  3. java数组循环_Java之循环结构及数组

    循环结构 for循环for(条件初始化;条件判断;条件变化){ 重复执行的代码: } for循环的执行流程: 1.条件初始化 2.条件判断 3.不满足条件结束循环,满足条件执行,执行循环体语句 4.条 ...

  4. java for循环_Java for循环语句

    Java for循环语句 在不少实际问题中有许多具有规律性的重复操作,因此在程序中就需要重复执行某些语句.一组被重复执行的语句称之为循环体,能否继续重复,取决于循环的终止条件.循环结构是在一定条件下反 ...

  5. java for新循环_Java for循环详解

    Java for循环详解 for 语句是应用最广泛.功能最强的一种循环语句.大部分情况下,for 循环可以代替 while 循环.do while 循环. for 语句是一种在程序执行前就要先判断条件 ...

  6. java foreach赋值_Java foreach循环使用详解[转]

    本文转自码农网 本文非常适合初学Java的程序员,主要是来了解一下Java中的几种for循环用法,分析得十分详细,一起来看看. J2SE 1.5提供了另一种形式的for循环.借助这种形式的for循环, ...

  7. java 日期for循环_java for循环的时候增加循环体的长度是不是不太好的?

    这写得啰嗦了.我的话先这样写: for (CityDataVO cityItem: citys){ if (validateCityItem(cityItem, list)) { continue; ...

  8. java 外循环_java内循环和外循环怎么区分

    关于for循环嵌套作如下解释: 首先内层循环属于外层循环循环体的一部分,当循环体执行完以后外层循环才进入第二次循环,此过程中内层循环需要执行符合条件的完整循环.(外循环控制行数,内循环控制每一行的个数 ...

  9. java while语句_Java while循环

    Java while循环用于重复程序的一部分几次或重复执行一个代码块. 如果迭代次数不固定,建议使用while循环. 语法: while(condition){ //code to be execut ...

最新文章

  1. asmlib方式管理oracle asm环境下,新加存储需特别注意
  2. VS2005中重构的用法
  3. 基于GraphHopper搭建离线路径规划服务并可视化
  4. java 微信多媒体文件_java微信开发之上传下载多媒体文件
  5. Liteide go: cannot find GOROOT directory
  6. Hud 敌兵布阵 --线段树的插点问线
  7. 第五章--数据库中间层实现读写分离
  8. Spring框架XML配置文件使用外部Bean属性注入
  9. go在windows下编译linux的运行的代码
  10. 汽车UDS诊断之控制诊断故障码设置服务(0x85)深度剖析
  11. Vue 实现数组四级联动
  12. 【STM32H7的DSP教程】第27章 FFT的示波器应用
  13. Python「剪藏」网页为 PDF
  14. rust货轮什么时候出现_面食究竟是什么时候出现的?浅谈古代面食发展和变迁史...
  15. 极验验证码破解之selenium
  16. 大数据技术与应用-广东省赛总结
  17. 丛林大反攻java_娱乐新天地:丛林大反攻Open Season
  18. 用计算机谈三生三世,三生三世唯美句子 关于三生三世的句子
  19. Lorawan MAC俗讲
  20. 趁着课余时间学点Python(七)一篇文了解迭代器

热门文章

  1. selenium2.0(WebDriver) API - 转载自:http://www.cnblogs.com/puresoul/p/3477918.html
  2. 捕获事件要比冒泡事件先触发
  3. jquery一些 事件的用法
  4. CSS教程--CSS 属性选择器
  5. 容易忽视但是功能灰常强大的Java API(二. 读写对象)
  6. [转载] (三)Python关键字和内置函数
  7. Uva 11395 Sigma Function (因子和)
  8. 网络篇-NSURLSession介绍
  9. Eclipse注释模板设置详解
  10. MySQL 5.6 dump/load buffer pool实验