流程控制语句3

1.循环语句for

for(初始化表达式; 循环条件; 操作表达式){
    执行语句
    ………
}
for(① ; ② ; ③){
    ④
}
第一步,执行①
第二步,执行②,如果判断结果为true,执行第三步,如果判断结果为false,执行第五步
第三步,执行④
第四步,执行③,然后重复执行第二步
第五步,退出循环

用for循环打印1-100

for(int i=1;i<=100;i++){
     System.out.println(i);
    }

用for循环求1-100的和

int count=0;
for(int i=1;i<=100;i++){
count+=i;
}
System.out.println(count);

用for循环求1-100的偶数和;(奇数和在if判断时改为i%2==1)

int count=0;
for(int i=1;i<=100;i++){
    if(i%2==0){
count+=i;
   }
}
System.out.println(count);

另一种,用for循环求1-100的偶数和;

int count=0;
for(int i=0;i<=100;i=i+2){
count+=i;
}
System.out.println(count);
原文地址:https://www.cnblogs.com/heitaitou/p/12760506.html