Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Comments | Basics
Introduction to C++

bookComments

Comments in programming are parts of code that are ignored. It’s a good rule to use them to improve the readability of your code with specific descriptions of what a certain part of the code does. Comments also help when you want to test a certain part of the program without deleting the other code.

To mark the line(or part of the line) as a comment, begin the respective string with //.

For example, in this case, the line which outputs the message "Hello!" will be ignored:

123456789
#include &lt;iostream&gt; using namespace std; int main() { &nbsp&nbsp&nbsp&nbsp // cout << "Hello! \n"; &nbsp&nbsp&nbsp&nbsp cout << "I love C++! "; &nbsp&nbsp&nbsp&nbsp cout << "Goodbye!"; &nbsp&nbsp&nbsp&nbspreturn 0; }
copy

This type of comment is also good for code explanation:

cout << "Goodbye!";  // Print the message

If you want the compiler to ignore several lines use multi-line comments. They start with /* and end with */:

123456789
#include &lt;iostream&gt; using namespace std; int main() { &nbsp&nbsp&nbsp&nbsp&nbsp/* cout << "Hello!"; &nbsp&nbsp&nbsp&nbsp cout << "I love C++! "; */ &nbsp&nbsp&nbsp&nbsp cout << "Goodbye!"; // Print the message &nbsp&nbsp&nbsp&nbsp return 0; }
copy
question-icon

The code must only print the message: "I have learned C++ basics!". Fill gaps in the code:

#include <iostream>
using namespace std;

int main() {
    
cout << "Hello!\n";
    cout << "World!\n";

    cout << "I have learned C++ basics! \n";
  Print the message
    return 0;
}
I’m learning C++!

Click or drag`n`drop items and fill in the blanks

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 4

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 3.33

bookComments

Svep för att visa menyn

Comments in programming are parts of code that are ignored. It’s a good rule to use them to improve the readability of your code with specific descriptions of what a certain part of the code does. Comments also help when you want to test a certain part of the program without deleting the other code.

To mark the line(or part of the line) as a comment, begin the respective string with //.

For example, in this case, the line which outputs the message "Hello!" will be ignored:

123456789
#include &lt;iostream&gt; using namespace std; int main() { &nbsp&nbsp&nbsp&nbsp // cout << "Hello! \n"; &nbsp&nbsp&nbsp&nbsp cout << "I love C++! "; &nbsp&nbsp&nbsp&nbsp cout << "Goodbye!"; &nbsp&nbsp&nbsp&nbspreturn 0; }
copy

This type of comment is also good for code explanation:

cout << "Goodbye!";  // Print the message

If you want the compiler to ignore several lines use multi-line comments. They start with /* and end with */:

123456789
#include &lt;iostream&gt; using namespace std; int main() { &nbsp&nbsp&nbsp&nbsp&nbsp/* cout << "Hello!"; &nbsp&nbsp&nbsp&nbsp cout << "I love C++! "; */ &nbsp&nbsp&nbsp&nbsp cout << "Goodbye!"; // Print the message &nbsp&nbsp&nbsp&nbsp return 0; }
copy
question-icon

The code must only print the message: "I have learned C++ basics!". Fill gaps in the code:

#include <iostream>
using namespace std;

int main() {
    
cout << "Hello!\n";
    cout << "World!\n";

    cout << "I have learned C++ basics! \n";
  Print the message
    return 0;
}
I’m learning C++!

Click or drag`n`drop items and fill in the blanks

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 4
some-alt