Programming Knowledge for Software Testers
Tutorial 4: Writing Comments
I am taking four different languages to explain the comments,
1. Java
2. Python
3. VBScript
4. HTML
Note: Writing comments is optional in computer programming, but best practice.
Comments are text notes added to the program to provide explanatory information about the source code.
Two important purposes of comments:
i. Comments can be used to make the code more readable.
ii. Comments can be used to prevent execution when testing code.
1. Java
Java supports single line comment and multi-line comment,
a) Single-line comments start with two forward slashes (//).
b) Multi-line comments start with /* and ends with */.
Example:
//Documentation Section
package myPackage;
public class MyClass {
//Interface Section
static int a=10, b=20; //Declare static variables
int c=20, d=40; //Declare Non Static Variables
public static void main(String[] args) {
//Main Program
System.out.println(a+b); //Add two variables/numbers
//Compare two variables / numbers
if (a b) {
System.out.println("A is Big Number");
}
else {
System.out.println("B is Big Number");
}
}
//Interface Section
}
----------------------------------------
2) Python
Python supports single line comment and multi-line comment,
a. Single-line comments start with a #, , and Python will ignore them
(Comments can be placed at the end of a line also, and Python will ignore the rest of the line)
b. Python does not really have a syntax for multi line comments. To add a multiline comment you could insert a # for each line,
you can add a multiline string (triple quotes) in your code, and place your comment inside it.
Example:
# This is a sample Python Program
a=100
b=200
# Add two variables/number and print
print (a+b)
if (a b):
print("A is Big Number")
else:
print ("B is Big Number")
print ("Hello Python")
print("") # Insert blank line
# Print 1 to 10 Number
for num in range (1, 11):
print (num)
3. VBScript
VBScript supports single line comment only, doesn't support multi line comment
VBScript single-line comments start with an '
Example:
Dim a, b
a=100
b=200
'Print addition of two variables / numbers
MsgBox (a+b)
MsgBox "" ' Insert Blank Line
If (a b) Then
MsgBox "A is Big Number"
else
MsgBox "B is Big Number"
End If
4. HTML
You can add comments to your HTML source by using the following syntax: