This file is designed to teach basic C++ concepts to beginners. It contains detailed explanations and simple examples so you can cover all foundational topics in one session. Functions, arrays, strings, and vectors are excluded in this version.
- Understand the general structure of a C++ program and how it executes.
- Learn to define variables and basic data types.
- Understand operators, especially increment and decrement operators.
- Get familiar with conditionals (
if,else,switch) and loops (for,while,do-while).
C++ is a powerful and widely used programming language in fields like engineering, simulation, and game development. Every C++ program starts from the main() function and usually contains statements for input and output.
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}Explanation:
#include <iostream>allows using input and output commands.using namespace std;removes the need to writestd::beforecoutorcin.main()is the starting point of the program.return 0;indicates successful execution.
In C++, cout is used for output, and cin for input.
#include <iostream>
using namespace std;
int main() {
string name;
int age;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
cin >> age;
cout << "Hello " << name << ", you are " << age << " years old." << endl;
return 0;
}Note:
coutstands for console output andcinstands for console input.
In C++, variables store data and must have a specific data type.
| Data Type | Description | Example |
|---|---|---|
int |
Integer | int x = 10; |
double |
Decimal number | double pi = 3.14; |
char |
Single character | char grade = 'A'; |
bool |
True or false | bool isOk = true; |
string |
Text (word or sentence) | string name = "Ali"; |
Important Notes:
- Variable names cannot start with a number.
- Case-sensitive (
Ageandageare different). - Multiple variables of the same type can be declared together:
int a = 10, b = 20, c = 30;
Example:
int number = 5;
double price = 12.5;
char letter = 'C';
bool passed = false;| Operator | Meaning | Example |
|---|---|---|
+ |
Addition | x + y |
- |
Subtraction | x - y |
* |
Multiplication | x * y |
/ |
Division | x / y |
% |
Modulus | x % y |
| Operator | Meaning | Example |
|---|---|---|
== |
Equal to | x == y |
!= |
Not equal | x != y |
> |
Greater than | x > y |
< |
Less than | x < y |
>= |
Greater or equal | x >= y |
<= |
Less or equal | x <= y |
| Operator | Meaning | Example |
|---|---|---|
&& |
AND | (x > 0 && y > 0) |
|| |
OR | (x > 0 || y > 0) |
! |
NOT | !(x > 0) |
These operators are used to increase or decrease a variable.
int i = 5;
i++; // i = 6
++i; // i = 7Prefix (++i) vs. Postfix (i++):
++i: increases first, then uses the value.i++: uses the value first, then increases.
Example:
int a = 5;
cout << a++ << endl; // prints 5, then a becomes 6
cout << ++a << endl; // a becomes 7, then prints 7Similarly:
int b = 5;
b--; // b = 4
--b; // b = 3Conditionals are used to make decisions in programs.
int number;
cout << "Enter a number: ";
cin >> number;
if (number > 0) {
cout << "Positive number" << endl;
} else if (number < 0) {
cout << "Negative number" << endl;
} else {
cout << "Zero" << endl;
}Loops execute a block of code multiple times. C++ has three main loop types:
Used when the number of iterations is known.
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}Used when the number of iterations is unknown and the loop continues as long as a condition is true.
int i = 1;
while (i <= 5) {
cout << i << " ";
i++;
}Similar to while, but executes at least once even if the condition is false.
int i = 1;
do {
cout << i << " ";
i++;
} while (i <= 5);break: exits the loop immediately.continue: skips to the next iteration without executing the remaining statements in the loop.
Example:
for (int i = 1; i <= 5; i++) {
if (i == 3) continue; // skip printing 3
if (i == 5) break; // exit loop when i is 5
cout << i << " ";
}
// output: 1 2 4Used when you want to check multiple possible values of a variable (instead of multiple if-else).
int day = 3;
switch (day) {
case 1:
cout << "Saturday";
break;
case 2:
cout << "Sunday";
break;
case 3:
cout << "Monday";
break;
case 4:
cout << "Tuesday";
break;
case 5:
cout << "Wednesday";
break;
default:
cout << "Invalid day";
}- W3Schools C++ Tutorial
- cppreference.com for