|
Almost every time the teacher
gives lesson in school, we might copy everything from
the board or make notes over what is taught. There is
a similar thing we call in c++, ‘Comments’.
Comments can be used for different purposes.
·
The most obvious one, to recall the
purpose of the code (Say you are referring to it after
6 months, makes less sense then. If you don’t
include a comment.)
·
Helps your computer teacher know what
you have actually written. Just kidding.
·
Now this one is an unobvious reason.
Sometimes after compiling you might want to
remove some line just to see how your program works
then, without deleting the actual code.
·
The final one, just a good programming
style to express your thoughts.
Comments can be written anywhere
in the program (Good Feature). They don’t effect
your code, unless you comment out something by
mistake.
There are two types of comments:
- C
Style:
/*
You can write anything that
makes sense here */
After the introduction of c++ it
just become a norm for programmers to ‘Comment
out’, huge parts of their code using this style.
The initial part (/*) and the final part(*/) can be on
separate lines.
E.g.:
- #include
<iostream.h>
- int
main()
- {/*
- cout<<
“ This whole line gets, commented
out!”;*/
- }
|
<Warning:
The numbered indentation is not to be included with
the code while typing. It is only for your
convenience>
- C++
Style
//
After the 2 slashes, you can
type anything that makes sense.
This comment style is used more
often these days but please note that if your comment
sentence goes over many lines, please use // on each
line at the beginning.
E.g.:
- #include
<iostream.h>
- int
main()
- {
- cout<<
“How are you I am fine”;
- //
Line 4 will print
- //“How
are you I am fine, to the screen.
- //
Notice that
- //I
placed the 2 slashes on this line too.
- }
|