1循环结构:
在某些条件满足的情况下,反复执行特定代码的功能
1.1循环语句分类:
for循环
while循环
do-while循环
1.2循环语句的四个组成部分
初始化部分
循环条件部分 ——>是Boolean类型
循环体部分
迭代部分
1.3for循环的结构:
for(①;②;④){
③
}
执行过程:① - ② - ③ - ④ - ② - ③ - ④-...②
for(int i = 1;i <= 5;i++){
System.out.println("Hello World");
}
i在for循环内有效。出了for循环就失效。
System.out.println(i);
遍历一百以内的偶数,输入所有偶数的和:
int sum = 0;//记录所有偶数的和
int count = 0;//记录偶数的个数
for(int i = 1;i <= 100;i++){
if(i % 2 == 0){
System.out.peintln(i);
sum + = i;
}
}
System.out.println("总和为:"+ sum);
System.out.println("个数为:"+count);`