c语言-分支与跳转
分支与跳转
1、if 语句
一个条件
if(条件语句)
{满足条件执行的语句;
}
//只有一条执行语句的时候 {} 可以省略
两个条件
if(条件语句)
{满足条件执行的语句;
}
else
{除了 if 以及 else if 语句执行条件外,剩下的条件执行的语句;
}
两个以上的选择时用 else if
if(条件语句)
{满足条件执行的语句;
}
else if(条件语句)
{满足条件执行的语句;
}
else if(条件语句)
{满足条件执行的语句;
}
.
.
.
.
else
{除了 if 以及 else if 语句执行条件外,剩下的条件执行的语句;
}
*如果 if 和 else 被用在一段程序中,但是中间没有大花括号{},则 else 和最近的 if 进行配对
if(num>6)if(num<12)printf("**********");
elseprintf("+++++++++");
以上代码中, else 和最近的 if ( num < 12 ) 配对,也就是说 else 的条件是 num>=12

#include
int main(void)
{const int standard = 0;float temperature;int cold_days = 0, all_days = 0;printf("请输入最近的最低气温:\n");printf("以q结束输入\n");while (scanf("%f", &temperature) == 1){all_days++;if (temperature < standard){cold_days++;}}if (all_days != 0){printf("您一共输入了%d天的最低气温,其中低于0摄氏度的天数有%d\n,低温百分率为%.2f\n", all_days, cold_days,(float)cold_days/all_days);}//if (all_days == 0)else{printf("您没有成功输入温度!");}return 0;}
2、读写字符
ch = getchar( );与 scanf("%c",&ch); 效果相同
putchar(ch); 与 printf("%c",ch); 效果相同
#include
#define SPACE " "
int main(void)
{char ch ;ch = getchar();while(ch!='\n'){if(ch==SPACE)putchar(ch);elseputchar(ch+1);ch=getchar();}putchar(ch);return 0;
}
2.1 ctype.h系列字符函数
3、逻辑运算符
- 在最新的C99的标准中,添加 iso646.h 头文件可以通过 and 代替 && ,or 代替 || ,not 代替 !
include<iso646.h>
include<stdio.h>
int main(void)
{bool istrue = true;if(4>6 and 6<12){printf("不运行,为假");}
}
4、条件运算符 ? :
x= (y<0) ? -y : y ;
//与
if (y<0)x=-y;
elsex=y;
//表达的意义相同expression1 ? expression2 : expression3;
//如果expression1为真,输出expression2;如果expression1为假,输出expression3;
#include
definde COVERAGE 200 /*每罐漆可喷涂的面积*/
int main(viod)
{int sq_feet;int cans;printf("请输入涂漆的面积");while(scanf(%d,&sq_feet) == 1){cans = sq_feet / CAVERAGE;cans += ((sq_feet % CAVERAGE ==0)) ? 0 :1 ;printf ("你需要%d罐油漆",cans);printf("请输入下一个面积(q字母暂停)");}return 0;
}
5、循环辅助手段 continue 和 break
5.1、continue
作用:用在循环语句之中,不再执行 continue 后的语句,重新开始新的循环
对于 while 和 do while 循环,continue 语句执行后的动作是求循环判断式的值
count = 0;
while(count<10)
{ch = getchar();if(ch == '\n')continue ;putchar(ch);count++ ;
}// 除了换行符以外,该语句会读出并回显出10个字符,当输入为换行符时,continue 会跳过后面的语句,进行循环条件的判断,开始新的循环
对于 for 循环,continue 语句执行后的动作是先更新表达式的值,再求循环判断式的值
for(count=0;count<10;count++)
{ch = getchar();if(ch == '\n')continue ;putchar(ch);count++ ;
}
//在本段程序中,continue 执行后,会先执行 count++ ,然后进行判断 count < 10 , 因此输出的换行符也包括在回显之内
5.2、break
作用:循环中的break 语句导致程序终止它所在的循环,执行循环后的语句。

6、多重选择:switch 和 break
switch(expression)
{case label1 : statement1;//当 label1 满足 expression 时,执行statement1break; //当 case1 执行后跳出 Switchcase label2 :statement2;break;default : statement3;//如果没有和 expression 匹配的 label ,则执行该语句
}
如果 case 后没有 break 语句,则当 expression 匹配到 label 之后,后面的所有的case* 的 label 对应的statement 都会执行

#include
#include
int main(void)
{char ch;printf("请输入动物的首字母,我说出对应首字母的动物\n , 以 # 结束游戏");while((ch=getchar()) != '#'){if('\n' == ch)continue;if(islower(ch)){swich(ch){case 'a' :printf("argali\n");break;case 'b' :printf("babirusa\n");break;default:printf("我能不知道\n") }}elseprintf("请输入小写字母\n");while(getchar()!='\n') //忽略其它输入的字符continue;printf("请输入下一个小写字母\n")}printf("bye!");return 0;
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
