C++ Basics for Beginners: Your First Steps in Programming.
Programming is the skill of giving a computer clear, step-by-step instructions to solve a problem. C++ is one of the most widely used languages for doing that, powering everything from operating systems and games to banking software and embedded devices. This article walks you through the core building blocks of C++ so you can write and understand your first programs.
What Is C++?
C++ is a general-purpose, compiled programming language. "Compiled" means that the code you write is translated by a program called a compiler into machine instructions the computer can run directly. This is one reason C++ programs are fast.
C++ grew out of the C language and added support for object-oriented programming, but you don't need to know any of that to begin. You can learn the fundamentals first and add the advanced ideas later.
Your First Program
Every journey starts with "Hello, World!":
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
Here is what each part does:
#include <iostream>loads the library that lets your program read input and print output.using namespace std;lets you writecoutinstead ofstd::cout.int main()is the entry point. Every C++ program starts running here.cout << ... << endl;prints text to the screen and moves to a new line.return 0;tells the operating system that the program finished successfully.- Curly braces
{ }mark the beginning and end of a block of code, and each statement ends with a semicolon;.
Variables and Data Types
A variable is a named place in memory where you store a value. In C++, you must declare the type of every variable, which tells the compiler what kind of data it will hold.
| Type | What it stores | Example |
|---|---|---|
int |
Whole numbers | int age = 25; |
float |
Decimal numbers (lower precision) | float price = 9.99f; |
double |
Decimal numbers (higher precision) | double pi = 3.14159; |
char |
A single character | char grade = 'A'; |
bool |
true or false | bool isReady = true; |
string |
Text | string name = "Sara"; |
To use string, add #include <string> at the top of your file.
A few rules for naming variables: use letters, digits, and underscores, never start with a digit, and avoid C++ keywords such as int or return. Remember that C++ is case-sensitive, so score and Score are different variables.
Input and Output
You already met cout for output. For input, use cin:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You are " << age << " years old." << endl;
return 0;
}
Think of the arrows as showing the direction of data: cout << pushes data out to the screen, and cin >> pulls data in from the keyboard.
Operators
Operators let you work with values.
Arithmetic operators: +, -, *, /, and % (remainder). Be careful with division: 7 / 2 gives 3 when both numbers are integers, because the decimal part is dropped. Use 7.0 / 2 to get 3.5.
Comparison operators: == (equal), != (not equal), <, >, <=, >=. These produce a bool result.
Logical operators: && (and), || (or), ! (not). They combine conditions, for example age >= 18 && hasId.
A classic beginner mistake is writing = (assignment) when you meant == (comparison).
Making Decisions with if and else
Programs become useful when they can choose between actions:
int score;
cout << "Enter your score: ";
cin >> score;
if (score >= 90) {
cout << "Excellent" << endl;
} else if (score >= 50) {
cout << "Pass" << endl;
} else {
cout << "Try again" << endl;
}
C++ checks the conditions from top to bottom and runs the first block whose condition is true.
Repeating Tasks with Loops
Loops run the same code several times. The for loop is ideal when you know how many repetitions you need:
for (int i = 1; i <= 5; i++) {
cout << "Number " << i << endl;
}
The three parts inside the parentheses are the starting value, the condition to keep going, and the step after each round. When you don't know the number of repetitions in advance, use a while loop:
int n = 1;
while (n <= 100) {
n = n * 2;
}
cout << n << endl;
Comments
Comments are notes for humans that the compiler ignores:
// This is a single-line comment
/* This is a
multi-line comment */
Good comments explain why you did something, not just what the code does.
Common Beginner Mistakes
- Forgetting the semicolon at the end of a statement.
- Using
=instead of==inside a condition. - Using a variable before giving it a value.
- Mismatched curly braces or parentheses.
- Integer division giving unexpected results.
When the compiler shows an error, read the first message and the line number it points to. Fixing the first error often removes several others.
Where to Go Next
Once you are comfortable with these basics, the natural next topics are arrays, functions, pointers, and then object-oriented programming with classes. The best way to learn is to practice: write small programs such as a calculator, a number-guessing game, or a temperature converter, and change them until you understand every line.
Programming is learned by doing, so open your editor and write your first program today.
Share this article
