C語言第三節-順序,分支,循環

循環結構

循環:在滿足某個條件時,反覆執行某程序段
       (循環條件)                 (代碼段

    /*
    
    
循環:防止代碼冗餘、可讀性差、容易出錯
       
   */
   
 
#pragma mark-----while循環
   
//    條件表達式爲真,執行循環體
//    while (<#condition#>) {
//        <#statements#>
//    }
   
   
   
//條件始終成立的循環:死循環, 應該避免出現誤操作的死循環
   
//輸出30次我很棒
//    int i = 0;
//   
//    while (i < 30) {
//       
//        printf("i = %d", i);
//        printf("我很棒\n");
//        i++;
//       
//    }
   
   
//輸出100helloworld
   
   
//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)  原理:餘數 < 除數
   
//獲取【010】之間的隨機數
   
//    int number = arc4random() % 11;
//    printf("number = %d\n", number);

   
   
//使用while循環實現:輸出10個【020】之間的隨機數
   
//    int count = 0;
//   
//    while (count < 10) {
//       
//        int number = arc4random() % 21;
//        printf("%d ", number);
//       
//        count++;
//    }
   
   
//練習:使用while輸出N個【043】範圍內的隨機數,循環次數從控制檯輸入
   
//    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連用)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章