C/C++知识点之iOS学习之C语言Day03
小标 2018-11-01 来源 : 阅读 846 评论 0

摘要:本文主要向大家介绍了C/C++知识点之iOS学习之C语言Day03,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了C/C++知识点之iOS学习之C语言Day03,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

1、while循环

 
    while (循环条件) {        循环体;    }    // 1.定义循环变量    int time = 1;    // 2.循环条件    while (time <= 3) {        // 3.循环体        printf("不能玩手机\n");        // 4.循环增量        time++;    }            // 定义循环变量    int quan = 1;    // 循环条件    while (quan <= 10) {        // 循环体        printf("跑圈%d\n", quan);        // 循环增量        quan++;    }       

1  练习:打印1-100之间所有的数
2     int number = 1;
3     while (number <= 100) {
4         printf("%-4d", number);
5         number++;
6     }


 1   
 2 
 3      打印1-100之间所有的奇数
 4     int a1 = 1;
 5     while (a1 <= 100) {
 6         if (a1 % 2 != 0) {
 7             printf("奇数为:%d\n",a1);
 8         }
 9         a1++;
10     }

 
      2、随机数
    arc4random()     原理: 余数 < 除数     [0, n] arc4random() % (n + 1)     [a, b] arc4random() % (b - a + 1)+a             [0, 9]    int random = arc4random() % 10;    printf("random = %d\n", random);        // [0, 10]    int random1 = arc4random() % 11;    printf("random1 = %d\n", random1);        // [0, 20]    int random2 = arc4random() % 21;    printf("random2 = %d\n", random2);        // [1, 10] -- [0, 9]+1    int random3 = arc4random() % 10 + 1;    printf("random3 = %d\n", random3);        // [10, 20]-- [0, 10] + 10    int random4 = arc4random() % 11 + 10;    printf("random4 = %d\n", random4);        

 1 练习: 随机产生10个数, 范围[0, 10]
 2     
 3     int number = 1;
 4     while (number <= 10) {
 5         
 6         // 打印随机数 [0, 10]
 7         int random = arc4random() % 11;
 8         printf("%d ", random);
 9         
10         number++;
11     }
12     
13      练习:用while打印10个随机数(范围为10~30),求最大值和最小值。
14     
15     int number = 1;
16     int max = 0;
17     int min = 30;
18     while (number <= 10) {
19         // 打印随机数(范围为10~30)
20         int random = arc4random() % 21 + 10;
21         printf("%4d", random);
22         if (max < random) {
23             max = random;
24         }
25         if (min > random) {
26             min = random;
27         }
28         
29         number++;
30     }
31     printf("max = %d\n", max);
32     printf("min = %d\n", min);
33     

 
3、 break, continue         break:在switch...case中,结束当前的case分支      在循环中,遇到break,break后面的语句不再执行并结束整个循环     continue:在循环中遇到continue,后面的语句不再执行并结束本次循环        int count = 1;    while (count <= 20) {                if (count == 15) {            printf("相中,走啦%d\n", count);            break;        }                if (count == 8) {            printf("前女友,跳过\n");            count++;            continue;        }                printf("count = %d\n", count);                count++;    }        

 1 练习:打印1-20之间所有的数,如果是7,不打印,如果是17,17和后面的数不再打印
 2     
 3     int a = 0;
 4     while (a < 20) {
 5         a++;
 6         if (a == 7) {
 7             a++;
 8             continue;
 9         }
10         
11         if (a == 17) {
12             break;
13         }
14         
15         printf("%d ", a);
16         a++;
17     }

 
 
    4、do...while        do {        循环体    } while (循环条件);         定义循环变量    int a = 1;    do {                a++;            } while (a > 10);    printf("a = %d\n", a);        while (a > 10) {        a++;    }    printf("a = %d\n", a);    5、 for循环       

1  打印1-5之间所有的数
2     
3     int a = 1;
4     
5     for (;a <= 5;) {
6         printf("%d ", a);
7         
8         a++;
9     }

 
 
         (定义循环变量 ; 循环条件;循环增量)    for (int a = 1;a <= 5;a++) {        printf("%d ", a);    }        

 1 用for循环打印1-100之间所有的数
 2     int i = 0;
 3     for (int i = 1; i <= 100; i++){
 4         printf("%d", i);
 5         printf("  ");
 6     }
 7 
 8     printf("i = %d\n", i);
 9     
10     
11      用for循环打印1-100之间所有的偶数
12     for (int i = 1; i <= 100; i++) {
13         if (i % 2 == 0) {
14             printf("%d ", i);
15         }
16     }
17     
18     
19      用for循环打印出1~100之间7的倍数。
20     for (int i = 1; i <= 100; i++) {
21         if (i % 7 == 0) {
22             printf("%4d", i);
23         }
24     }
25     
26      用for循环打印出1~100之间个位为7的数。
27     for (int i = 1; i<= 100; i++) {
28         if (i % 10 == 7) {
29             printf("%4d", i);
30         }
31     }
32     
33      用for循环打印出1~100之间十位为7的数。
34     for (int i = 1; i <= 100; i++) {
35         if (i / 10 == 7) {
36             printf("%4d", i);
37         }
38     }
39     
40     
41      用for循环打印出1~100之间既不是7的倍数并且也不包含7的数。
42     for (int i = 1; i <= 100; i++) {
43         if (i % 7 != 0 && i % 10 != 7 && i / 10 != 7) {
44             printf("%-4d", i);
45         }
46     }

 
 
    6、循环嵌套    

 1     /*
 2      1 2 3 4
 3      1 2 3 4
 4      1 2 3 4
 5      */
 6     
 7      控制行数
 8     for (int i = 1; i <= 3; i++) {
 9         // 控制每一行要打印的内容
10         for (int j = 1; j <= 4; j++) {
11             printf("%d ", j);
12         }
13         printf("\n");
14     }

 
    

 1     /*
 2      1
 3      1 2
 4      1 2 3
 5      1 2 3 4
 6      1 2 3 4 5
 7      */
 8     
 9      控制行数
10     for (int i = 1; i <= 5; i++) {
11         // 每一行要打印的内容
12         for (int j = 1; j <= i; j++) {
13             printf("%d ", j);
14         }
15         printf("\n");
16     }

 
 

 1    
 2     /*
 3      1 2 3 4 5
 4      1 2 3 4
 5      1 2 3
 6      1 2
 7      1
 8      */
 9     
10      控制行数
11     for (int i = 5; i >= 1; i--) {
12         // 每一行要打印的内容
13         for (int j = 1; j <= i; j++) {
14             printf("%d ", j);
15         }
16         printf("\n");
17 
18     }

 
 
   

 1      打印乘法口诀表
 2     
 3      控制行数
 4     for (int i = 1; i <= 9; i++) {
 5         
 6         // 控制打印的方格
 7         for (int j = 1; j <= i; j++) {
 8             
 9             printf("%dx%d=%d ", j, i, j*i);
10         }
11         printf("\n");
12     }

 
 
        

 1 打印三个数字(0 - 9)的组合可能(组合成三位数)。
 2     
 3      控制百位数
 4     for (int i = 1; i <= 9; i++) {
 5         
 6         // 控制十位数
 7         for (int j = 0; j <= 9; j++) {
 8             
 9             // 控制个位数
10             for (int k = 0; k <= 9; k++) {
11                 
12                 printf("%d ", i*100+j*10+k);
13             }
14         }
15     }

 
 
     总结:     for循环通常用于知道循环次数的情况下使用(常用)     while:不明确循环次数,知道循环结束的标识

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程