🎯 What You'll Learn
- Basic C++ program structure
- Variables and data types
- Input/Output (cin, cout)
- Operators and expressions
- Conditionals (if/else, switch)
- Loops (for, while, do-while)
📚 Core Concepts
1. Basic Program Structure
Every C++ program follows this structure:
#include <iostream> // Include input/output library
using namespace std;
int main() {
// Your code here
cout << "Hello, World!" << endl;
return 0; // Exit program successfully
}
💡 Remember: C++ is case-sensitive! "Main" is different from "main"
2. Variables & Data Types
// Integer types
int age = 18; // Whole numbers (-2147483648 to 2147483647)
long bigNum = 1000000000L; // Larger integers
short smallNum = 100; // Smaller integers (-32768 to 32767)
// Floating point
float price = 9.99f; // Decimal numbers (6-7 digits precision)
double pi = 3.14159265359; // More precision (15 digits)
// Character and string
char grade = 'A'; // Single character
string name = "John"; // Text (needs #include <string>)
// Boolean
bool isPassing = true; // true or false
// Display values
cout << "Age: " << age << endl;
cout << "Name: " << name << endl;
✏️ Try it: Create variables for your name, age, height, and whether you're a student. Print them all.
3. Input & Output
// Output with cout
cout << "Hello World" << endl; // endl = new line
cout << "Score: " << 95 << endl;
// Input with cin
int age;
string name;
cout << "Enter your age: ";
cin >> age;
cout << "Enter your name: ";
cin >> name; // Warning: cin stops at spaces!
// For full line input with spaces
string fullName;
cout << "Enter full name: ";
cin.ignore(); // Clear buffer
getline(cin, fullName);
cout << "Hello, " << fullName << "!" << endl;
4. Operators
// Arithmetic operators
int a = 10, b = 3;
cout << a + b; // 13 (addition)
cout << a - b; // 7 (subtraction)
cout << a * b; // 30 (multiplication)
cout << a / b; // 3 (division - integer!)
cout << a % b; // 1 (remainder/modulo)
// Increment/Decrement
int x = 5;
x++; // x becomes 6 (post-increment)
++x; // x becomes 7 (pre-increment)
x--; // x becomes 6 (post-decrement)
// Comparison operators
10 > 5 // true
10 < 5 // false
10 >= 10 // true
10 == 10 // true (equal to)
10 != 5 // true (not equal to)
// Logical operators
true && true // AND: true
true || false // OR: true
!true // NOT: false
5. Conditionals
// If statement
int score = 85;
if (score >= 90) {
cout << "Grade: A" << endl;
} else if (score >= 80) {
cout << "Grade: B" << endl;
} else if (score >= 70) {
cout << "Grade: C" << endl;
} else {
cout << "Grade: F" << endl;
}
// Ternary operator
string result = (score >= 60) ? "Pass" : "Fail";
// Switch statement
int day = 3;
switch(day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
default:
cout << "Other day" << endl;
}
💡 Important: Don't forget "break" in switch statements! Without it, code falls through to next case.
6. Loops
// For loop - when you know how many times
for (int i = 0; i < 5; i++) {
cout << i << " "; // Output: 0 1 2 3 4
}
cout << endl;
// While loop - when you don't know how many times
int count = 0;
while (count < 3) {
cout << count << " ";
count++;
}
// Do-while loop - runs at least once
int num = 10;
do {
cout << num << " ";
num--;
} while (num > 0);
// Break and continue
for (int i = 0; i < 10; i++) {
if (i == 5) break; // Exit loop at 5
if (i % 2 == 0) continue; // Skip even numbers
cout << i << " "; // Output: 1 3
}
✏️ Try it: Write a loop that prints all even numbers from 2 to 20.
7. Common Pitfalls
// ❌ WRONG: Using = instead of ==
if (x = 5) { // This ASSIGNS 5 to x!
// ...
}
// ✅ CORRECT: Use == for comparison
if (x == 5) { // This COMPARES x with 5
// ...
}
// ❌ WRONG: Forgetting semicolons
int x = 5 // Missing semicolon!
// ✅ CORRECT: Always end statements with ;
int x = 5;
// ❌ WRONG: Integer division surprise
cout << 5 / 2; // Output: 2 (not 2.5!)
// ✅ CORRECT: Use doubles for decimals
cout << 5.0 / 2.0; // Output: 2.5
🛠️ Practice Projects
Project 1: Simple Calculator
Build a calculator that performs basic operations
Features to add:
- Input two numbers
- Choose operation (+, -, *, /)
- Display result
- Handle division by zero
- Loop to do multiple calculations
Project 2: Number Guessing Game
Classic guessing game with hints
Features to add:
- Generate random number (1-100)
- Get user guesses
- Give "higher" or "lower" hints
- Count attempts
- Display win message
// Hint for random numbers:
#include <cstdlib>
#include <ctime>
srand(time(0));
int random = rand() % 100 + 1;
Project 3: Grade Calculator
Calculate student grades and averages
Features to add:
- Input scores for 5 subjects
- Calculate average
- Determine letter grade
- Show if passed/failed
- Display highest and lowest scores