循环语句

1.while循环

int a = 1;
int b = 10;
while (a < b) {
  System.out.println(a);
  a++;
}

先判断再执行。

2.do……while循环

int a = 1;
int b = 10;
do{
  System.out.println(a);
  a++;
}


while (a < b);

先执行再判断。

3.for循环

for(表达式1;表达式2;表达式3){

  语句块;

}

使用break可以跳出循环。

3.foreach

int arr[] = { 3, 4, 5 };
for (int x : arr) {        //(元素变量x : 遍历对象)
  System.out.println(x);   //引用x的Java语句
}

 

posted @ 2019-10-22 09:50  梦想不远  阅读(91)  评论(0)    收藏  举报