Tag Archives: comments

Using comments in C#

Comments are useful for explaining the purpose of a piece of code or how it works. Comments can also be used to identify the author of code or when it was created or last updated.

In C# the // characters (two forward slashes) are used to add comments to your code. Any text on a line after the two forward slashes will not be treated as code. Comments are useful as they contain notes about what is happening in the code and can help others understand the algorithm. They won’t change the way a program runs and won’t be visible in the working program.

Here is an example of a comment:

// This is a comment

Watch the video below to see how to use comments and scroll down for the sample code.

Sample code

using System;

namespace MyCSharpProject
{
  class MainClass
  {
    public static void Main(string[] args)
    {
      string message = "The result is ";
      float num1 = 6f; // Assign value to num1 variable
      float num2 = 4f; // Assign value to num2 variable

      float result = num1 + num2 + 10f; // Add numbers

      result = num1 - num2; // Subtract
      result = num1 * num2; // Multiply
      result = num1 / num2; // Divide
      result = num1 % num2; // Mod

      int num3 = 30;
      num3--;

      Console.WriteLine(result);
      Console.WriteLine(num3);
    }
  }
}

Next tutorial: Combining types in output statements

Echo statement and comments

This tutorial introduces the echo statement for displaying messages and also discusses comments that can be added to your code. Echo statements can be used to output simple messages, variable values, and also HTML code. You can even include JavaScript code within echo statements. The echo statement is similar to the print statement you might see in many other languages.

For example, the statement echo “Hello world”; will display the message “Hello world”. The statement echo $myVariable; will output the value of the variable called $myVariable. The statement echo “<h1>My heading</h1>”; will output text formatted in a h1 size heading (using the h1 heading HTML tag).

You could even add JavaScript code to an echo statement, for example: echo ‘<script>alert(“Hello world”);</script>’;

Watch the video below and then scroll down to see the sample code:

PHP sample code:

<?php
 // this is a single-line comment
 
 /* 
 this
 is
 a
 multi-line
 comment
 */
 
 ############################
 # this is a comment block #
 ############################

 echo "Hello world"; // this is a string in an echo statement
 echo "He said \"Hello world\""; // this is an example of escaping quotation marks
 echo 'Hello world'; // this is an example of a string inside single quotation marks
?>

A reminder about the ?> tag

If you are a beginner PHP programmer, it is recommended that you omit the closing ?> PHP tag at the end of your code if the file is pure PHP code. This will prevent any unwanted effects that may occur if you add whitespace after the closing tag.

If you do use the closing ?> then it is important that you do not add any other white space (spaces or new lines) after the tag. You can read the reasons why at the official PHP documentation site.

PHP Manual references:

HTML Formatting

You can use a range of tags in HTML to format text on your webpage. This tutorial covers the following HTML formatting tags:

      • <strong> is used for bold text
      • <b> is also used for bold text
      • <em> is used for italicised text
      • <i> is also used for italicised text
      • <u> is used for underlined text
      • <del> is used for deleted text
      • <sub> is used for subscripted text
      • <sup> is used for superscripted text
      • <mark> is used for marked/highlighted text
      • <hr/> is used for a horizontal rule (line) as seen below

The video below shows how to use these tags and also how to write comments inside your code that aren’t displayed on the webpage by the browser. Comments which are only visible in your code are written inside the <!– and –> tags.

Watch the video below and then scroll down to see the sample code and find out what each line of code does.

The code below shows how to use these different HTML formatting tags.

<!DOCTYPE html>
<html>
<head>
 <title>HTML Formatting</title>
 <meta charset="utf-8"/>
</head>

<body>
<p>This is a paragraph of plain text.</p>
<hr/>
<p>This is <strong>bold</strong> text and this is <em>italicised</em> text.</p>
<p>This is <u>underlined</u> text</p>
<p>This is <del>deleted</del> text and this is <mark>highlighted</mark> text.</p>
<p>This is <sub>subscripted</sub> text and this is <sup>superscripted</sup> text.</p>
<!-- this is a comment -->
</body>

</html>

A comment is not actually displayed in the web browser. It is hidden and only visible inside your code. You can use comments to include the author of the code and date it was created. You can also use it to make notes on code that needs to be changed or what different bits of code are used for.

The code from above would display the following content in your web browser (note that the comment is not visible):

HTML formatting
HTML formatting

The difference between the <strong> and <b> tags

The <strong> tag is a part of HTML5. The <b> tag has been around for a very long time. The <em> tag is also a HTML5 tag, whereas the <i> tag has been around for a long time. In most browsers, it will appear that <strong> and <b> both do the same thing – they make text bold. And it may also appear that <em> and <i> do the same thing – they italicise text. However, there is a difference.

The <b> tag is a style. It describes how the text it surrounds should be displayed. The <strong> tag is indication of how something should be understood. It describes the text it surrounds. (eg. this text should be stronger than the rest of the text you’ve displayed).