1 / 16

讓程式轉彎

判斷. 讓程式轉彎. Loop. 迴圈. 迴圈的種類. while 迴圈 do while 迴圈 for 迴圈. for 迴圈 (1/3). for 迴圈的格式及執行流程:. for 迴圈敘述格式. for ( 設定迴圈初值 ; 判斷條件 ; 設定增減量 ) { 迴圈主體 ; }. 這兒不可以加分號. 這兒不可以加分號. 『 迴圈 』 敘述. 計數迴圈( for 迴圈敘述) for 迴圈語法. for( 初值設定運算式 ; 測試比較運算式 ; 增量設定運算式 ) { … 敘述區塊 … }. for 迴圈流程圖:.

Download Presentation

讓程式轉彎

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 判斷 讓程式轉彎

  2. Loop 迴圈

  3. 迴圈的種類 • while 迴圈 • do while 迴圈 • for 迴圈

  4. for迴圈 (1/3) • for迴圈的格式及執行流程: for迴圈敘述格式 for(設定迴圈初值; 判斷條件; 設定增減量) { 迴圈主體; } 這兒不可以加分號 這兒不可以加分號

  5. 『迴圈』敘述 • 計數迴圈(for迴圈敘述) • for迴圈語法 for(初值設定運算式;測試比較運算式;增量設定運算式) { … 敘述區塊 … } for迴圈流程圖: for迴圈執行流程:

  6. 求1~5的總和 • 您會如何做??? • 徒法鍊鋼法 public class stupid { public static void main(String[] args){ int sum=0; sum=1+2; sum = sum +3; sum = sum +4; sum = sum +5; } }

  7. 求1~5(續) • 聰明法

  8. 執行結果

  9. for迴圈 (3/3) 下面的程式利用for迴圈計算1+3+...+30: int sum=0;//why write here for(inti=1;i<=30;i +=2 ){ sum += i; } System.out.println("sum="+sum);

  10. while 迴圈 (1/3) while迴圈敘述格式 設定迴圈初值; while(判斷條件) { 迴圈主體; 設定增減量; } 這兒不可以加分號 這兒不可以加分號

  11. While(2/3) 利用while 迴圈計算1+2+…+10:

  12. While(3/3) 利用while迴圈計算1+3+...+30:

  13. do while 迴圈 (1/2) • do while是用於迴圈執行的次數未知時 • do while至少會執行1次迴圈主體 do while迴圈敘述格式 設定迴圈初值; do { 迴圈主體; 設定增減量; } while(判斷條件); 要加分號

  14. do while 迴圈 (2/2) 下面的程式利用do-while迴圈計算1+2+...+10: do{ sum +=i; i++; }while(i<=10);

  15. 巢狀迴圈 (nested loops)(1/2) • 迴圈敘述中又有其它迴圈敘述時,稱為巢狀迴圈 • 以列印部份的九九乘法表為例,練習巢狀迴圈: for(設定迴圈初值; 判斷條件; 設定增減量) { for(設定迴圈初值; 判斷條件; 設定增減量){ 敘述主體; } }

  16. 巢狀迴圈 (nested loops)(2/2)

More Related