All lessons

1. Getting started

Comments and program structure

0 of 5 activities0%

Reading 1

Comments do not run

Open

Use comments to explain intent. They are ignored by the compiler.

A line comment starts with // and runs to the end of that line. A block comment starts with /* and ends with */.

C++ statements usually end with a semicolon. Curly braces { } group statements into a block, such as the body of main.

// This is a line comment
int x = 3;  // also a line comment

/*
  This is a block comment.
  It can span several lines.
*/

Check 2

Which line is a comment?

Open

Which of these is a valid C++ comment?

Fill in 3

End of a statement

Open

Most C++ statements end with this single character:

Hint: It is the same character used at the end of English sentences in some styles of typing.

Try it 4

A program with comments

Open

Run this program. Then uncomment the second cout line and run again.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Print 42

Open

Write a program that prints 42 and a newline. You may include a comment.

main.cpp
Loading editor…