BASIC 程式設計/BASIC 入門/PRINT、CLS 和 END
外觀
BASIC 程式設計 > BASIC 入門 > 你的第一個程式
在前面的程式碼示例中,我們編寫了你的第一個 BASIC 程式。在其中,你看到了 PRINT、CLS 和 END 命令的示例。它們在程式中的作用可能當時並不明顯,但由於它們對 BASIC 語言至關重要,因此將在本文中進行討論。
10 CLS
20 PRINT "Hello, world!"
30 PRINT "I'm making the sample program clear and understandable."
40 PRINT "This text is being printed via the PRINT command."
50 PRINT "On the next line, I'll use CLS, which will clear everything I just printed, so you won't even see the preceding text."
55 PRINT "Also, you can't give CLS a line to PRINT; it won't actually do anything"
60 CLS "These words actually do nothing; they do not PRINT or anything."
70 PRINT "Finally, on line 80, I'll use END, which will keep me from reaching line 90."
80 END "Like CLS, putting a string here does nothing; it does not PRINT or anything."
90 PRINT "this is not really my answer."
(CLS) Hello, world!" I'm making the sample program clear and understandable." This text is being printed via the PRINT command." On the next line, I'll use CLS, which will clear everything I just printed, so you won't even see the preceding text." Also, you can't give CLS a line to PRINT; it won't actually do anything" (CLS) Finally, on line 80, I'll use END, which will keep me from reaching line 90." (END)
-程式結束-
從那個例子中,我們可以很容易地推斷出每個命令的功能。
- CLS
- 一個縮寫,代表 CLear Screen(清除螢幕)。在上面的程式中,當你使用第 60 行的 CLS 時,螢幕上列印的所有文字都被清除了。
- 寫入螢幕。有命令可以列印到其他東西,比如印表機,但那是以後要討論的內容。每個新的 PRINT 命令都會從新行開始列印。要插入空行,不要指定要列印的字串。 "PRINT" 的語法是:PRINT "[你想要在這裡列印的任何內容]"
- END
- 它在該行停止程式;也就是說,之後新增的任何內容都不會顯示。這就是為什麼第 90 行的 PRINT 命令沒有列印任何東西。END 命令可以包含在控制結構中,如果滿足條件,則結束程式。這將在控制結構中討論。
正在發生什麼?
- 第 10 行,螢幕被清空。
- 第 20 行到第 50 行,文字被列印到螢幕上。
- 第 60 行,螢幕被清空。
- 第 70 行,顯示了你在執行此程式後應該看到的訊息。
- 第 80 行,結束程式。
- 第 90 行,顯示 END 語句停止程式。END 語句之後的任何指令都不會執行。
鑑於當今計算機的速度,你應該看不到第 20 行到第 50 行顯示的段落,它應該在你有機會看到它之前被第 60 行的 CLS 語句清空。如果你降低程式速度,你就可以看到程式將訊息寫入螢幕。然後第 70 行被寫入螢幕/顯示,然後第 80 行停止所有操作。第 90 行永遠不會執行。