Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Comments · Lesson 18 of 25

Single-Line Comments

A free lesson from Swipe Coding · C++, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
Two slashes and you're commenting — C++'s simplest kind of note. ✏️

The simplest way to write a comment in C++ is with two forward slashes: //. Everything on the same line after // is completely ignored by the compiler.

#include <iostream>

int main() {
    // Prints a greeting
    std::cout << "Hello!" << std::endl;
    return 0;
}
The line with // is ignored. The line with std::cout runs completely normally.

A single-line comment doesn't have to sit on its own line. It can also appear directly after code on the same line — this is called an "inline comment".

std::cout << "Hello!" << std::endl; // prints "Hello!" to the console
C++ executes the code before the // and simply ignores the rest of the line after it.
Fill in the blank

A single-line comment in C++ begins with the characters ____.

🤖
Turning off code without deleting it is a handy trick. 🔌

With //, you can also temporarily "turn off" an entire line of code without deleting it. This is called "commenting out". The compiler then treats that line as if it weren't there at all.

int main() {
    std::cout << "Line 1" << std::endl;
    // std::cout << "Line 2" << std::endl;
    std::cout << "Line 3" << std::endl;
    return 0;
}
"Line 2" is never printed because the line is commented out. Only "Line 1" and "Line 3" appear.
Quick check

What does this program print?

std::cout << "A" << std::endl;
// std::cout << "B" << std::endl;
std::cout << "C" << std::endl;
🤖
One small catch about how far a // comment actually reaches. 👀

Important: // only comments out text up to the end of the current line. A line break automatically ends the comment — the next line is read as normal code again.

True or false

A // comment automatically also applies to the next line of code.

Keep your streak going in Swipe Coding

Swipe through every level of C++ on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →