15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

280<br />

Adding Comments to Source Code<br />

A block comment lets you identify the beginning and end of a comment. So if<br />

you wanted to write a comment on two separate lines in C++, you’d have to<br />

type the // symbols in front of each line, such as<br />

// Calculates the hypotenuse of a triangle (C)<br />

// using the Pythagoras theorem: C 2 = A 2 + B 2<br />

If you created this as a block comment, you could use the /* and */<br />

symbols to mark the start and end of a comment like this:<br />

/* Calculates the hypotenuse of a triangle (C)<br />

using the Pythagoras theorem: C 2 = A 2 + B 2<br />

*/<br />

No matter how many comments you add, you only need to use the /* and<br />

*/ comment symbols once like this:<br />

/* Calculates the hypotenuse of a triangle (C)<br />

using the Pythagoras theorem: C 2 = A 2 + B 2 .<br />

The length of the hypotenuse is then used to<br />

move a cartoon figure on the screen.<br />

*/<br />

Block comments just make it easier to add multiple lines of comments.<br />

Programmers often use both line and block comments in a single program<br />

like this:<br />

/* Calculates the hypotenuse of a triangle (C)<br />

using the Pythagoras theorem: C 2 = A 2 + B 2 .<br />

The length of the hypotenuse is then used to<br />

move a cartoon figure on the screen.<br />

*/<br />

c = sqrt(a * a + b * b) // Pythagoras theorem<br />

The comment symbol in one language may have a completely different<br />

meaning in another language, so make sure you don’t mix them up.<br />

✦ In the curly bracket languages (such as C++ and Java), the curly brackets<br />

are used to define a block of commands like this:<br />

int main()<br />

{<br />

cout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!