iOS学习之C语言Day03

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:不明确循环次数,知道循环结束的标识

时间: 2024-08-25 09:42:31

iOS学习之C语言Day03的相关文章

iOS学习笔记---c语言第九天

高级指针 指向结构体变量的指针,称为结构体指针 可以使用->指向内容. %p打印地址 void pLenth(cPoint *p1,cPoint *p2) //求两点间的距离  用的开方函数sqrt()和平方函数pow(,) { float a = sqrt(pow((p1->x-p2->x), 2)+pow((p1->y-p2->y), 2)); printf("两点距离为%.2f\n",a); } //main.m中代码 #import <Fou

ios学习笔记---c语言第二天

一.bool布尔类型    c语言没有bool类型,oc里有bool类型 是一种非真即假的数据类型,布尔类型的变量只有yes和no两个值.yes表示表达式是真,no表示表达式是假. 在c语言中认为非0即为真. 分支语句中常用bool值做判断,判断执行if语句还是else语句. 循环结构中,也常使用bool值做判断,判断是否要执行循环. 注意事项: #define yes 1 #define no 0 计算机在识别时,yes就替换成1,no就替换成0. 二.关系运算符 >   >=   <

iOS学习笔记---oc语言第三天

继承.初始化方法 一.继承 继承的上层:父类  继承的下层:子类 继承是单向的,不能相互继承 继承具有传递性:A继承于B,B继承于C,A具有B和C的特征和行为 子类能继承父类全部的特征和行为(私有变量也继承过来了,只是不能访问) 面向对象提供了继承语法.能大大简化代码,把公共的方法和实例对象写在父类里.子类只需要写自己独有的实例变量和方法即可 继承既能保证类的完整,又能简化代码 继承特点 oc中只允许单继承 没有父类的类称为根类,oc中得根类是NSObject(祖宗) 继承的内容:所有的实例变量

IOS学习笔记---C语言第四天

1 //?生成2个数组,每个数组都有10个元素,元素取值范围20-40之间,数组对应元素相 加,放到另外?一个数组中 2 #import <Foundation/Foundation.h> 3 4 int main(int argc, const char * argv[]) 5 { 6 7 int num1[10]={0},num2[10]={0},num3[10]={0}; 8 for (int i = 0; i<10; i++) { 9 num1[i]=arc4random()%2

iOS学习笔记---oc语言第五天

字典,数组 ,集排序 一.字典类 存储以下数据 name:张三; sex:男;age:18 film:风暴; playcount:10000次;price:60元 字典类用于保存具有映射关系(key-value对)的数据 对于“name:张三”来讲,key就是“name”,key对应的value是“张 三” 一个key-value对认为是一个元素(实体),字典是存储key-value对 的容器. 特点: 与数组不同,数组靠下标存取数据,数组的下标是唯一的. 字典靠key存取元素.key不能重复,

iOS学习笔记---oc语言第六天

Block .数组高级 block本质上就是匿名函数(没有名称的函数) block语法和函数指针很相似 回顾函数 函数:C语?中,实现某一类功能的代码段. 完整的函数包含两部分:函数声明.函数定义 函数声明,即函数原型.例如:int sum(int x,int y);具有两个整型参 数,一个整型返回值的函数. 函数定义,即函数实现.例如:int sum(int x,int y){     retrun x + y;     } 回顾函数指针 函数指针(变量):存放函数地址(函数名)的指针变量.

iOS学习笔记---oc语言第四天

字符串 数组 一.使用苹果帮助文档 学会使?用苹果帮助?文档是开发者的?一项技能 Inherits from 继承?自 Conforms to 遵循什么协议 Framework 属于哪个框架 Availability 什么时候可?用的 Declared in 声明在什么头文件?里 Related documents 相关文档 Sample code ?示例代码 快速打开帮助文档 在代码中,将?鼠标停留在 类名或者?法名上,option+?鼠标左键,点击 Reference的超链接进?入帮助?文档

iOS学习笔记---oc语言第二天

实例变量与方法 一.实例变量的可见度 二.方法 oc中的方法分两种:类方法和实例方法 类方法:只能类使用 eg:+ (id)alloc  注:类方法中不能使用实例变量 实例方法:只能对象使用,eg:- (void)sayHi iOS学习笔记---oc语言第二天

iOS学习笔记---c语言第八天

指针 首先将变量a的地址存放在另一个变量中,比如存放在变量b中,然后通过变量b来间接引用变量a,间接读写变量a的值.用来存放变量地址的变量,就称为"指针变量" int *p=null;   初始值:null恒等于0          变量类型:整型指针 int * 指针变量所占字节数只于操作系统有关 指针类型转换符%p printf("p = %p\n",p); //c语言定义指针的时候,喜欢使用p,q,r等. int *p = NULL; //我们通常所说的指针,