What are block comments?
Block comments are used in programming languages for commenting. Programmers can add as many numbers of lines of strings as a block comment. Programmers can provide large explanations or descriptions of the program as block comments. Block comments increase the understandability of the program.
Comments
Every programming language has some way to write comments in between the source programs. Comments are written as part of the source code. These comments are the parts in the source program that are not executed (they are simply skipped) by the interpreter/compiler/assembler. Comments are used to describe the logic of a programming language to increase the readability of the program. It is the text that is embedded within the source code to describe the respective line or block of code. They are mainly targeted for making the code easier for non-programmers.
The programming comments are ignored by the compiler, hence there should be a syntax rule by which a compiler can recognize a comment. Various symbols are used to denote the start and end of a comment depending upon the programming language and pattern.
Different types of commenting
Mainly there are two ways of commenting:
- Line comments- Used for a single line of comments
- Block Comments- Used for multiple lines of comments
Programs can use any type of comment depending upon the length of the code description.
Line comments
Line comments (also called single-line comments or inline comments) are used for writing a single line of comment. In most programming languages, these comments contain a certain symbol in front of the sentence that helps the translator program to understand the start of the comment line.
E.g. In python, we use the # symbol for inline comments.
n = 10 # this is a comment
m = n + 5
In C, C++, or Java single-line comments are made using the // symbol.
n = 10; // this is a comment
m = n + 5
While the interpreter executing the program encounters the symbol for line commenting, it skips that part and continues the execution from the next line. For example, If '#' or '//' precedes a line of code, then it will not be executed.
Commenting between program code needs to be done with care. Sometimes placing the # symbol in front of important lies of executable code can lead to improper execution of the program. This can lead to logical and compiler errors.
Block comments
Block comment or multi-line comments are those comments that include multiple lines of comments or multi-line strings. It is possible to write multiple lines of comments by using single-line comments. In this method, the programmer will have to use the single-line comment symbol at the starting of each line of the description. It is time-consuming and hinders with the readability of the program. Hence most programming languages provide multiple line commenting or block commenting by using certain assigned symbols that mark the starting and ending of a comment. Block comments can be multiple lines of comments without a limit. These lines need to be enclosed in the symbol that marks the start and the end of the block.
For instance, In python, triple quotes ''' (three single quotes) or """ (three double quotes) for multi-line comments can be used for marking the starting and end of the comment.
n = 10
'''
these lines
are comments
'''
m = n + 5
In C, C++, and Java, /* is used for the beginning of a block comment and */ for the end of it.
n = 10
/ *
these lines
are comments
* /
m = n + 5
Block comment is mainly used for writing paragraphs or consecutive lines of comments. When the interpreter encounters the starting symbol for block comment, then it skips all the lines from the line containing the starting symbol till the line containing the ending symbol for block comment.
Block comments are helpful while debugging the program. Block comments facilitate in removing errors from the program code. If an error was observed in a particular line of code, then changing that into a single line comment will help the programmer to avoid the error without deleting the code. The same logic can be applied with block comments for debugging multiple lines of code. This method of debugging is mainly used to solve logical errors where the programmer wants to correct the logic of a program without deleting codes. Hence using block comments to debug can save code lines from being deleted.
In C, C++, and Java, block comments can be added in between the same line of code, not in Python.
Comments in Java
Comments in Java are the code description. It is ignored during the run time by the compiler. When used in HTML files, Java comments are known as Javadoc comments. They are used in Javadoc.exe files. The HTML documentation string is created using Javadoc comments. They have a specific format for giving descriptions of java docstrings while building an application user interface. It is represented by the /** symbol.
Key Takeaways
- Block comments (or line comments) are required for a better understanding of the program written. Writing comments is a good practice to become a good programmer.
- Commenting also helps in debug of code efficiently and saves time in editing the code.
- Many lines of comments to a program can also be added to explain a code (this never alters the function of the program). Consequently, a person reading the code for the first time can easily understand the code.
- When a comment is added to a program, proper care must be taken because source code might be commented by mistake which could affect the functioning of the whole program.
- By removing the symbols, a comment line can be easily uncommented.
Context and Applications
This topic is studied under various courses such as
- Masters in Computer Science
- Bachelors in Computer Science
Practice Problems
Question 1) Which of the following is correct?
a) The translator will function to execute the comments and will detect spelling mistakes.
b) Comments can only be added at the beginning of a program.
c) In comments you can only use alphanumeric characters.
d) You can write a single line of comment in block comments.
Answer- Option d
Explanation- The block comments can have any number of lines of strings as comments, it can even have a single line of comment or even no comment at all.
Question 2) What would be the output of the given Python program?
n = 10
m = 5
#n += m
m += n
print(m)
a) 10
b) 15
c) 20
d) Error
Answer- Option b
Explanation- The third line of code is a comment, so it will not be executed, and the variable m will have the value 15 after the execution of the fourth line, and that result is printed.
Question 3) Which of the following represents multiple lines of comments in python?
a) #
b) /*
c) '''
d) //
Answer- Option c
Explanation- Multiple lines of comments in python are represented by '''( triple quotes).
Question 4) Which of the following statements is correct about comments?
a) Comments cannot be used inside a source program.
b) Only codes can be commented on in a computer language.
c) Comments are added inside programs to give meaning to that program.
d) Only Java supports commenting.
Answer- Option c
Explanation- Comments are mainly used to give descriptions, definitions, and ideas of programs or codes used in the program.
Question 5) Which among the following statements is correct?
a) Comments can be used for debugging.
b) Using comments is mandatory, otherwise, the program will not be executed properly.
c) Every comment should be written at the end of a program.
d) A proper code in a program will be executed even if you comment on that code.
Answer- Option a
Explanation- While debugging you can comment on those faulty or unwanted lines of codes and skip the execution.
Common Mistakes
Commenting is widely used by programmers hence, giving the wrong description for a line of code can lead to misunderstanding.
Using commenting symbols should be precisely done, and should follow syntax rules. As each language has its own syntax rules, using the wrong symbol can lead to compiler errors. To mark the start of a comment, the # sign is used in Python. Not using the end symbol for block comments will cause logical and syntax errors. Without the symbol, the block comment is considered a line of code and is compiled. Here, the program starts to misbehave and it might confuse people.
Not adding a blank line between codes and their comments is a common mistake made by new programmers. Even though it would not break the syntax rule, it can lead to confusion and can make the code hard to read. It is better to add a blank line between some codes and comments to know which all comments are meant for certain lines of codes in the program.
Comments are also a part of social media content. This overlap of the topic can cause confusion.
Related Concepts
- Python comments
- Docstrings
- Javadoc comments
- Function of compilers
Want more help with your computer science homework?
*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.
Block comments Homework Questions from Fellow Students
Browse our recently answered Block comments homework questions.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.