結構化程式定理

出自維基百科,自由嘅百科全書

結構化程式定理structured program theorem)係喺 1966 年出嘅一條定理,革新咗程式編寫呢個領域。根據呢條定理,是但搵個用咗 goto 嘅程式,呢個程式都可以轉化做一個冇任何 goto 喺入面、而且功能上完全一樣嘅程式,後者當中衹係需要有條件運算式迴圈。打後仲有學者指出,查實就連條件運算式都唔係必要嘅。呢條定理表示咗,任何一個(當時相對普遍嘅)有 goto 嘅程式都可以變做冇 goto 而唔喪失功能-為結構化編程奠咗基[1][2]

結構化程式定理一個簡化版嘅證明如下:想像有一段用咗若干個 goto 嘅碼,而佢啲 goto 冚唪唥都指咗向正確位置。當 做第 句陳述式,整一個變數 代表現時位置,將成段碼搵個 while 迴圈包住:

  while l <= M // 當中 M 係段碼裏面嘅陳述式數量
    do 個程式

然後再跟以下規則改寫段碼:

  1. 將所有「 goto 」改做「if (l = i) then l ← j」(「a ← b」喺編程上係指「將 a 嘅數值設成 b」);
  2. 將所有「if (cond) then goto 」改做「if ((l == i) AND (cond == true)) then l ← j else l ← l + 1」;
  3. 將所有「」改做「if (l == i) then do , l ← l + 1」;

做咗呢三個步驟之後,得出嗰段碼喺功能上會同原本嗰段一樣,但冇嗮啲 goto -變咗一段更加易用又不失原有功能嘅源碼[3]

睇埋[編輯]

[編輯]

  1. Böhm, Jacopini. "Flow diagrams, turing machines and languages with only two formation rules" Comm. ACM, 9(5):366-371, May 1966.
  2. C. Böhm, G. Jacopini, "Flow diagrams, Turing Machines and Languages with only Two Formation Rules", Comm. of the ACM, 9(5): 366–371,1966.
  3. How to prove the structured program theorem? 互聯網檔案館歸檔,歸檔日期2019年9月3號,.. Computer science questions.