C语言第三节-顺序,分支,循环

循环结构

循环:在满足某个条件时,反复执行某程序段
       (循环条件)                 (代码段

    /*
    
    
循环:防止代码冗余、可读性差、容易出错
        
   */
   
 
#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 】之间的随机数
    //[10,30]之间的随机数  020+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" );
    printf("此次随机数的最大值:%d\n", max);


break
1、switch语句:跳出switch语句
2、循环体:跳出本层循环(通常与if连用)
continue
1、结束本次循环(continue后面的代码不再执行),进入下次循环(通常与if连用)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值