C++ Basics Cheatsheet

C++ Basics Cheatsheet (Syntax + Examples for Beginners)

Learn C++ fundamentals fast with this beginner-friendly cheatsheet. Master syntax, understand core concepts, and start writing code immediately. Use this page to revise quickly, then move to advanced topics when ready.

Quick Navigation

1. Variables & Data Types

Core building blocks of every C++ program.

cpp
01int age = 25;
02float price = 10.5;
03double pi = 3.14159;
04char grade = 'A';
05string name = "Schoolabe";
06bool isActive = true;

Key Notes

  • int stores whole numbers
  • float and double store decimals (double is more precise)
  • char stores a single character
  • string stores text
  • bool stores true or false

Interview Insight

The difference between float and double is a common interview question.

2. Input / Output (cin & cout)

Take input and print output with streams.

cpp
01#include <iostream>
02using namespace std;
03
04int main() {
05 int x;
06 cin >> x;
07 cout << "Value: " << x;
08}

Key Notes

  • cin takes input from the user
  • cout prints output to the screen
  • << sends data to output
  • >> receives input

These appear in almost every C++ program.

3. Operators & Expressions

Use operators to build expressions and calculations.

cpp
01int a = 10, b = 5;
02
03cout << (a + b); // 15
04cout << (a - b); // 5
05cout << (a * b); // 50
06cout << (a / b); // 2
07cout << (a % b); // 0

Common Trap

cpp
01int x = 5 / 2; // Output = 2

Integer division removes decimals. Use 5.0 / 2 to get 2.5.

4. Control Flow

Make decisions and repeat logic.

If-Else

cpp
01if (age >= 18) {
02 cout << "Adult";
03} else {
04 cout << "Minor";
05}

For Loop

cpp
01for (int i = 0; i < 5; i++) {
02 cout << i;
03}

While Loop

cpp
01int i = 0;
02while (i < 5) {
03 cout << i;
04 i++;
05}

Interview Tip

Know when to use each loop and how they differ in structure and readability.

5. Functions

Write reusable logic and keep code clean.

cpp
01int add(int a, int b) {
02 return a + b;
03}
04
05// Usage
06cout << add(3, 4); // 7

Why Functions?

  • Reusability without copy-paste
  • Cleaner, more readable code
  • Better structure for teamwork

6. Arrays & Strings

Store multiple values and work with text.

Arrays

cpp
01int arr[3] = {1, 2, 3};
02cout << arr[0]; // 1

Strings

cpp
01#include <string>
02
03string name = "Schoolabe";
04cout << name.length();

Key Notes

  • Array indexing starts from 0
  • Arrays have fixed size
  • Strings are more flexible for text

7. Common Mistakes (Beginner Traps)

Avoid these to save time and frustration.

Uninitialized Variable

cpp
01int x;
02cout << x; // Garbage value

Assignment vs Comparison

cpp
01if (a = 5) // WRONG
02if (a == 5) // CORRECT

Missing Semicolon

cpp
01int a = 10 // Error

8. Quick Practice (Test Yourself)

Try before checking answers.

Q1

cpp
01int x = 10;
02cout << x % 4;

Answer: 2

Q2

cpp
01for (int i = 1; i <= 3; i++) {
02 cout << i;
03}

Output: 123

Q3

cpp
01int a = 5;
02if (a > 3) {
03 cout << "Yes";
04}

Output: Yes

Pro Tip

Run these in an editor and modify the values. Active practice builds confidence faster than passive reading.

What's Next

Move from quick revision to real C++ power.

Next Step: C++ Intermediate Cheatsheet

Pointers, references, vectors, and memory basics to level up from beginner to job-ready.

Open Intermediate Cheatsheet

When to Use This Page

  • Before coding for quick revision
  • During coding as a syntax reference
  • After learning to reinforce fundamentals
  • Before interviews for rapid recall

Keep Improving

Small habits that make a big difference.

  • Add your own notes while revising
  • Highlight weak areas and revisit weekly
  • Practice consistently to build speed

FAQs

Is this enough to learn C++?

No. This cheatsheet is for quick revision. Use the full course for deeper learning.

How long should I spend here?

10 to 15 minutes per session plus practice is enough for most learners.

What should I do next?

Move to the intermediate cheatsheet and practice problems to build real C++ fluency.

Product Insight (For You)

This page is optimized for speed and action.

Searchable

SEO entry point for C++ beginners.

Scannable

Short sections for fast revision.

Actionable

Practice prompts built in.