用易语言做攻击网站软件,网站建设接活,四川电大住房和城乡建设厅网站,做自媒体的网站有哪些分支语句 if elseswitch 循环语句 whilefordo while goto语句 文章目录 1.什么是语句2.分支语句#xff08;选择结构#xff09;2.1 if语句2.1.1 悬空else2.1.3 练习 2.2 switch语句2.2.1 在switch语句中的break2.2.2 default子句 3.循环语句3.1 while循环3.1.1 while语句中… 分支语句 if elseswitch 循环语句 whilefordo while goto语句 文章目录 1.什么是语句2.分支语句选择结构2.1 if语句2.1.1 悬空else2.1.3 练习 2.2 switch语句2.2.1 在switch语句中的break2.2.2 default子句 3.循环语句3.1 while循环3.1.1 while语句中的break和continue 3.2 for循环3.2.1 语法3.2.2 break和continue在for循环中3.2.3 for语句的循环控制变量3.2.4 一些for循环的变种3.2.5 试题 3.3 do...while()循环3.3.1 do...while()循环语法3.3.2 执行流程3.3.3 do语句的特点3.3.4 do while 循环中的break和continue 3.4 习题3.4.3 猜数字游戏 4.goto语句 1.什么是语句
在C语言当中语句可以分为以下5类
表达式语句函数调用语句控制语句复合语句空语句 控制语句 用于控制程序的执行流程以实现程序的各种结构方式它们由特定的语句符号符组成C语言有9种控制语句 1.条件判断语句也叫分支语句if else 语句 switch语句 2.循环执行语句for语句 while语句 do while语句 3.转向语句break语句 continue语句 return语句 goto语句 2.分支语句选择结构
这样吧你先在steam搜索“千恋*万花”点击购买安装好后立即运行打开千恋万花在主页面点击开始游戏进入游戏页面然后依次选择“说实话”、“不好说”、“觉得很可爱”、“单独行动”、“摸摸头”、“有点担心”和“安抚”进入丛雨路线 没错galgame中的选项就是选择语句。
达成对应的条件后就进入对应的结果。
2.1 if语句
if语句的语法结构
//单分支
if(exp1)语句;
//双分支
if(exp1)语句1;
else语句2;//多分支
if(exp1)语句1;
else if(exp2)语句2;
else语句3;实例演示
//代码1
#include stdio.h
int main()
{int age 0;scanf(%d,age);if(age18){printf(未成年\n);}else{printf(已成年\n);}return 0;
}//代码2
#include stdio.h
int main()
{int age 0;scanf(%d,age);if(age18){printf(青少年\n);}else if(age18age30){printf(青年\n);}else if(age30age60){printf(中年\n);}else if(age60age80){printf(老年\n);}else{printf(寿星\n);}return 0;
}在C语言中0表示假非0表示真 值得注意的是If只能管理后面一条语句所有我们要多用{}进行匹配防止使人误解. {}就是一个代码块。
2.1.1 悬空else
看下面这个代码
#include stdio.h
int main()
{int a 0;if(a)if(a0)printf(hello\n);elseprintf(hi\n);return 0;
}你觉得会打印什么呢 答案是什么都不打印这里的else是和第二个if进行匹配的。 为了避免这种情况我们要让代码好好对齐或者加入{}来避免误解 //代码对齐
#include stdio.h
int main()
{int a 0;if(a)if(a0)printf(hello\n);elseprintf(hi\n);return 0;
}//利用{}
#include stdio.h
int main()
{int a 0;if(a){if(a0)printf(hello\n);elseprintf(hi\n);}return 0;
}else的匹配else是和离他最近的if匹配的
2.1.3 练习 1.判断一个数是否是奇数。 2.输出1~100之间的奇数。 //1.判断一个数是否是奇数。
#include stdio.h
int main()
{int a 0;scanf(%d,a);if(a%21){printf(是奇数\n);}else{printf(不是奇数\n);}return 0;
}//2.2.输出1~100之间的奇数。
#include stdio.h
int main()
{//利用while循环int i 1;while(i100){if(i%21){printf(%d ,i);}i;}return 0;
}//方法2
#include stdio.h
int main()
{//利用while循环int i 1;while(i100){printf(%d ,i);i2;}return 0;
}2.2 switch语句
switch语句也是一种分支语句常常用于多分支的情况。
switch(整型表达式)
{case 整型常量表达式://case是整型常量表达式 也可以是char 因为底层为ASCII码也算语句;
}
比如让你根据数字输出对应的星期。
#include stdio.h
int main()
{int day 0;scanf(%d,day);switch(day){case 1:printf(星期一\n);case 2:printf(星期二\n);case 3:printf(星期三\n);case 4:printf(星期四\n);case 5:printf(星期五\n);case 6:printf(星期六\n);case 7:printf(星期日\n);}return 0;
}//当我们输入5时
/*
打印结果
星期五
星期六
星期日
*/这是为什么呢因为在switch中我们只有利用了break才能实现真正的分支。
2.2.1 在switch语句中的break
#include stdio.h
int main()
{int day 0;scanf(%d,day);switch(day){case 1:printf(星期一\n);break;case 2:printf(星期二\n);break;case 3:printf(星期三\n);break;case 4:printf(星期四\n);break;case 5:printf(星期五\n);break;case 6:printf(星期六\n);break;case 7:printf(星期日\n);break;}return 0;
}
//当我们输入5时
/*
打印结果
星期五
*/break语句的实际效果是把语句列表划分为不同的分支部分。 思考如果有人在这个程序运行时输入8会怎么样
2.2.2 default子句
答案当输入的值与所有case都不匹配时就直接退出switch然后继续程序。 但是如果你不想要忽略不匹配这种情况。 你可以在语句列表中增加一个default子句。 可以写在任何一个case标签可以出现的位置switch里面的判断可以没有顺序。
#include stdio.h
int main()
{int day 0;scanf(%d,day);switch(day){case 1:printf(星期一\n);break;case 2:printf(星期二\n);break;case 3:printf(星期三\n);break;case 4:printf(星期四\n);break;case 5:printf(星期五\n);break;case 6:printf(星期六\n);break;case 7:printf(星期日\n);break;default:printf(输入错误\n);break;//建议加上 }return 0;
}switch允许嵌套使用。
3.循环语句
whilefordo while 简单的循环结构 3.1 while循环
while(条件)语句;当条件满足时循环就会执行下去。 while语句执行流程 打印1-10数字 #include stdio.h
int main()
{//利用while循环int i 1;while(i10){printf(%d ,i);i;}return 0;
}
//打印结果
//1 2 3 4 5 6 7 8 9 103.1.1 while语句中的break和continue
break介绍 在循环中只要执行了break就会直接终止循环直接跳出循环体。
#include stdio.h
int main()
{//利用while循环int i 1;while(i10){if(i5)break;printf(%d ,i);i;}return 0;
}
//打印结果
//1 2 3 4 continue介绍 continue是用来终止本次循环的也就是本次循环中continue后面的代码不会执行 而是直接跳转到while语句的判断部分。进入下一次循环。 //代码1
#include stdio.h
int main()
{//利用while循环int i 1;while(i10){if(i5)continue;printf(%d ,i);i;}return 0;
}
//打印结果
//1 2 3 4 4 4 4 4 4 4 4....//代码2
#include stdio.h
int main()
{//利用while循环int i 1;while(i10){i;if(i5)continue;printf(%d ,i);}return 0;
}
//打印结果
//2 3 4 6 7 8 9 103.2 for循环
了解完while循环后for可以说是while的整合。
3.2.1 语法
for(expr1;expr2;expr3)循环语句;exp1 为初始化部分用来初始化循环变量的。 exp2 为条件判断部分用于判断循环终止的 exp3 为调整部分用于循环条件的调整。 打印1-10 //代码1
#include stdio.h
int main()
{int i 0;for(i 1;i10;i){printf(%d ,i);}return 0;
}
//打印结果1 2 3 4 5 6 7 8 9 10//代码2
#include stdio.h
int main()
{for(int i 1;i10;i)//在里面初始化是c里的写法但在c99标准后引入{printf(%d ,i);}return 0;
}
//打印结果1 2 3 4 5 6 7 8 9 10for执行流程图
for与while循环的对比
#include stdio.h
int main()
{int i 0;while(i10){printf(%d ,i);i;}for(i 1;i10;i){printf(%d ,i);}return 0;
}循环的3个必要条件无论是while循环还是for都是有的但是while的三部分比较分散。 查找修改就不够集中于方便对此for循环的风格更胜一筹。
3.2.2 break和continue在for循环中
可以发现的是在for和在while循环里面是没有特别大区别的但由于for独特的执行调整步骤和while还是略有差异
//continue在while
#include stdio.h
int main()
{//利用while循环int i 1;while(i10){if(i5)continue;printf(%d ,i);i;}return 0;
}
//打印结果
//1 2 3 4 4 4 4 4 4 4 4....//continue在for循环
#include stdio.h
int main()
{//利用for循环int i 1;for(i 1;i10;i){if(i5)continue;printf(%d ,i);}return 0;
}
//打印结果
//1 2 3 4 6 7 8 9 10for循环里的continue在跳过本次循环时是无法跳过for中的调整语句的。
3.2.3 for语句的循环控制变量
建议不是必须 1.不要在for循环体内修改循环变量防止for循环失去控制 2.建议for语句的循环变量的取值采用 前闭后开区间的写法 int i 0;
//前闭后开
for(i 0;i10;i)
{}
//前闭后闭
for(i 0;i9;i)
{}3.2.4 一些for循环的变种
#include stdio.h
int main()
{//利用for循环int i 1;for(;;)//for省略判断即为判断恒为真{printf(hello\n);}//代码2int i 0;int j 0;//打印多少个hello呢for(i 0;i10;i){for(j 0;j10;j){printf(hello\n);}}//100个//代码3int i 0;int j 0;//打印多少个hello呢for(;i10;i){for(;j10;j){printf(hello\n);}}//10个//代码4 int x,y;for(x 0,y 0;x2y5;x,y)//使用多个变量控制循环{printf(hello\n);}return 0;
}
//打印结果
//hello hello hello...3.2.5 试题
//循环多少次
#include stdio.h
int main()
{int i 0;int k 0;for(i 0,k 0;k0;i,k)k;return 0;
}//0次3.3 do…while()循环
3.3.1 do…while()循环语法
do
{循环语句;
}while(表达式);3.3.2 执行流程 3.3.3 do语句的特点
循环至少执行一次使用场景有限不常使用。 打印1-10 #include stdio.h
int main()
{int i 1;do{printf(%d ,i);i;}while(i10);return 0;
}
// 打印结果
//1 2 3 4 5 6 7 8 9 103.3.4 do while 循环中的break和continue
//代码1
#include stdio.h
int main()
{int i 10;do{if(i 5)break;printf(%d ,i);}while(i10);return 0;
}//代码2
#include stdio.h
int main()
{int i 10;do{if(i 5)continue;printf(%d ,i);}while(i10);return 0;
}3.4 习题 1.计算n的阶乘 2.计算1!2!…10! 3.在一个有序数组中查找具体某个数n 4.编写代码演示多个字符从两端移动向中间汇聚。 //1.
#include stdio.h
int main()
{int n 0;scanf(%d,n);int ret 1;for(int i 1;in){ret*i;}printf(%d\n,ret);return 0;
}//2.1
#include stdio.h
int main()
{int sum 0;for(int n 1;n10;n){int ret 1;for(int i 1;in;i){ret*i;}sumret;}printf(%d\n,sum);return 0;
} //2.2
#include stdio.h
int main()
{int sum 0;int ret 1;for(int n 1;n10;n){ret * n;sumret;}printf(%d\n,sum);return 0;
}//3.1
#include stdio.h
int main()
{int arr[10] {1,2,3,4,5,6,7,8,9,10};int x 0;scanf(%d,x);for(int i 0;i10;i){if(xarr[i]){printf(找到了下标为%d\n,i);break;}}if(i10){printf(没找到\n);}return 0;
}
//3.2二分查找
#include stdio.h
int main()
{int arr[10] {1,2,3,4,5,6,7,8,9,10};int x 0;scanf(%d,x);int left 0,right 9;int flag 0;while(leftright){int mid (leftright)/2;if(arr[mid]x){printf(找到了下标为%d\n,mid);flag 1;break;}else if(arr[mid]x){left mid1;}else{right mid-1;}}if(flag 0){printf(找不到\n);}return 0;
}//4
#include stdio.h
int main()
{char arr1[] hello world!;char arr2[] ************;int n sizeof(arr1);int left 0;int right n - 1;for (left 0, right n - 1; left right; left, --right){arr2[left] arr1[left];arr2[right] arr1[right];printf(%s\n, arr2);}return 0;
}3.4.3 猜数字游戏
学以上的内容我们可以写一个简单的猜谜游戏
#include stdio.h
#include stdlib.h//srand和rand的头文件
#include time.h//time的头文件
void menu()
{printf(**********************\n);printf(*****1.paly***********\n);printf(*****0.exit***********\n);printf(**********************\n);
}
void game()
{int r rand() % 100 1;//rand可以产生随机数但因为rand的随机数是根据srand所提供的种子生成所以我们还要使用srand但是由由于srand所提供的种子也要输入数字才可以生成为此我在使用了time函数生成当前时间的时间戳数来充当输入的数字以达到rand生成随机数的效果。int guess 0;//玩家要猜测的数printf(游戏开始\n);while (1)//直到猜中{scanf(%d, guess);if (guess r){printf(猜小了\n);}else if (guess r){printf(猜大了\n);}else{printf(恭喜你猜中了\n);break;}}
}
int main()
{int input 0;srand((unsigned)time(NULL));do//进入循环{menu();//打印菜单printf(请选择_);//提示信息scanf(%d, input);//根据提示选择进入还是退出//根据输入进入相应区域,用到多分支语句switchswitch (input){case 1:game();//进入游戏break;case 0:printf(已退出游戏\n);break;default:printf(输入错误\n);break;}} while (input);}4.goto语句
C语言中提供了可以随意滥用的goto语句和标记跳转的标号。 从理论上来讲goto语句是没有必要的实际中没有goto语句也可以很容易的写成代码。 但是在某些场合下goto语句还是用的着的最常见的用法就是终止某些深层嵌套的结构。 例如一次性跳出多层循环。 多重循环下使用break是达不到目的的break只能从最内层循环退出到上一层循环。
for(...)
{for(...){for(...){if(disaster)goto error;}}
}
error:if(disaster)//处理错误情况大部分部分使用goto的情况是可以利用循环代替的。
//代码1
#include stdio.h
int main()
{
again://...if(...){//...break;}else{goto again;}return 0;
}//代码2
#include stdio.h
int main()
{while(1){if(...){//...break;}}return 0;
}完 文章转载自: http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn http://www.morning.clccg.cn.gov.cn.clccg.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.mzzqs.cn.gov.cn.mzzqs.cn http://www.morning.jlmrx.cn.gov.cn.jlmrx.cn http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.ldnrf.cn.gov.cn.ldnrf.cn http://www.morning.rmfh.cn.gov.cn.rmfh.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.tndhm.cn.gov.cn.tndhm.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.djwpd.cn.gov.cn.djwpd.cn http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn http://www.morning.gbgdm.cn.gov.cn.gbgdm.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.sqyjh.cn.gov.cn.sqyjh.cn http://www.morning.rptdz.cn.gov.cn.rptdz.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.mnclk.cn.gov.cn.mnclk.cn http://www.morning.xptkl.cn.gov.cn.xptkl.cn http://www.morning.jprrh.cn.gov.cn.jprrh.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.xbbrh.cn.gov.cn.xbbrh.cn http://www.morning.dzqyn.cn.gov.cn.dzqyn.cn http://www.morning.xbmwm.cn.gov.cn.xbmwm.cn http://www.morning.qxjck.cn.gov.cn.qxjck.cn http://www.morning.fy974.cn.gov.cn.fy974.cn http://www.morning.hwtb.cn.gov.cn.hwtb.cn http://www.morning.qstjr.cn.gov.cn.qstjr.cn http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.wqbbc.cn.gov.cn.wqbbc.cn http://www.morning.gghhmi.cn.gov.cn.gghhmi.cn http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.zbqry.cn.gov.cn.zbqry.cn http://www.morning.xhwty.cn.gov.cn.xhwty.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.zdnrb.cn.gov.cn.zdnrb.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.pqjlp.cn.gov.cn.pqjlp.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.bzbq.cn.gov.cn.bzbq.cn http://www.morning.pdkht.cn.gov.cn.pdkht.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.nsyzm.cn.gov.cn.nsyzm.cn http://www.morning.xmxbm.cn.gov.cn.xmxbm.cn http://www.morning.pdkht.cn.gov.cn.pdkht.cn http://www.morning.bjjrtcsl.com.gov.cn.bjjrtcsl.com http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.c7624.cn.gov.cn.c7624.cn http://www.morning.knnc.cn.gov.cn.knnc.cn http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.ygbq.cn.gov.cn.ygbq.cn http://www.morning.nmkfy.cn.gov.cn.nmkfy.cn http://www.morning.knscf.cn.gov.cn.knscf.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.wqmpd.cn.gov.cn.wqmpd.cn http://www.morning.qymqh.cn.gov.cn.qymqh.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.snkry.cn.gov.cn.snkry.cn http://www.morning.cszbj.cn.gov.cn.cszbj.cn http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn http://www.morning.nbfkk.cn.gov.cn.nbfkk.cn http://www.morning.fxqjz.cn.gov.cn.fxqjz.cn http://www.morning.stwxr.cn.gov.cn.stwxr.cn http://www.morning.qzfjl.cn.gov.cn.qzfjl.cn http://www.morning.rsdm.cn.gov.cn.rsdm.cn http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.tntgc.cn.gov.cn.tntgc.cn http://www.morning.tkchg.cn.gov.cn.tkchg.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.ggmls.cn.gov.cn.ggmls.cn http://www.morning.tymwx.cn.gov.cn.tymwx.cn http://www.morning.ttvtv.cn.gov.cn.ttvtv.cn http://www.morning.wjqyt.cn.gov.cn.wjqyt.cn