C语言学习3:if ,for,do..while,continue和break,goto,swtich,输入输出缓存区,随机数产生

1,if语句

 1 #include <stdio.h>
 2 
 3 /*
 4  *if (expr)
 5  *    stat
 6  *else if (expr)
 7  *    stat
 8  *else
 9  *    stat
10  *
11  *expr: expression
12  *stat: statement -> expr; | { ... } 
13  */
14 
15 #define EPISILON    0.0000001
16 
17 int main(void)
18 {
19     double d = 5.0;
20 
21     // 对浮点数进行判断, 最好给一个判定范围
22     // 而不是直接使用 ==
23     if (5.0 - EPISILON < d && d < 5.0 + EPISILON)
24     {
25         printf("d == 5.0
");    
26     }
27 
28     unsigned u = -1;
29     int i = -1;
30     if (u > 0)
31     {
32         printf("u > 0 !
");    
33     }
34 
35     if (i > 0)
36         printf("i > 0 @
");    
37     else
38         printf("i < 0 !
");    
39 
40     int a;
41     printf("pls input a num: ");
42     scanf("%d", &a);
43     if (a > 5)
44     {
45         printf("the num you inputed is bigger than 5!
");    
46     }
47     else if (a < 3)
48     {
49         printf("the num you inputed if less than 3!
");    
50     }
51     else
52     {
53         printf("the num is 3 or 4 or 5.
");    
54     }
55 
56     return 0;
57 }

结果:

d == 5.0
u > 0 !
i < 0 !
pls input a num: 3
the num is 3 or 4 or 5.
2,for语句

 1 #include <stdio.h>
 2 
 3 /*
 4  *for (init_expr; cond_expr; update_expr)
 5  *    stat
 6  *
 7  *--> init_expr 
 8  *    --> cond_expr(if true,...)
 9  *        --> stat 
10  *            --> updata_expr
11  *                --> cond_expr ...
12  */
13 
14 int main(void)
15 {
16     int i;
17 
18     for (i = 0; i < 5; i++)
19         printf("i = %d
", i);
20 
21     printf("for loop over. after for loop:
");
22 
23     printf("i = %d
", i);
24 
25     return 0;
26 }

结果:

i = 0

i = 1

i = 2

i = 3

i = 4

for loop over. after for loop:

i = 5

3,while ....do 语句

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int i;
 6 
 7     i = 0;
 8     while (i < 5)
 9     {
10         printf("i = %d
", i);    
11         i++;
12     }
13 
14     printf("after while, i = %d
", i);
15 
16     // 无论如何会执行一次.
17     do {
18         printf("i = %d
", i);    
19         i++;
20     } while (i < 5);
21 
22     printf("after do while, i = %d
", i);
23 
24     return 0;
25 }

结果:

i = 0

i = 1

i = 2

i = 3

i = 4

after while, i = 5

i = 5

after do while, i = 6

4,continue和break的区别

 1 #include <stdio.h>
 2 
 3 // continue 结束本次循环
 4 // break 跳出本层循环
 5 int main(void)
 6 {
 7     int i, j;
 8 
 9     for (i = 0; i < 10; i++)
10     {
11         if (i == 3)
12             continue;
13 
14         j = 0;    
15         while (1)
16         {
17             printf("%d ", j);    
18             if (j >= i)
19                 break;
20             j++;
21         }
22         putchar('
');
23     }
24 
25     return 0;
26 }

结果:

0

0 1

0 1 2

0 1 2 3 4

0 1 2 3 4 5

0 1 2 3 4 5 6

0 1 2 3 4 5 6 7

0 1 2 3 4 5 6 7 8

0 1 2 3 4 5 6 7 8 9

少了0 1 2 3这一排就是continue的作用

5,goto跳转

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int i = 0;
 6 
 7 labeli:
 8     
 9     printf("i = %d
", i);
10     i++;
11     if (i < 5)
12         goto labeli;
13 
14     return 0;
15 }

结果:goto是无条件跳转

i = 0

i = 1

i = 2

i = 3

i = 4

6,swtich语句

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     char i = 0;
 6 
 7     printf("pls input a char: ");
 8     scanf("%c", &i);
 9 
10     printf("i = %d
", i);
11     switch (i)
12     {
13         case 'a':
14         case 'A':
15             printf("you inputed a or A.
");    
16             break;
17         case 'b':
18         case 'B':
19             printf("you inputed b or B.
");    
20             break;
21         case 'c':
22         case 'C':
23             printf("you inputed c or C.
");    
24             break;
25         default:
26             break;
27     }
28 
29     return 0;
30 }

结果:多分支选择语句

pls input a char: a

i = 97

you inputed a or A.

7,输出缓存区效果

 1 #include <stdio.h>
 2 
 3 /*
 4  * 0      1       2
 5  * stdin, stdout, stderr
 6  * */
 7 
 8 /*
 9  *输出缓冲区的刷新条件:
10  *1. 缓冲区满
11  *2. 遇到'
'
12  *3. 强制刷新(fflush(stdout))
13  */
14 
15 int main(void)
16 {
17     printf("hello");
18 
19     fflush(stdout);
20 
21     while (1)
22     {
23         sleep(1);
24     }
25 
26     return 0;
27 }

结果:如果没有fflush(stdout);语句,则hello不会显示出来,如果变成printf("hello ");hello就可显示了,也就是上面的printf有问题的话,下面要刷新

8,scanf输入以及输入缓存区效果

 1 #include <stdio.h>
 2 
 3 /*
 4  *scanf 根据格式去标准输入缓冲区取字符/字符串
 5  *如果该字符/字符串不符合scanf 参数指定的格式
 6  *则scanf 直接返回(不取东西)
 7  *scanf的返回值代表取的参数个数.
 8  *如果scanf的返回值为0, 代表scanf没有取到东西.
 9  */
10 int main(void)
11 {
12     char ch;
13     int a;
14 
15     printf("pls input a int: ");
16     scanf("%d", &a);
17     printf("a = %d
", a);
18 
19     printf("------------
");
20 
21     /* 清空标准输入缓冲区 */
22     while (getchar() != '
')
23         ;
24 
25     printf("pls input a char: ");
26     scanf("%c", &ch);
27     printf("ch = %c
", ch);
28 
29     return 0;
30 }

结果:如果没有清空,就无法输入了。对于下面只接受一个是因为下面scanf只允许接受一个字符

有清空

pls input a int: 54
a = 54
------------
pls input a char: 56
ch = 5
无清空

pls input a int: 54
a = 54
------------
pls input a char: ch =


9,输入输出简单应用,选择

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int quit = 0;
 6     int op;
 7 
 8     int ret;
 9 
10     printf("欢迎来到xxx系统.
");
11     while (!quit)
12     {
13     retry:
14         printf("==> 1. a 操作.
");    
15         printf("==> 2. b 操作.
");    
16         printf("==> 3. c 操作.
");    
17         printf("==> 4. d 操作.
");    
18         printf("==> 5. 退出.
");
19         printf("请选择操作[1 - 5]: ");
20 
21         ret = scanf("%d", &op);
22         /* 清空标准输入缓冲区 */
23         while (getchar() != '
')
24             ;
25         if (ret == 0)
26         {
27             printf("姿势不对, 再试一次.
");    
28             goto retry;
29         }
30 
31         switch (op)
32         {
33             case 1:
34                 printf("----------a------------
");
35                 break;
36             case 2:
37                 printf("----------b------------
");
38                 break;
39             case 3:
40                 printf("----------c------------
");
41                 break;
42             case 4:
43                 printf("----------d------------
");
44                 break;
45             case 5:
46                 quit = 1;
47                 break;
48             default:
49                 break;
50         }
51     }
52 
53     return 0;
54 }

结果:

欢迎来到xxx系统.
==> 1. a 操作.
==> 2. b 操作.
==> 3. c 操作.
==> 4. d 操作.
==> 5. 退出.
请选择操作[1 - 5]: 1
----------a------------
==> 1. a 操作.
==> 2. b 操作.
==> 3. c 操作.
==> 4. d 操作.
==> 5. 退出.
请选择操作[1 - 5]: 2
----------b------------
10,随机数的产生

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int i;
 6 
 7     int sec;
 8 
 9     sec = time(NULL);
10     printf("1970 --> now has %d sec.
", sec);
11 
12     // 给 rand 做一个随机数种子
13     srand(sec);
14 
15     i = rand() % 100;
16     printf("i = %d
", i);
17 
18     i = rand() % 100;
19     printf("i = %d
", i);
20 
21     return 0;
22 }

结果:time函数是记载时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数

          再用时间做随机数种子,因为每运行一次,时间改变一次,每次随机就不一样了

1970 --> now has 1377135061 sec.

i = 35

i = 49

原文地址:https://www.cnblogs.com/will-boot/p/3278736.html