C++ syntax has a few rules you need to get comfortable with. Do not worry โ they are not complicated. They are just rules. Once you learn them, they become second nature.
Semicolons
Every statement in C++ ends with a semicolon ;. Think of it like a period
at the end of a sentence. It tells the compiler "this statement is done, move to the next one."
Forgetting a semicolon is the most common error beginners make. The compiler will yell
at you, but do not worry โ you will learn to spot it quickly.
Curl braces { }
Curly braces group blocks of code together. Everything inside { } belongs
to the same block. Functions, loops, if-statements โ they all use braces to mark
where they start and end. Indentation is not required by C++, but please use it.
Your future self will thank you.
The main() Function
Every C++ program must have a main() function. This is where your program
starts running. Without main(), the compiler will not know where to begin.
The int before it means the function returns an integer to the operating
system โ usually 0 to say "everything went fine."
#include and Namespace
#include is a preprocessor directive that tells the compiler to include
a file (like a library header). #include <iostream> gives you
input/output functionality. The using namespace std; line lets you write
cout instead of std::cout. Some people prefer one style,
some prefer the other. Both work.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
Multiple Statements
You can have as many statements as you want inside a block. Just remember the semicolons.
#include <iostream>
using namespace std;
int main() {
cout << "First line.";
cout << "Second line.";
cout << "Third line.";
return 0;
}