Overview

Adding comments to your code helps explain to other people reading your code what you have written in code and why you have written your code in a certain way. Comments added to code can be useful for teachers who want to explain activities to their students. It can act as a memory aid if you need to edit your code at a later date. A good comment clearly describes what is happening in the code and sometimes why the code is included.


A comment is defined by the special characters or symbols that surround it eg // # or “””. This tells the program to ignore anything following the characters, on the same line or in the case of a multi-line comment before the symbols are used again. If we don’t use these symbols, the program thinks that our comments are code that it needs to run and will show us an error.

Contents

MakeCode Editor




Comments in MakeCode Editor Blocks

To write a comment at a workspace level right-click anywhere within the workspace area and select Add Comment from the drop-down menu. A comment box will appear in the workspace that is independent of any block. Type your comment in this block.


To write a comment in your code in the MakeCode Editor at the block level, right-click on the block and select ‘Add Comment’ from the drop-down menu.  A comment box will appear in the workspace linked to the block. Type your comment in this block.


A screenshot of MakeCode Editor showing comment notes attached to blocks


Comments in MakeCode Javascript

Note: The steps below to write comments in MakeCode Javascript, and MakeCode Python, include the phrases ‘multi-line comment’ and ‘single-line comment’. A multi-line comment is the equivalent of a workspace comment and a single-line comment is the equivalent of a block-level comment.


To write a multi-line comment in the MakeCode editor Javascript type /**  and write your comment. To close your comment press enter to start another new line and type */. It is common practice to add a * at the beginning of each new line, other than the end line.


To write a single-line comment type // then write your comment. If you continue the comment onto another line, you should type another // at the beginning of the new line.


All comments must be indented to match the code that they describe.


/**
 * A program to scroll hello world and then display a heart
 */

// Create a loop
basic.forever(function () {
    // Show this string on the micro:bit LED display
    basic.showString("Hello!")
    // Show the heart image on the micro: bit LED display
    basic.showIcon(IconNames.Heart)
})


Comments in MakeCode Python

To write a multi-line comment MakeCode Python you need to type “”” on a new line, press enter to start a new line and write your comment. To close your comment press enter to start a new line and type “””.


To write a single-line comment in your code type at the beginning of a new line and write your comment. When you have finished writing your comment press enter and write your code on a new line.


All comments should be indented to match the code they describe.


"""
A program to scroll hello world and then display a heart
"""

# Create a function to run in our loop
def on_forever():
    # Show this string on the micro:bit LED display
    basic.show_string("Hello!")
    # Show the heart image on the micro:bit LED display
    basic.show_icon(IconNames.HEART)

# call the on_forever function in a 'forever' loop
basic.forever(on_forever)


Python Editor

Comments in MicroPython

To write a multi-line comment in MicroPython you need to type “”” on a new line, press enter to start a new line and write your comment. To close your comment press enter to start a new line and type “””.


To write a single-line comment in your code type at the beginning of a new line and write your comment. When you have finished writing your comment press enter and write your code on a new line.


All comments should be indented to match the code they describe.


"""
A program to scroll hello world and then display a heart
"""

# import everything from the microbit library
from microbit import *

# while this statement is true ie. forever
while True:
    # scroll a string across the display
    display.scroll('Hello, World!')
    # display an image of a heart
    display.show(Image.HEART)
    # pause the program for 2 seconds
    sleep(2000)


Comments in C++

To write a multi-line comment in your code in C++ type /* then press enter and type your comment on a new line. To close your comment press enter to start a new line and type */.


To write a single-line comment in your code type // at the beginning of a new line and write your comment. When you have finished writing your comment press enter and write your code on a new line.


All comments should be indented to match the code they describe.


/*
A program to scroll hello world and then display a heart
*/

// include this header file
#include "Microbit.h"

// Use the uBit instance of the MicroBit class
MicroBit uBit;

// Create a variable to contain our heart image
const uint8_t full_heart_arr[] {

                          0, 1, 0, 1, 0,
                          1, 1, 1, 1, 1,
                          1, 1, 1, 1, 1,
                          0, 1, 1, 1, 0,
                          0, 0, 1, 0, 0, };

MicroBitImage full_heart(5,5,full_heart_arr);

// Start our program
int main()
{
  // Initiate an instance of uBit
  uBit.init();
  // Scroll a string on the display
  uBit.display.scroll("Hello, World!");
  // Display a heart image on the screen
  uBit.display.print(full_heart)
}