循环结构
循环:在满足某个条件时,反复执行某程序段
(循环条件) (代码段)
/*
循环:防止代码冗余、可读性差、容易出错
循环:防止代码冗余、可读性差、容易出错
*/
#pragma mark-----while循环
// 条件表达式为真,执行循环体
// while (<#condition#>) {
// <#statements#>
// }
//条件始终成立的循环:死循环, 应该避免出现误操作的死循环
//输出30次我很棒
// int i = 0;
//
// while (i < 30) {
//
// printf("i = %d", i);
// printf("我很棒\n");
// i++;
//
// }
//输出100次helloworld
//count循环变量:控制循环的次数
// int count = 0;
//
// //()内称为循环条件 结果只有两个: 真、假
// while (count < 100) {
//
// printf("count = %d\n", count);
//
// printf("hello world\n");
//
// //循环增量控制
// count++;
// }
// int count = 1;
//
// while (count <= 10) {
// printf("%d ", count++);
// }
//练习:输出95 到 80之间的数
// int count = 95;
//
// while (count >= 80) {
// printf("%d ", count--);
// }
// 用while打印出 1~100之间7的倍数
// int count = 1;
//
// while (count <= 100) {
//
// if (count % 7 == 0) {
// printf("%d ", count);
// }
//
// count++;
// }
// 用while打印出 1~100之间个位为7的数。
// int count = 1;
//
// while (count <= 100) {
//
// if ((count - 7) % 10 == 0) {
//
// printf("%d ", count);
// }
//
// count++;
// }
// 用while打印出 1~100之间十位为7的数。
// 用while打印出 1~100之间不是7的倍数并且不包含7的数
// int count = 1;
//
// while (count <= 100) {
//
// if (count % 7 != 0 && (count % 10 != 7) && (count / 10 != 7)) {
// printf("%d ", count);
//
// }
// count++;
// }
//练习:计算1~5之间所有数的和
int count = 1;
int sum = 0;
while (count <= 5) {
sum = sum + count;//sum += count;
count++;
}
printf("sum = %d\n", sum);
return
0;
随机数
#pragma mark------随机数
//随机数 arc4random() 返回一个随机数,没有范围限制,是整数
//如果要随即一个【a,b】范围内的数
//公式:arc4random() % (b - a + 1 ) + a
//获取一个[0, n]之间的随机数
// 公式:arc4random() % (n + 1) 原理:余数 < 除数
//获取【0,10】之间的随机数
// int number = arc4random() % 11;
// printf("number = %d\n", number);
//使用while循环实现:输出10个【0,20】之间的随机数
// int count = 0;
//
// while (count < 10) {
//
// int number = arc4random() % 21;
// printf("%d ", number);
//
// count++;
// }
//练习:使用while输出N个【0,43】范围内的随机数,循环次数从控制台输入
// int n = 0;
// int count = 0;
//
// printf("输入循环次数:");
// scanf("%d", &n);
//
// while (count < n) {
//
//// int number = arc4random() % 44;
//// printf("%d ", number);
// printf("%d ", arc4random() % 44);
//
// count++;
// }
//获取【a,b】之间的随机数
//随机数 arc4random() 返回一个随机数,没有范围限制,是整数
//如果要随即一个【a,b】范围内的数
//公式:arc4random() % (b - a + 1 ) + a
//获取一个[0, n]之间的随机数
// 公式:arc4random() % (n + 1) 原理:余数 < 除数
//获取【0,10】之间的随机数
// int number = arc4random() % 11;
// printf("number = %d\n", number);
//使用while循环实现:输出10个【0,20】之间的随机数
// int count = 0;
//
// while (count < 10) {
//
// int number = arc4random() % 21;
// printf("%d ", number);
//
// count++;
// }
//练习:使用while输出N个【0,43】范围内的随机数,循环次数从控制台输入
// int n = 0;
// int count = 0;
//
// printf("输入循环次数:");
// scanf("%d", &n);
//
// while (count < n) {
//
//// int number = arc4random() % 44;
//// printf("%d ", number);
// printf("%d ", arc4random() % 44);
//
// count++;
// }
//获取【a,b】之间的随机数
//[10,30]之间的随机数
【0,20】+10
//练习:用户从控制台
输入一个n,用while打印n个随机数(范围为30~70),找出n个随机数中的最大值。
int n = 0;
int count = 0;
int max = 0; // 存放临时最大值
printf("输入打印个数:");
scanf("%d", &n);
while (count < n) {
//求随机数 30~70 0~40+30
int number = arc4random() % 41 + 30;
printf("%d ", number);
//比较判断当前的随机数是否是最大值
if (max < number) {
max = number;
}
count++;
}
printf("\n");
int n = 0;
int count = 0;
int max = 0; // 存放临时最大值
printf("输入打印个数:");
scanf("%d", &n);
while (count < n) {
//求随机数 30~70 0~40+30
int number = arc4random() % 41 + 30;
printf("%d ", number);
//比较判断当前的随机数是否是最大值
if (max < number) {
max = number;
}
count++;
}
printf("\n");
printf("此次随机数的最大值:%d\n",
max);
break
1、switch语句:跳出switch语句
2、循环体:跳出本层循环(通常与if连用)
continue
1、结束本次循环(continue后面的代码不再执行),进入下次循环(通常与if连用)