Holey_yan

20140225_KUAS_VisualC++_Exam01

Feb 24th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. /************************************/
  2. /** 20140225 Exam 01               **/
  3. /** Maker  : Yan                   **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2014/02/25 pm.01:00   **/
  6. /************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. void _for_next();
  12. void _while();
  13. void _do_while();
  14.  
  15. //主程式
  16. void main()
  17. {
  18.     //將欲使用之副程式刪除註解即可使用該程式碼片段
  19.      
  20.     //_for_next();
  21.     //_while();
  22.     //_do_while();
  23.     system("pause");
  24. }
  25.  
  26. /*前言:
  27.   典型的迴圈會進行下列三項基本任務
  28.   控制變數初始設定, 迴圈結束條件測試, 調整控制變數的值*/
  29.  
  30. //這是for.. next.. 迴圈寫法
  31. void _for_next()
  32. {
  33.     /*for 迴圈的使用格式為
  34.     -----------------------------------------------------------------
  35.       for([控制變數初始設定]; [迴圈結束條件測試]; [調整控制變數的值])
  36.       {
  37.       [敘述式]
  38.       }
  39.     -----------------------------------------------------------------*/
  40.     for(int i = 1; i <= 100; i++)
  41.     /*如此迴圈
  42.       控制變數初始設定: i = 1, 意同i 由1 開始執行
  43.       迴圈結束條件測試: i <= 100, 意同當i 小於等於100 時執行迴圈內程式碼, 若超出則跳出迴圈
  44.       調整控制變數的值: i++, i++ 相同於i = i + 1, 意思為當成是每執行完for 迴圈一次, i 加一*/
  45.     {
  46.         printf("%d\n", i); //輸出i 值
  47.     }
  48. }
  49.  
  50. //while 迴圈寫法
  51. void _while()
  52. {
  53.     int i = 1; //需手動指定i 初始值, 此行程式碼在執行意義上相同於for 迴圈之控制變數初始設定
  54.                //也就是典型迴圈的控制變數初始設定
  55.  
  56.     /*while 迴圈使用格式為
  57.     ---------------------------
  58.       while([迴圈結束條件測試])
  59.       {
  60.       [敘述式]
  61.       }
  62.     ---------------------------*/
  63.     while(i <= 100)
  64.     /*如此迴圈
  65.       迴圈結束條件測試: i <= 100, 意同當i 小於等於100 時執行迴圈內程式碼, 若超出則跳出迴圈*/
  66.     {
  67.         printf("%d\n", i); //輸出i 值
  68.         i++; /*需手動控制i 值, 此行程式碼在執行意義上相同於for 迴圈之調整控制變數的值
  69.                也就是典型迴圈的調整控制變數的值*/
  70.     }
  71. }
  72.  
  73. //do.. while.. 迴圈寫法
  74. void _do_while()
  75. {
  76.     int i = 1; /*需手動指定i 初始值, 此行程式碼在執行意義上相同於for 迴圈之控制變數初始設定
  77.                  也就是典型迴圈的控制變數初始設定*/
  78.  
  79.     /*do.. while.. 迴圈使用格式為
  80.     -------------------------------
  81.       do
  82.       {
  83.     [迴圈內之執行片段]
  84.       }while([迴圈結束條件測試]);
  85.     -------------------------------*/
  86.     do //迴圈開始
  87.     {
  88.         printf("%d\n", i); //輸出i 值
  89.         i++; /*需手動控制i 值, 此行程式碼在執行意義上相同於for 迴圈之調整控制變數的值
  90.                也就是典型迴圈的調整控制變數的值*/
  91.     }while(i <= 100); //迴圈結束條件測試: i <= 100, , 意同當i 小於等於100 時返回繼續執行迴圈內程式碼, 若超出則結束迴圈
  92. }
Advertisement
Add Comment
Please, Sign In to add comment