IOS學習筆記---C語言第三天

來源:互聯網
上載者:User

標籤:style   class   blog   code   java   http   

迴圈結構 : while迴圈   do...while迴圈(幾乎不用)      for迴圈(使用最多)

特點:在給定的條件成立時,反覆執行某程式段,直到條件不成立為止。

給定的條件為迴圈條件,反覆執行的程式段位迴圈體。

一、while迴圈

while(條件運算式){

  迴圈語句;

}

 

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){    int i = 0;    while (i<10) {        printf("hello,word!\n");        i++;    }    return 0;}
 1 #import <Foundation/Foundation.h> 2  3 int main(int argc, const char * argv[]) 4 { 5 //用while列印出1~100之間7的倍數。 6 //    int i = 1,d; 7 //    while (i<=100) { 8 //        d = i%7; 9 //        if (d == 0 ) {10 //            printf("%d\t",i);11 //        }12 //        13 //        i++;14 //    }15 //用while列印出1~100之間個位為7的數16 //    int i = 1,d;17 //    while (i<=100) {18 //        d = i%10;19 //        if (d == 7 ) {20 //            printf("%d\t",i);21 //        }22 //23 //        i++;24 //    }25     //用while列印出1~100之間?十位為7的數26 //    int i = 1,d;27 //    while (i<=100) {28 //        d = i/10;29 //        if (d == 7 ) {30 //            printf("%d\t",i);31 //        }32 //        33 //        i++;34 //    }35     //用while列印出1~100之間不是7的倍數並且 不包含7的數36     int i = 1,d,a,b;37     while (i<=100) {38         d = i/10;39         a = i%7;40         b = i%10;41         if (d != 7 &&a!=0 &&b!=7) {42             printf("%d\t",i);43         }44         45         i++;46     }47 48     return 0;49 }

隨機數 arc4random()-----返回一個偽隨機數

?
1 2 //得到0到30之間的數     printf("%d",arc4random()%31);

//得到10到40之間的數

    printf("%d",arc4random()%31+10);

 取(m,n)之間的隨機數  printf("%d",arc4random()%(n-m+1)+m);

 1    // 使用者從控制台輸?入?一個n,?用while列印n個隨機數, 範圍為[30~70],找出n個隨機數中的最?大值 2 #import <Foundation/Foundation.h> 3  4 int main(int argc, const char * argv[]) 5 { 6     int n,i,max=0,b; 7     printf("輸入一個數n:"); 8     scanf("%d",&n); 9     while (i<n) {10         b = arc4random()%41+30;11         printf("%d ",b);12         if (max < b) {13             max = b;14         }15         i++;16         17         18     }19     printf("\nmax=%d",max);20     return 0;21 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.