Fundamentals of Programming: Comments

From Wikibooks, open books for an open world
Jump to navigation Jump to search

PAPER 1 - ⇑ Fundamentals of programming ⇑

← Variables Comments Input and output →


Over the course of the next few chapters you might see a lot of text in the code examples that doesn't seem to do anything other than aid understanding of the code. This text is called a comment and if you have this book in colour you'll see that these comments are highlighted in green.

comment - programmer-readable annotation in the source code of a computer program that helps programmers understand the code but is usually ignored at run time


In Visual Basic all comments start with an apostrophe ' or the word REM. Let's take a look at a quick example:

' 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)

Another use for comments is to disable code that you don't want to delete but you do want to keep. By commenting out the code it means that you can always restore it later if you want to. It isn't a good idea to leave commented out code in final code, but it is a very common practice when you are developing something:

'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 example[edit | edit source]

Comments in Python use the hash # character, like this:

# this is a comment
animal = "bunny"  # initialise a variable with the string "bunny"
print(animal)
# this is another comment