目录
C语言版,最最基础的东西
for循环一般形式
for(表达式1;表达式2;表达式3)
语句块;
for循环常用形式
for(循环变量赋初值;循环条件;修改循环变量值)
循环体语句;
for循环流程图
代码实战1
常规打印输出
#include <stdio.h>
int main( ){
printf("爱惜粮食,从一粒米开始\n");
printf("爱惜粮食,从一粒米开始\n");
printf("爱惜粮食,从一粒米开始\n");
printf("爱惜粮食,从一粒米开始\n");
printf("爱惜粮食,从一粒米开始\n");
printf("爱惜粮食,从一粒米开始\n");
printf("爱惜粮食,从一粒米开始\n");
printf("爱惜粮食,从一粒米开始\n");
printf("爱惜粮食,从一粒米开始\n");
printf("爱惜粮食,从一粒米开始\n");
}
for循环打印输出
#include <stdio.h>
int main(){
int i ;
for (i=1;i<=10;i++)
printf("爱惜粮食,从一粒米开始\n");
}
运行结果–均为:
代码实战2
代码块加花括号{ }的位置
代码块加花括号
#include <stdio.h>
int main(){
int i ,sum=0;
for (i=1;i<=100;i++)
{sum=sum+i;}
printf(“一百天一共节约:%d粒粮食\n”,sum);
}
代码块不加花括号
#include <stdio.h>
int main(){
int i ,sum=0;
for (i=1;i<=100;i++)
sum=sum+i;
printf(“一百天一共节约:%d粒粮食\n”,sum);
}
运行结果–均为:
复合语句加花括号
#include <stdio.h>
int main(){
int i ,sum=0;
for (i=1;i<=100;i++)
{sum=sum+i;
printf("一百天一共节约:%d粒粮食\n",sum);}
}
运行结果:
开始运行...
一百天一共节约:1粒粮食
一百天一共节约:3粒粮食
一百天一共节约:6粒粮食
一百天一共节约:10粒粮食
一百天一共节约:15粒粮食
一百天一共节约:21粒粮食
一百天一共节约:28粒粮食
一百天一共节约:36粒粮食
一百天一共节约:45粒粮食
一百天一共节约:55粒粮食
一百天一共节约:66粒粮食
一百天一共节约:78粒粮食
一百天一共节约:91粒粮食
一百天一共节约:105粒粮食
一百天一共节约:120粒粮食
一百天一共节约:136粒粮食
一百天一共节约:153粒粮食
一百天一共节约:171粒粮食
一百天一共节约:190粒粮食
一百天一共节约:210粒粮食
一百天一共节约:231粒粮食
一百天一共节约:253粒粮食
一百天一共节约:276粒粮食
一百天一共节约:300粒粮食
一百天一共节约:325粒粮食
一百天一共节约:351粒粮食
一百天一共节约:378粒粮食
一百天一共节约:406粒粮食
一百天一共节约:435粒粮食
一百天一共节约:465粒粮食
一百天一共节约:496粒粮食
一百天一共节约:528粒粮食
一百天一共节约:561粒粮食
一百天一共节约:595粒粮食
一百天一共节约:630粒粮食
一百天一共节约:666粒粮食
一百天一共节约:703粒粮食
一百天一共节约:741粒粮食
一百天一共节约:780粒粮食
一百天一共节约:820粒粮食
一百天一共节约:861粒粮食
一百天一共节约:903粒粮食
一百天一共节约:946粒粮食
一百天一共节约:990粒粮食
一百天一共节约:1035粒粮食
一百天一共节约:1081粒粮食
一百天一共节约:1128粒粮食
一百天一共节约:1176粒粮食
一百天一共节约:1225粒粮食
一百天一共节约:1275粒粮食
一百天一共节约:1326粒粮食
一百天一共节约:1378粒粮食
一百天一共节约:1431粒粮食
一百天一共节约:1485粒粮食
一百天一共节约:1540粒粮食
一百天一共节约:1596粒粮食
一百天一共节约:1653粒粮食
一百天一共节约:1711粒粮食
一百天一共节约:1770粒粮食
一百天一共节约:1830粒粮食
一百天一共节约:1891粒粮食
一百天一共节约:1953粒粮食
一百天一共节约:2016粒粮食
一百天一共节约:2080粒粮食
一百天一共节约:2145粒粮食
一百天一共节约:2211粒粮食
一百天一共节约:2278粒粮食
一百天一共节约:2346粒粮食
一百天一共节约:2415粒粮食
一百天一共节约:2485粒粮食
一百天一共节约:2556粒粮食
一百天一共节约:2628粒粮食
一百天一共节约:2701粒粮食
一百天一共节约:2775粒粮食
一百天一共节约:2850粒粮食
一百天一共节约:2926粒粮食
一百天一共节约:3003粒粮食
一百天一共节约:3081粒粮食
一百天一共节约:3160粒粮食
一百天一共节约:3240粒粮食
一百天一共节约:3321粒粮食
一百天一共节约:3403粒粮食
一百天一共节约:3486粒粮食
一百天一共节约:3570粒粮食
一百天一共节约:3655粒粮食
一百天一共节约:3741粒粮食
一百天一共节约:3828粒粮食
一百天一共节约:3916粒粮食
一百天一共节约:4005粒粮食
一百天一共节约:4095粒粮食
一百天一共节约:4186粒粮食
一百天一共节约:4278粒粮食
一百天一共节约:4371粒粮食
一百天一共节约:4465粒粮食
一百天一共节约:4560粒粮食
一百天一共节约:4656粒粮食
一百天一共节约:4753粒粮食
一百天一共节约:4851粒粮食
一百天一共节约:4950粒粮食
一百天一共节约:5050粒粮食
运行结束。
注意事项:
for循环后面跟的是英文的圆括号;
圆括号内包含三个表达式,其中用两个 分号隔开;
for循环后的代码块在花括号内表示话括号内的语句都进入循环