【文章內(nèi)容簡介】
? 循環(huán) ? 概述 C語言可實現(xiàn)循環(huán)的語句: ? 用 goto 和 if 構成循環(huán) ? while 語句 ? do ~ while 語句 ? for 語句 ? goto語句 一般格式 p132-轉移作用 goto 語句標號 ; ….….. 標號:語句 ; 標號:語句; ….….. goto 語句標號; ?不能用整數(shù)作標號 ?只能出現(xiàn)在 goto所在函數(shù)內(nèi) ,且唯一 ?只能加在可執(zhí)行語句前面 ?限制使用 goto語句 例 用 if 和 goto語句構成循環(huán),求 1+2+…+100 /*loop using goto statement*/ include main() { int i,sum=0。 i=1。 loop: if(i=100) { sum+=i。 i++。 goto loop。 } printf(%d,sum)。 } sum=0+1 sum==1+2=3 sum=3+3=6 sum=6+4 …… sum=4950+100=5050 循環(huán)初值 循環(huán)終值 循環(huán)變量增值 循環(huán)條件 循環(huán)體 例 從鍵盤輸入一組數(shù)據(jù),以 0結束輸入,求數(shù)據(jù)和 /*sum of data*/ include main() { int number,sum=0。 read_loop: scanf(%d,amp。number)。 if(!number) goto print_sum。 sum+=number。 goto read_loop。 print_sum: printf(The total sum is %d\n,sum)。 } 例 輸入 10個男孩的身高和體重,統(tǒng)計身高超過 170體重少于 50公斤的人數(shù)。 例 pp135 RANGE of Numbers: A survey of the puter macket show that personal puters are sold at varying costs bythe following is the list of costs(in hundreds)quoted by some vendors: , , , , , , , , , Determine the average cost and the range of values. 例 pp136 PayBill Calculations: A manufacturing pany has classified its executives into four levels for the benefit of certain perks. The levels and corresponding perks are shown below: Perks Level Conveyance Entertainment allowance allowance 1 1000 500 2 750 200 3 500 100 4 250 An executive?s gross salary includes basic pay, house rent allowance at 25% of basic pay and other perks. Ine tax is withheld from the salary on a percentage basis as follows: Gross salary Tax rate Gross=2021 No tax deduction 2021Gross=4000 3% 4000Gross=5000 5% Gross5000 8% Write a program that will read an executive?s job number, level number, and basic pay and then puter the salary after withholding ine tax. The problem is detailed in the program. ?while語句 p147 ?一般形式 : while(表達式 ) 循環(huán)體語句; ?執(zhí)行流程 : expr 循環(huán)體 假 (0) 真 (非 0) while ?特點:先判斷表達式,后執(zhí)行循環(huán)體 ?說明: ?循環(huán)體有可能一次也不執(zhí)行 ?循環(huán)體可為任意類型語句 ?下列情況,退出 while循環(huán) ?條件表達式不成立(為零) ?循環(huán)體內(nèi)遇 break,return,goto ?無限循環(huán) : while(1) 循環(huán)體 。 例 用 while循環(huán)求 1+2+…+100 /*sum of 1 to 100*/ include main() { int i,sum=0。 i=1。 while(i=100) { sum=sum+i。 i++。 } printf(%d,sum)。 } 循環(huán)初值 循環(huán)終值 循環(huán)變量增值 循環(huán)條件 循環(huán)體 例 顯示 1~10的平方 /*square of every number*/ include main() { int i=1。 while(i=10) { printf(%d*%d=%d\n,i,i,i*i)。 i++。 } } 運行結果: 1*1=1 2*2=4 3*3=9 4*4=16 5*5=25 6*6=36 7*7=49 8*8=64 9*9=81 10*10=100 ?do~while語句 p150 ?一般形式: do 循環(huán)體語句; while(表達式 )。 ?執(zhí)行流程 : do 循環(huán)體 expr 假 (0) 真 (非 0) while ?特點:先執(zhí)行循環(huán)體,后判斷表達式 expr 循環(huán)體 假 (0) 真 (非 0) 循環(huán)體 While循環(huán) ?至少執(zhí)行一次循環(huán)體 ?do~while可轉化成 while結構 例 用 do~while循環(huán)求 1+2+…+100 /**/ include main() { int i,sum=0。 i=1。 do { sum+=i。 i++。 }while(i=100)。 printf(%d,sum)。 } 例 while和 do~while比較 include main() { int i,sum=0。 scanf(%d,amp。i)。 do { sum+=i。 i++。 }while(i=10)。 printf(%d,sum)。 } include main() { int i,sum=0。 scanf(%d,amp。i)。 while(i=10) { sum+=i。 i++。 } printf(%d,sum)。 } ?for語句 p152 ?一般形式: for([expr1] 。[ expr2] 。[ expr3]) 循環(huán)體語句; ?執(zhí)行流程 : expr2 循環(huán)體 假 (0) 真 (非 0) for expr1 expr3 ?for語句一般應用形式 : for(循環(huán)變量賦初值;循環(huán)條件;循環(huán)變量增值 ) { 循環(huán)體語句; } expr1。 while(expr2) { 循環(huán)體語句; expr3。 } 例 用 for循環(huán)求 1+2+…+100 include main() { int i,sum=0。 for(i=1。i=100。i++) sum+=i。 printf(%d,sum)。 } ?for語句中 expr1, expr2 ,expr3 類型任意,都可省略,但分號;不可省 ?無限循環(huán) : for(。) ?for語句可以轉換成 while結構 ?“ 溫故 ” 逗號運算符 p154 例 用 for循環(huán) 生成 10個隨機數(shù)并累加 include include include main() { int i, tmp, sum=0。 /* Seed the randomnumbe