C语言培训_005
来源:互联网 发布:linux timekeeper 编辑:程序博客网 时间:2024/05/17 02:51
循环语句
while(){}
特点:先判断括号中的值(或者说是判断括号中的条件),值为0,则不执行,非0则循环。到下一次再开始判断。
#include"windows.h"#include"stdio.h"int main(){int num;int temp;temp=scanf("%d",&num);printf("输入整数开始循环,输入字母停止循环。");while(temp==1){printf("*****输入的数字为%d\n",num);temp=scanf("%d",&num);}system("pause");return 0;}将上述代码简化一下:
#include"windows.h"#include"stdio.h"int main(){int num; while(scanf("%d",&num)){printf("****输入的数字为%d\n",num);}system("pause");return 0;}
do{}while();
注意这里有分号,特点是,先做一次,再看while()条件。
来看看下面程序,说说他们执行了几次,特点是什么。
#include"windows.h"#include"stdio.h"int main(){int i=0;int sum=0;do{i++;sum+=i;}while(i<100);int j=0;int soo=0;while(j<5){soo=soo+j;}int a=0;int m=0;do{a++;m=m+1;}while(a<0);printf("%d\n",sum);system("pause");return 0;}
for( *1号位置* ; *2号位置* ; *3号位置* )
特点:先执行一次1号位置代码,以后1号位置则不再执行,再判断2号位置的条件成立则执行主体的花括号代码。最后执行3号位置代码。然后再进入2号位置判断,再进入花括号,再进入3号位置……
#include"windows.h"#include"stdio.h"int main(){int sum=0;for(int i=0;i<-5;i++){sum++;}printf("%d\n",sum);system("pause");return 0;}
运算符
a++;先用后加。
++a;先加后用。
break和continue与goto
break,终止本层循环。
continue,跳过本层循环的一次
goto,跳到指定位置。(一般可以用其它来替换它)
#include"windows.h"#include"stdio.h"int main(){for(int i=0;i<10;i++){if(i%2==0){continue;//当可以被2整除,直接跳到下一次循环。}printf("%d\n",i);}printf("******************************");for(int i=0;i<10;i++){if(i%2==0){break;//当可以被2整除,终止整个循环。}printf("%d\n",i);}system("pause");return 0;}来看一下goto:
#include"windows.h"#include"stdio.h"int main(){int x=0;while(x++!=20){printf("本次循环变量为%d\n",x);if(x==10){goto loopA;}}loopB:goto loopD;loopA:printf("跳转后的第一行代码\n");loopC:goto loopB;loopD:printf("loopD\n");system("pause");return 0;}
嵌套
循环里面还有循环,就是循环的嵌套。可以嵌套很多层的。
#include"windows.h"#include"stdio.h"int main(){for(int i=1;i<11;i++){int j=1;while(j<11){printf("%d\t",j++);}printf("\n");}system("pause");return 0;}
输出乘法表:
#include"windows.h"#include"stdio.h"int main(){for(int i=1;i<11;i++){for(int j=1;j<=i;j++){printf("%2d*%2d=%2d ",j,i,i*j);}printf("\n");}system("pause");return 0;}
if() else if() else 的初步学习
这是一种选择结构。
先判断 if() ,再判断 else if() ,其中else if 可能有很多,最后判断 else 遇到第一个判断成立的则进入其中,进入之后其他后来的都不再判断不再执行。
例子:
#include"windows.h"#include"stdio.h"int main(){int temp;scanf("%d",&temp);if(temp>0){if(temp%2==0){ printf("正偶数\n");}else{printf("正奇数\n");} }else if(temp==0){printf("0\n");}else if(temp==-1){printf("-1\n");}else{printf("不等于-1的负整数"); }system("pause");return 0;}
判断闰年:
#include"windows.h"#include"stdio.h"int main(){int year;scanf("%d",&year);if(year%4==0&&(year%100!=0||year%400==0)){printf("闰年\n");}else{printf("不是闰年");}system("pause");return 0;}
0 0
- C语言培训_005
- unity3d培训_005
- 【linux c】c语言十进制数转换为十六进制输出_学习笔记_005
- C语言培训总结
- C语言培训_001
- C语言培训_002
- C语言培训_003
- C语言培训_004
- C语言培训_006
- C语言培训_007
- C语言培训_008
- C语言培训_009
- C语言培训_010
- C语言培训_011
- [C] C程序100例_005
- C语言与算法设计技能培训
- 一次C语言培训的试讲
- 培训 学习总结(C语言)
- SimpleAdapter的应用
- jvm垃圾收集器配置-1
- 下面哪些特性可能导致代码体积膨胀:----腾讯2016研发工程师在线模拟笔试题
- ATOM基础教程一ATOM自定义代码片段(8)
- bzoj1026 [SCOI2009]windy数(数位dp)
- C语言培训_005
- [POJ3133]Manhattan Wiring 插头dp
- Spring核心技术(九)——Spring管理的组件和Classpath扫描
- 疯狂JAVA讲义比较--JAVA概述
- VS调试C++项目实例
- Python模拟登录
- 巩固基础篇:八皇后问题:经典回溯法
- 程序员的升级之路
- 存储过程使用DDL语句