//
// main.m
// day03_Class_C
//
// Created by dllo on 15/11/26.
// Copyright © 2015年 dllo. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
#pragma mark - 知识点 1 循环结构
#if 1
// 顺序执行
// 按条件执行 -> 分支语句
// 重复执行 -> 循环结构
// 循环结构 :如果条件成立,就会重复执行相应的代码
// 实现循环结构的三种语法
// for
// while
// do...while
#endif
#pragma mark - 知识点 2 for 循环
#if 0
/* a b c
for (初始化语句; 条件语句; 循环控制语句) {
d
循环体
}
*/
// a - b 如果b成立 -d -c -b -d -c
// 直到b不成立,跳出for循环
for (int i = 0; i < 5; i++) {
printf(“a\n”);
}
// 把重复性代码当成循环体
#endif
#pragma mark - 练习一 - 打印1到100
#if 0
for (int i = 1 ; i < 101; i++) {
printf("%d\n", i);
}
#endif
#pragma mark - 练习二 - 练习十四
#if 0
// for (int i = 1; i < 6; i++) {
// printf("%d\n", i);
// }
// for (int i = 5; i > 0; i–) {
// printf("%d\n", i);
// }
// for (int i = 1; i < 101; i++) {
// printf("%d\n", i);
// }
// for (int i = 2; i < 101; i += 2) {
// printf("%d\n", i);
// }
// for (int i = 1; i < 101; i += 2) {
// printf("%d\n", i);
// }
// for (int i = 7; i < 101; i++) {
// if (i % 7 == 0) {
// printf("%d\n",i);
// }
// }
// for (int i = 0; i < 101; i++) {
// if (i - i / 10 * 10 != 7) {
// printf("%d\n",i);
// }
// }
// for (int i = 0 ; i < 101; i++) {
// if (i / 10 != 7) {
// printf("%d\n",i);
// }
// }
// for (int i = 0; i < 101; i++) {
// if (i % 7 != 0 && i - i / 10 * 10 != 7 && i / 10 != 7) {
// printf("%d\n",i);
// }
// }
int y = 0;
for (int i = 0; i < 101; i++) {
y = y + i;
if (i == 100) {
printf("%d\n",y);
}
}
int x = 0;
for (int i = 0; i < 101; i += 2) {
x = x + i;
if (i == 100) {
printf("%d\n",x);
}
}
int z = 0;
for (int i = 1; i < 101; i = i + 2) {
z = z + i;
if (i == 99) {
printf("%d\n",z);
}
} //计数器
int w = 0;
for (int i = 200; i < 301; i++) {
if ( i % 2 == 0) {
w = w + 1;
}
}
printf("%d\n",w);
#endif
#pragma mark - 知识点 3 随机数 求最大 最小
#if 0
//生成[a,b]范围内的数
//int a = 0;
//int b = 0;
//int result = arc4random() % (b - a + 1) + a;
int a = 0;
for (int i = 0; i < 11 ; i++) {
int result = arc4random() % (40 - 20 + 1) + 20;
printf("%d\n",result);
if (result > a) {
a = result;
}
}
printf(".....%d\n",a);
int min = 31;
for (int i = 0; i < 11; i++) {
int result = arc4random() % (30 - 10 + 1) + 10;
printf("%d\n",result);
if (result < min) {
min = result;
}
}
printf(".....%d\n",min);
#endif
#pragma mark - 知识点 4 while 循环
#if 0
// while (条件表达式) {
// 循环体
// }
// int a = 1;
// while (a < 101) {
// printf("%d\n",a);
// a++;
// }
int b = 1; //初始化语句
int c = 0;
while (b < 101) { //条件
c += b;//循环体
b++; //循环控制变量语句
}
printf("%d\n",c);
#endif
#pragma mark - 知识点 5 do…while
#if 0
// do {
// statements
// } while (condition); 无论条件是否成立,循环体至少会执行一次
int a = 0;
do {
a++;
printf("%d\n",a);
} while (a < 5);
#endif
#pragma mark - 知识点 6 for嵌套
#if 0
// for (int x = 1; x < 4; x++) {
//
// for (int i = 1; i < 4; i++) {
// printf(“1 “);
// }
// printf(”\n”);
// }
// for (int x = 1; x < 4; x++) {
// for (int i = 1; i < 4; i++) {
// if (i <= x ) {
// printf("%d “, i);
// }
// }
// printf(”\n");
// }
// for (int x = 1; x < 4; x++) {
// for (int i = 3; i > x - 1; i–) {
//
// printf("%d “, i);
//
//
//
// }
// printf(”\n");
// }
// for (int x = 1; x < 10; x++) {
//
// for (int y = 0; y < 10; y++) {
//
// for (int z = 0; z < 10; z++ ) {
// printf("%d%d%d",x,y,z);
// printf("\n");
// }
// }
// }
// for (int x = 1; x < 10; x++) {
//
// for (int y = 1; y <= x; y++) {
// printf("%dx%d=%2d “,y,x,x * y);
// }
// printf(”\n");
// }
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2 - i; j++) {
printf(" ");
}
for (int x = 0; x < i + 1 ; x++ ){
printf("* ");
}
printf("\n");
}
// for (int i = 1; i < 4; i++) {
// printf("1 ");
// }
#endif
#pragma mark - 知识点 7 break 和 continue
#if 0
// int a = 0;
// for (int i = 1; i < 101; i++) {
// if (i % 2 != 0) {
//
// continue; //跳出本次循环,contin后面的代码都不执行,直接进入下一次循环
// }
// a += i;
// }
// printf("%d\n",a);
//随机生成20 [35,45]之间的数
// int a = 0;
for (int i = 0; i < 21 ; i++) {
int result = arc4random() % (45 - 35 + 1) + 35;
if (result == 38) {
printf(“第%d个数是%d\n”,i + 1,result);
break; //跳出本层整个for循环
}
printf("%d\n",result);
}
#endif
#pragma mark -限时代码
#if 1
int a = 0;
for (int i = 0; i < 11 ; i++) {
int result = arc4random() % (40 - 20 + 1) + 20;
printf("%d\n",result);
if (result > a) {
a = result;
}
}
printf(".....最大值%d\n",a);
#endif
return 0;
}