码迷,mamicode.com
首页 > 编程语言 > 详细

java---for循环

时间:2021-02-27 13:41:34      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:system   str   循环控制   语句块   for语句   步骤   布尔   相等   update   


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


  • 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
  • 然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
  • 执行一次循环后,更新循环控制变量。
  • 再次检测布尔表达式。循环执行上面的过程

package hello.demo;

public class hello2 {
public static void main(String[] args){
for (int x = 20; x < 50; x++){
System.out.println("the value of x is "+x);
System.out.println("update x");
}
}
}

增强型的java for循环

for(声明语句 : 表达式) {
//代码句子
}

声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

 

可以把增强型的for语句理解为python中的for i in list ,用于循环list中的元素



package hello.demo;

public class hello2 {
public static void main(String[] args){
String[] StringList={"guoshun1","guoshun2","guoshun3"};
for (String i : StringList){
System.out.println("The Stirng in StringList is "+i);
}
int[] IntList={1,2,3,33,44,55};
for (int j : IntList){
System.out.println("the int in Intlist is "+j);
}
}
}

java---for循环

标签:system   str   循环控制   语句块   for语句   步骤   布尔   相等   update   

原文地址:https://www.cnblogs.com/shunguo/p/14455144.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!