C语言 | 04 选择结构程序设计

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36292543/article/details/88640482

4.1 if 语句

4.1.1 if语句

#include <stdio.h>

 

int main()

{

int a = 1;

int b = 2;

 

if (a > b)

{

printf("%d\n", a);

}

 

return 0;

} 

4.1.2 if…else语句

#include <stdio.h>

int main()

{

int a = 1;

int b = 2;

 

if (a > b)

{

printf("%d\n", a);

}

else

{

printf("%d\n", b);

}

return 0;

}

4.1.3 if…else if…else语句

#include <stdio.h>

 

int main()

{

unsigned int a;

scanf("%u", &a);

 

if (a < 10)

{

printf("个位\n");

}

else if (a < 100)

{

printf("十位\n");

}

else if (a < 1000)

{

printf("百位\n");

}

else

{

printf("很大\n");

}

 

return 0;

}

 

4.2 switch语句

4.2.1 switch语句

#include <stdio.h>

 

int main()

{

char c;

c = getchar();

 

switch (c) //参数只能是整型变量

{

case '1':

printf("OK\n");

break;//switch遇到break就中断了

case '2':

printf("not OK\n");

break;

default://如果上面的条件都不满足,那么执行default

printf("are u ok?\n");

}

return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_36292543/article/details/88640482
今日推荐