跳至內容

程式設計基礎:註釋

來自華夏公益教科書,開放的書籍,開放的世界

試卷 1 - ⇑ 程式設計基礎 ⇑

← 變數 註釋 輸入和輸出 →


在接下來的幾章中,您可能會在程式碼示例中看到很多文字,這些文字似乎除了幫助理解程式碼之外什麼也不做。這些文字稱為註釋,如果您有這本書的彩色版本,您會看到這些註釋以綠色突出顯示。

註釋 - 程式設計師在計算機程式原始碼中可讀的註釋,有助於程式設計師理解程式碼,但在執行時通常被忽略


在 Visual Basic 中,所有註釋都以撇號' 或單詞 REM 開頭。讓我們看一個簡單的示例

' this is a comment
dim a, b, c as string' declare variables to store names

'''' read the three names ''''
a = console.readline()
b = console.readline()
c = console.readline()

console.writeline("the names are :" & a & b & c)

註釋的另一個用途是停用不想刪除但要保留的程式碼。透過註釋掉程式碼,意味著您隨時可以恢復它。在最終程式碼中保留註釋掉的程式碼不是一個好主意,但在開發過程中這是一種非常常見的做法

'the code below now only takes one name as input instead of three
dim a as string ' declare variables to store names
', b, c as string ' this code does nothing!

'''' read the three names ''''
 a = console.readline()
' b = console.readline()
' c = console.readline()

console.writeline("the names are :" & a)
' & b & c)


Python 示例

[編輯 | 編輯原始碼]

Python 中的註釋使用井號# 字元,如下所示

# this is a comment
animal = "bunny"  # initialise a variable with the string "bunny"
print(animal)
# this is another comment
華夏公益教科書