Java中有三种主要的循环结构:

  • while 循环
  • do…while 循环
  • for 循环

while 循环

while是最基本的循环,它的结构为:

  1. while (布尔表达式){
  2. // 循环内容
  3. }

只要布尔表达式为 true,循环就会一直执行下去。

  1. public class Test {
  2. public static void main(String args[]) {
  3. int x = 10;
  4. while( x < 20 ) {
  5. System.out.print("value of x : " + x );
  6. x++;
  7. System.out.print("\n");
  8. }
  9. }
  10. }
  11. //运行
  12. value of x : 10
  13. value of x : 11
  14. value of x : 12
  15. value of x : 13
  16. value of x : 14
  17. value of x : 15
  18. value of x : 16
  19. value of x : 17
  20. value of x : 18
  21. value of x : 19

do…while 循环

对于while循环语句而言,如果不满足条件,则不能进入循环。有时候我们需要即使不满足条件,也至少执行一次
do…while 循环和while循环相似,不同的是do….while循环至少会执行一次

  1. do {
  2. //代码语句
  3. }while(布尔表达式)

注意:布尔表达式在循环体的后面,所以语句块在检测布尔表达式之前已经执行了。 如果布尔表达式的值为 true,则语句块一直执行,直到布尔表达式的值为 false。

  1. public class Test {
  2. public static void main(String args[]){
  3. int x = 10;
  4. do{
  5. System.out.print("value of x : " + x );
  6. x++;
  7. System.out.print("\n");
  8. }while( x < 20 );
  9. }
  10. }
  11. //运行
  12. value of x : 10
  13. value of x : 11
  14. value of x : 12
  15. value of x : 13
  16. value of x : 14
  17. value of x : 15
  18. value of x : 16
  19. value of x : 17
  20. value of x : 18
  21. value of x : 19

for循环

虽然所有循环结构都可以用 while 或者 do…while表示,但 Java 提供了另一种语句 —— for 循环,使一些循环结构变得更加简单。

  1. for (初始化; 布尔表达式; 更新) {
  2. //代码语句
  3. }

关于 for 循环有以下几点说明:

  • 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
  • 然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
  • 执行一次循环后,更新循环控制变量。
  • 再次检测布尔表达式。循环执行上面的过程。
  1. public class Test {
  2. public static void main(String args[]) {
  3. for(int x = 10; x < 20; x = x+1) {
  4. System.out.print("x : " + x );
  5. System.out.print("\n");
  6. }
  7. }
  8. }
  9. //运行
  10. x : 10
  11. x : 11
  12. x : 12
  13. x : 13
  14. .....
  15. .....
  16. x : 19

Java 增强 for 循环

Java增强for循环语句格式:

  1. for (声明语句:表达式) {
  2. //代码语句
  3. }

声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

  1. public class For {
  2. public static void main(String [] args){
  3. int [] numbers = {10, 20, 30, 40, 50};
  4. for(int x : numbers){
  5. System.out.println("x:" + x);
  6. }
  7. System.out.println("------------"+"\n");
  8. String [] names = {"MIke", "Nike", "Jon"};
  9. for (String name : names){
  10. System.out.println("name:" + name);
  11. }
  12. }
  13. }
  14. //运行
  15. x:10
  16. x:20
  17. x:30
  18. x:40
  19. x:50
  20. ------------
  21. name:MIke
  22. name:Nike
  23. name:Jon

break关键字

break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。
break 跳出最里层的循环,并且继续执行该循环下面的语句。

语法

break 的用法很简单,就是循环结构中的一条语句:
break;

  1. public class Test {
  2. public static void main(String args[]) {
  3. int [] numbers = {10, 20, 30, 40, 50};
  4. for(int x : numbers ) {
  5. // x 等于 30 时跳出循环
  6. if( x == 30 ) {
  7. break;
  8. }
  9. System.out.print( x );
  10. System.out.print("\n");
  11. }
  12. }
  13. }
  14. //run
  15. 10
  16. 20

continue 关键字

continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。
在 for 循环中,continue 语句使程序立即跳转到更新语句。
在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。

语法

continue 就是循环体中一条简单的语句:
continue;

  1. public class Test {
  2. public static void main(String args[]) {
  3. int [] numbers = {10, 20, 30, 40, 50};
  4. for(int x : numbers ) {
  5. if( x == 30 ) {
  6. continue;
  7. }
  8. System.out.print( x );
  9. System.out.print("\n");
  10. }
  11. }
  12. }
  13. //run
  14. 10
  15. 20
  16. 40
  17. 50