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 in code can be very useful for teachers who want to explain activities to their students and it can act as a memory aid if you want 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 in the workspace and select Add Comment from the drop-down menu. A comment box will appear in the workspace that is independent to the blocks. Type your comment in this block.


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


Comments in MakeCode Javascript

The steps below to write comments in MakeCode Javascript, 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 MakeCode editor Javascript type /**  and write your comment. To close your comment press enter to start another new line and type **/


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


It is important that comments are indented to match the code that they describe.


/**
A program to scroll hello and then display a heart in a loop.
**/


// Create a loop
basic.forever(function () {
// Show this string on the micro:bit LED display
basic.showString("Hello, World!")
// 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 in 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 and then display a heart in a loop.
"""

# Create a function to run in our loop
def on_forever():
# Show this string on the micro:bit LED display
basic.show_string("Hello, World!")
# 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 in 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 in a loop.
"""

# 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 and then display a heart on the LED display.
*/

// 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)
}