1.if语句与switch语句的对比
练习:输⼊任意⼀个整数值,计算除3之后的余数 如果用if语句完成,如下:1 #include <stdio.h>
2 int main()
3 {
4 int n = 0;
5 scanf("%d", &n);
6 if(n%3 == 0)
7 printf("整除,余数为0\n");
8 else if(n%3 == 1)
9 printf("余数是1\n");
10 else
11 printf("余数是2\n");
12 return 0;
13 }
如果用switch语句完成,如下:
1 #include <stdio.h>
2 int main()
3 {
4 int n = 0;
5 scanf("%d", &n);
6 switch(n%3)
7 {
8 case 0:
9 printf("整除,余数为0\n");
10 break;
11
12 case 1:
13 printf("余数是1\n");
14 break;
15
16 case 2:
17 printf("余数是2\n");
18 break;
19 }
20 return 0;
21 }
2.if语句与while语句的对比
if语句这样写:
1 #include <stdio.h>
2 int main()
3 {
4 if(1)
5 printf("hehe\n"); //if后边条件满⾜,打印⼀次hehe
6
7 return 0;
8 }
while语句这样写:
1 #include <stdio.h>
2
3 int main()
4 {
5 while(1)
6 printf("hehe\n"); //while后边的条件满⾜,死循环的打印hehe
8 return 0;
8 }
3.while循环语句与for循环语句的对比
while循环这样写:
1 #include<stdio.h>
2 int main()
3 {
4 int i = 1; //1.初始化
5 while(i <= 10) //2.判断
6 {
7 printf("%d ",i);
8 i = i + 1; //3.调整
9 }
10 return 0;
11}
for循环这样写:
1 #include<stdio.h>
2 int main()
3 {
4 int i = 0;
5 for(i = 1;i <= 10; i++) //(1.初始化;2.判断;3.调整 )
6 {
7 printf("%d ",i);
8
9 }
10 return 0;
11}
标签:语句,余数,int,while,switch,printf,易懂,include
From: https://blog.csdn.net/2401_87522762/article/details/143030642