您的位置 首页 java

Java基础之for循环的六种形式

 
/**
* @desc: for循环的使用
* @author: Mr_YanMingXin
* @create: 2021/7/25-20:07
**/
public class Test01 {
  /**
* 方式一:for(变量初始化;boolean表达式;)
* 最经常使用的 for循环 
*/
  public static void Test01() {
    for (int i = 0; i < 10; i++) {
      System.out.println(i);
    }
  }
  /**
* 方式二:for(;;)
* 等同于while(true)
*/
  public static void Test02() {
    for (; ; ) {
      System.out.println("i");
    }
  }
  /**
* 方式二:for(;;)
*/
  public static void Test03() {
    for (int i = 0; i < 10; ) {
      System.out.println(i);
    }
  }
  /**
* 方式二:for(;;)
* foreach
*/
  public static void Test04() {
    for (int i : new int[]{1, 2, 3, 4, 5}) {
      System.out.println(i);
    }
  }
  /**
* 方式二:for(;;)
* 等同于 while(1==1)
*/
  public static void Test05() {
    for (; 1 == 1; ) {
      System.out.println("i");
    }
  }
  /**
* 方式六:for(初始化;;)
*/
  public static void Test06() {
    for (int i = 0; ; i++) {
      System.out.println(i);
    }
  }
}  

测试:
Test01

0
1
2
3
4
5
6
7
8
9

Test02

i
i
i
i
i
i
i
i
i
i
(无限循环)

Test03

0
0
0
0
0
0
0
0
(无限循环)

Test04

1
2
3
4
5

Test05

i
i
i
i
i
i
i
i
i
i
i
i
i
i
i
i
i
(无限循环)

Test06

(从0开始)
862878
862879
862880
862881
862882
862883
862884
862885
862886
862887
862888
862889
862890
862891
862892
(一直增加直到报错)

文章来源:智云一二三科技

文章标题:Java基础之for循环的六种形式

文章地址:https://www.zhihuclub.com/184499.shtml

关于作者: 智云科技

热门文章

网站地图