
A program is a list of instructions for the computer. It follows them one at a time, from top to bottom. The classic first program just prints one line of text.
ON THIS PAGE
//THE EXAMPLE
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}Hello, World!
//LINE BY LINE
Watch it run
The computer starts at the very top line. This one brings in the printing tools.
The parts every program has
Every C++ program has the same skeleton. First you bring in the tools you need, then you write main, which is where the program starts.
#include <iostream>— brings in the tools for printing to the screen.using namespace std;— lets you writecoutinstead of the longerstd::cout.int main() { … }— the program always starts here. It must exist.return 0;— tells the operating system everything finished correctly.
You cannot rename main. The computer looks for that exact name.
Semicolons and braces
In C++ every statement ends with a semicolon (`;`). It works like a full stop: it says "this instruction is finished".
Curly braces { } group statements together into one block. The body of main is such a block.
int main() {
cout << "one"; // ← ; хэрэгтэй
cout << "two"; // ← ; хэрэгтэй
} // ← хаалтын дараа ; хэрэггүйA missing semicolon is the most common beginner error. The compiler usually points at the next line, so look one line above where it complains.
How code becomes a program
The computer does not understand your text directly. A program called a compiler translates it into machine code, and then that result is run.
- 1.Write — you type your code into
main.cpp. - 2.Compile — the compiler checks for errors and produces machine code.
- 3.Run — the resulting program executes and prints its output.
This is why C++ has two kinds of error: compile errors (it never runs) and run-time errors (it runs but behaves wrongly).