Quick Syntax Guide

C++ Quick Syntax Guide

The essential reference for beginners. Use this "cheat sheet" to quickly recall syntax, data types, and logic structures during your practice sessions.

Standard I/O
cin >> / cout <<
Loops
for / while / do
Data Types
int / double / string
Memory
&pointer / *reference

1. Basic Structure

#include <iostream> // Header library
using namespace std;   // Use standard names

int main() {           // Main function
    cout << "Hello World!"; 
    return 0;           // Exit successfully
}

2. Common Data Types

TypeExampleDescription
int42Whole numbers
double3.14Floating point
char'A'Single character
string"Text"Sequence of chars
booltrueLogic (1 or 0)

3. Conditionals (if-else)

if (condition) {
    // code if true
} else if (another) {
    // another check
} else {
    // default case
}

4. Loops (Iteration)

// FOR loop
for (int i=0; i < 5; i++) {
    cout << i;
}

// WHILE loop
while (condition) {
    // repeats until false
}

5. Functions & Modular Code

// Declaration: type name(parameters)
int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(5, 3); // Function call
    return 0;
}

Functions are essential for C++ modular programming. Always define the return type and ensure parameters match the call signature. Using void if no value is returned.

6. Arrays & Vectors

Static Array:

int arr[5] = {1, 2, 3, 4, 5};

Dynamic Vector (Modern C++):

vector<int> v = {1, 2, 3};
v.push_back(4); // Adds 4 to end

Mastering C++ Programming: Frequently Asked Questions

How to read input in C++?
Use std::cin with the extraction operator >> to capture user input into variables.

Difference between Array and Vector?
Arrays have a fixed size, while std::vector is dynamic and can grow during runtime.

What is #include <iostream>?
It is a header file library that allows working with input and output objects like cout.

Found what you were looking for?

Go back to the Practice Lab and apply this syntax to real problems.

Back to Exercises

C++ Syntax Cheat-Sheet
Standard Headers Namespaces STL Vectors :: Operator Pointers & Refs Class Syntax Auto Keyword

Share this C++ Quick Syntax Guide

Help other students master C++ by sharing this guide.

© C++ Programming Exercises. Master the art of performance coding.