//选择结构:判断奇数int main(){ int num; printf(\"请输入:\"); scanf(\"%d\", &num); if (num % 2 != 0) printf(\"%d是奇数\\n\", num); else printf(\"%d不是奇数\\n\",num); return 0;}//输出100内的奇数int main(){ int i=1; while (i <= 100) { if(i%2!=0) printf(\"%d \",i); i++; } return 0;}//else总是和它相邻最近的if匹配int main(){ int a = 6; if (5 == a) if (a > 10) printf(\"Test00\\n\"); else //0 printf(\"Test11\\n\"); else //1 //如果注释这里的else1则else0和它最近的if匹配 printf(\"Test22\");}//if,if...else,if...else if,switch选择语句//if测试int main(){ char ch = \'a\';//ACSII码值97 if (ch == 97) printf(\"Yes\\n\");}int main(){ char ch = \'a\'; //ACSII码值97 if (ch == 98) printf(\"Yes\\n\"); else printf(\"No\\n\");}//if。。。else测试int main(){ int day; printf(\"Please enter the day:\"); scanf(\"%d\",&day); if (day == 1) printf(\"Monday!\\n\"); else if (day == 2) printf(\"Tuesday!\\n\"); else if (day == 3) printf(\"Wednesday!\\n\"); else if (day == 4) printf(\"Thuresday!\\n\"); else if (day == 5) printf(\"Friday!\\n\"); else if (day == 6) printf(\"Saturday!\\n\"); else if (day == 7) printf(\"Sunday!\\n\");}//switch测试int main(){ int days; scanf(\"%d\",&days); switch (days) { case 1: printf(\"Monday!\\n\"); break; //break是结束语句,跳出程序的出口,如果没有会从case1执行到defualt,也就是会打印全部的天数和defualt的内容 case 2: printf(\"Tuesday!\\n\"); break; case 3: printf(\"Wednesday!\\n\"); break; case 4: printf(\"Thursday!\\n\"); break; case 5: printf(\"Friday!\\n\"); break; case 6: printf(\"Saturday!\\n\"); break; case 7: printf(\"Sunday!\\n\"); break; default: printf(\"Enter Errors,Please enter again!\"); break; }}//建议每个switch都匹配一个defualt分支//default是没有匹配的情况才会执行,每个switch语句只能有一个default,其可以出现在语句列表的任何位置int main(){ int days; scanf(\"%d\", &days); switch (days) { case 1: printf(\"Monday!\\n\"); case 2: printf(\"Tuesday!\\n\"); case 3: printf(\"Wednesday!\\n\"); case 4: printf(\"Thursday!\\n\"); case 5: printf(\"Friday!\\n\"); case 6: printf(\"Saturday!\\n\"); case 7: printf(\"Sunday!\\n\"); default: printf(\"Enter Errors,Please enter again!\\n\"); }}运行结果:/****************************************************************/1Monday!Tuesday!Wednesday!Thursday!Friday!Saturday!Sunday!Enter Errors, Please enter again!请按任意键继续. . ./***************************************************************///以下是多个case分支共用一个打印语句int main(){ int num; scanf(\"%d\",&num); switch (num) { case 0: case 1: printf(\"Test--->0\"); break; case 3: case 4: printf(\"Test--->1\"); break; default: printf(\"Test--->none\"); break; }}int main(){ int n = 1; int m = 2; switch (n) { case 1:++m; case 2:n++; case 3: switch (m) //m=3,无匹配结果 { case 1:n++; case 2:m++; n++; break; } case 4:m++; break; default: break; } printf(\"n=%d,m=%d\\n\",n,m);}/*************************************break和continuebreak:直接结束循环continue :结束本次循环,直接接入下一次循环**************************************/int main(){ int i = 1; while (i<=10) { if (i == 5) break; //此时i=5,结束循环,不再打印5出来 printf(\"%d \",i); i++; }}int main(){ int i = 1; while (i <= 10) { i++; //只有放在前面时程序才可以正常执行,得到想要的结果 if (i == 5) continue; //直接进入下一趟循环 printf(\"%d \", i); //i++; 放在这里没有办法执行,因为当i=5时,直接进入到下一趟循环,不会执行到i++这里,也就是i的值没有改变的机会,可能程序会进入死循环了 }}运行结果:2 3 4 6 7 8 9 10 11 //连续输入int main(){ //CTRL+Z结束 char ch; while ((ch = getchar()) != EOF) //EOF -->end of file 文件结束标志 { putchar(ch); }}//实例:输入密码,等待确认,确认成功!int main(){ int ch = 0; int ret = 0; char password[] = { 0 }; printf(\"请输入密码:>\"); scanf(\"%s\",password); //如果不加下面的while,当输入空格时,没等到Y确认就直接输出Fail /* while ((ch = getchar()) != \'\\n\') { ; } */ printf(\"请确认(Y/N):>\"); ret = getchar(); if (ret == \'Y\') printf(\"OK!\\n\"); else printf(\"Fail\\n\");}//循环Test示例int main(){ int ch=0; while ((ch=getchar()) != EOF) { if (ch < \'0\' || ch > \'9\') continue; putchar(ch); }}
01.C语言学习(控制结构)
未经允许不得转载:爱站程序员基地 » 01.C语言学习(控制结构)