C/分支结构
< C
跳到导航
跳到搜索
if[编辑]
条件判断语句,条件成立则执行后续语句(或者大括号内语句)。
如果条件成立,则执行后面的语句,如果不成立,则执行else后面的语句(如果不设置,则不处理)。
//for Ex.
#include <stido.h>
main()
{
int a=1,b=2,c;
c=a+b;
if(3==c) /*如果结果为3,则打印字符串*/
{
printf("The solution is right!\n");
}
return 0;
}
输出结果:
The solution is right!
switch[编辑]
//for Ex.
#include <stidio.h>
main()
{
int score=3; /*以5分制成绩为例*/
switch(c) /*如果结果为3,则打印字符串*/
{
case 5 : printf("Great!");
case 4 :
case 3 : printf("Your score is %d \n",c); break;
default: printf("You may need to work harder");
}
return 0;
}