跳轉到內容

程式設計基礎/流程圖

來自華夏公益教科書,開放的書籍,開放的世界
顯示“Hello world!”的流程圖

流程圖是一種圖表,用於表示演算法、工作流或過程。流程圖將步驟顯示為各種型別的框,並透過箭頭連線框來表示它們的順序。這種圖表表示說明了給定問題解決方案模型。流程圖用於分析、設計、記錄或管理各個領域的流程或程式。[1]

流程圖將程式碼中的步驟顯示為相互連線的形狀,並用箭頭連線。主要目標是建立對編碼問題解決方案的粗略草稿。流程圖中看到的形狀型別取決於程式設計師想要建立的語句。例如,“if”語句(僅在特定條件為真時才起作用的程式碼部分)由菱形表示,而迴圈語句(允許程式碼部分根據需要重複自身)由六邊形表示。流程圖還可以對不同型別的語句進行顏色編碼,使程式碼更易於閱讀。

以下是常見的流程圖符號和示例。在第一次閱讀本節時,請重點關注簡單符號和示例。在後面的章節中再回到本節,回顧高階符號和示例。

簡單的流程圖符號

[編輯 | 編輯原始碼]

圓角矩形或終點表示流程圖的起點和終點。

Terminal

流程線

[編輯 | 編輯原始碼]

注意:預設流程是從左到右,從上到下(與閱讀英文的方式相同)。為了節省時間,箭頭通常只在流程線與正常方向相反時繪製。

Line

輸入/輸出

[編輯 | 編輯原始碼]

平行四邊形表示輸入或輸出操作。

Input/Output

矩形表示過程,例如數學計算或變數賦值。

Process

菱形用於表示決策符號中測試的真/假語句。

Decision

高階流程圖符號

[編輯 | 編輯原始碼]

模組呼叫

[編輯 | 編輯原始碼]

程式模組在流程圖中用一個矩形表示,並用一些線來區分它與過程符號。程式設計師通常會在程式控制和特定任務模組之間,或在本地函式和庫函式之間進行區分。

Predefined Process

連線符

[編輯 | 編輯原始碼]

有時流程圖會分成兩個或多個更小的流程圖。這通常是在流程圖不適合單個頁面,或者必須分成幾個部分時進行的。連線符符號(是一個帶字母或數字的小圓圈)允許你在同一頁面上連線兩個流程圖。看起來像襯衫上口袋的連線符符號,允許你連線到不同頁面上的流程圖。

On-page Connector Off-page connector

簡單示例

[編輯 | 編輯原始碼]


簡單示例

[編輯 | 編輯原始碼]

我們將透過顯示一些虛擬碼的流程圖來演示各種流程圖元素。

虛擬碼:沒有引數傳遞的函式

Function clear monitor
    Pass In: nothing
    Direct the operating system to clear the monitor
    Pass Out: nothing
End function

虛擬碼:主函式呼叫清除監視器函式

Function main
    Pass In: nothing
    Doing some lines of code
    Call: clear monitor
    Doing some lines of code
    Pass Out: value zero to the operating system
End function

順序控制結構

[編輯 | 編輯原始碼]

下一個專案是關於一個簡單的溫度轉換程式的虛擬碼。這演示了頁面內和頁面外連線符的使用。它還說明了順序控制結構,其中沒有發生任何不尋常的事情。只需按照列表中的順序依次執行每個指令。

虛擬碼:順序控制結構

Filename: Solution_Lab_04_Pseudocode.txt
Purpose:  Convert Temperature from Fahrenheit to Celsius
Author:   Ken Busbee; © 2008 Kenneth Leroy Busbee
Date:     Dec 24, 2008

Pseudocode = IPO Outline

input
    display a message asking the user for the temperature in Fahrenheit
    get the temperature from the keyboard

processing
    calculate the Celsius by subtracting 32 from the Fahrenheit temperature then multiply the result by 5 then divide the result by 9. Round up or down to the whole number.
    HINT: Use 32.0 when subtracting to ensure floating-point accuracy.

output
    display the Celsius with an appropriate message
    pause so the user can see the answer 

高階示例

[編輯 | 編輯原始碼]

選擇控制結構

[編輯 | 編輯原始碼]

虛擬碼:如果則否則

If age > 17
    Display a message indicating you can vote.
Else
    Display a message indicating you can't vote.
Endif

虛擬碼:情況

Case of age
    0 to 17   Display "You can't vote."
    18 to 64  Display "You are in your working years."
    65 +      Display "You should be retired."
End case

迭代(重複)控制結構

[編輯 | 編輯原始碼]

虛擬碼:While

count assigned zero
While count < 5
    Display "I love computers!"
    Increment count
End while

虛擬碼:For

For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End for

for 迴圈沒有標準的流程圖方法,您會發現它以不同的方式完成。 作為計數迴圈的 for 迴圈可以類似於作為計數迴圈的 while 迴圈進行流程圖。

虛擬碼:Do While

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero

虛擬碼:Repeat Until

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one

關鍵術語

[編輯 | 編輯原始碼]
決策符號
流程圖中用於提出問題和做出決策的菱形。
流程線
連線各個流程圖符號的線(有時帶箭頭)。
流程圖
一種程式設計設計工具,它使用圖形元素來直觀地描述函式中邏輯的流程。
輸入/輸出符號
流程圖中用於輸入/輸出互動的平行四邊形。
處理符號
流程圖中用於正常流程(如賦值)的矩形。

參考資料

[編輯 | 編輯原始碼]
華夏公益教科書