Comments, Whitespace, and Code Style Basics
Code is read far more often than it’s written. Even as a beginner, you’ll find yourself scrolling back through the code you wrote last week, trying to remember what you were thinking. The compiler doesn’t care about how your code looks. It turns your source into a runnable program either way. But humans do care, and writing for humans is part of the job.
C++ gives you a few ways to do this: comments to explain intent, and whitespace to give code visual structure.
Comments
A comment is text in your source file that the compiler completely ignores. It exists purely for the human reading the code.
C++ has two styles.
Single-line comments start with // and run to the end of that line:
#include <iostream> // gives us access to coutBlock comments start with /* and end with */, and can span multiple lines:
In practice, most working C++ code uses // almost exclusively. Block comments are mainly used for longer descriptions at the top of a file.
Either way, comments have no impact on how the program runs. You can add, remove, or change them freely without affecting the output.
What makes a good comment
The most common mistake beginners make is commenting what the code does instead of why it does it.
#include <iostream> // include the iostream libraryThis comment adds little clarity to what the code already says. The following is more helpful:
#include <iostream> // needed for std::coutIf a line is already clear, a comment just adds noise. Comments earn their place when they explain a decision or give context that isn’t immediately obvious from reading the code itself.
Whitespace
Whitespace means spaces, tabs, and blank lines. The compiler ignores all of it. You could write a valid C++ program on a single line, but no one would ever want to read it.
Here’s the Hello World program squashed down:
#include<iostream>int main(){std::cout<<"Hello, world!";return 0;}And here it is with normal whitespace:
#include <iostream>int main() { std::cout << "Hello, world!"; return 0;}This second version is the same program, but leads to a completely different reading experience. It shows its structure at a glance.
Indentation
You’ll notice that the body of main is indented one level in from the braces that contain it. The two most common choices are 2 spaces or 4 spaces per level. This course uses 4. Later, when your code has other kinds of statements (like if statements and loops) inside main, you’ll see that each block of code gets indented one more level.
Blank lines
Blank lines separate logical sections, the same way paragraphs separate ideas in prose. You can already see the convention in the above Hello World program: a blank line between the #include block and main, so the two sections don’t blur together.
Spaces around operators and symbols
There’s a space between #include and <iostream>. Notice the spaces around <<:
std::cout << "Hello, world!";Compare that to:
std::cout<<"Hello, world!";Both compile, but the first is easier to scan.
Paying attention to spaces around operators is a habit worth building from the start.