C++ Basics for Beginners: Variables, Decisions and Loops
Why learn C++?
C++ is one of the most respected programming languages in the world. It powers games, operating systems, browsers and high-performance software, and it is a core subject in many computer science degrees. It is stricter than Python — you must declare types and end lines with semicolons — but that strictness teaches you exactly how a computer handles data, which makes every other language easier afterwards.
Your first C++ program, explained line by line
#include <iostream>
using namespace std;
int main() {
cout << "Hello, Tahmeer Tech!";
return 0;
}#include <iostream>brings in input and output tools, includingcoutint main()is where every C++ program starts runningcout <<prints text to the screen;<<means “send this out”return 0;tells the computer the program finished successfully- Every statement ends with a semicolon
;— forgetting one is the most common beginner error
Variables and types — labelled boxes with a fixed shape
Unlike Python, C++ asks you to say what kind of data a variable holds before you use it:
int age = 20; // whole number
double price = 99.5; // decimal number
char grade = 'A'; // a single character, in single quotes
string name = "Nouman"; // text, needs #include <string>
bool passed = true; // true or falseChoosing the right type matters. Store a decimal in an int and the decimal part is cut off. Divide two integers, like 7 / 2, and C++ gives 3, not 3.5 — because integer division throws away the remainder. When you need decimals, use double.
Input and output — talking to the user
int marks;
cout << "Enter your marks: ";
cin >> marks;
cout << "You scored " << marks << " marks.";cin >> reads what the user types into the variable. Always print a clear prompt first, or the user will stare at a blank screen wondering what to type.
Decisions — if, else if, else
if (marks >= 80) {
cout << "Grade: A";
} else if (marks >= 65) {
cout << "Grade: B";
} else {
cout << "Keep practising";
}The conditions are checked top to bottom and only the first true branch runs. Two traps to memorise: = assigns a value, while == compares two values — writing if (marks = 80) by mistake changes the variable instead of testing it. And in C++, a condition in an if must be inside round brackets.
Loops — repeat without repeating yourself
A for loop is best when you know how many times to repeat, for example five subjects:
int total = 0;
for (int i = 0; i < 5; i++) {
int m;
cin >> m;
total = total + m;
}A while loop repeats while a condition stays true — perfect for “keep asking until the input is valid”. Every loop needs a way to finish: a counter that moves, or a condition that can become false. A loop that never finishes is called an infinite loop, and it is a rite of passage every programmer meets once.
Functions — reusable mini-programs
double average(int total, int count) {
return (double)total / count;
}A function takes inputs, does one job, and can return a result. Note (double)total: it converts the total to a decimal before dividing, so the average is not rounded down by integer division. Small functions with clear names make programs readable — and readable programs are easier to debug.
Practice task (60 minutes)
Write a C++ marks calculator: ask for 5 subject marks, use a loop to add them, calculate the average with a function, and print a grade with an if-else chain (80+ A, 65+ B, 50+ C, otherwise “Work harder”). Test it with all high marks, all low marks, and one invalid mark (for example 150) — then decide what your program should do with invalid input. You will build this properly in Lab 24.
Common mistakes to avoid
- Missing semicolons — the compiler error often points to the line after the real mistake
- Using
=when you mean==inside an if condition - Integer division surprises:
7 / 2is 3, not 3.5 - Declaring a variable inside a loop and trying to use it after the loop ends
- Copying code you cannot explain line by line
C++ = types declared up front, cout and cin for input and output, if-else for decisions, loops for repetition, functions for reuse. Master these five ideas and you can read almost any beginner C++ program.