
The C++ syntax is easy to learn and implement with user friendly syntaxes and concepts explained with examples in this article below. The first step to learn any language is to understand the rules and regulations to implement the syntax.
The C++ syntax is a predefined rules and directives which guides programmers and developers in building their codebase. In this article, let us learn more about C++ syntax for loops and C++ syntax cheat sheet.
C++ syntax dictates everything from how programs are structured to how variables, functions, and control statements are written.
| #include <iostream> int main() { std::cout << "Hello, World!"; return 0; } |
| #include <iostream> // Include library for input-output int main() { std::cout << "Hello, World!"; return 0; } |
| int age = 25; // Declare and initialize an integer float price = 99.99; // Declare and initialize a float char grade = 'A'; // Declare and initialize a character |
| #include <iostream> int main() { int age; std::cout << "Enter your age: "; // Output message std::cin >> age; // Take input from user std::cout << "You are " << age << " years old."; // Display output return 0; } |
| #include <iostream> int main() { int number = 10; if (number > 0) { std::cout << "Positive"; } else if (number < 0) { std::cout << "Negative"; } else { std::cout << "Zero"; } return 0; } |
| for (int i = 0; i < 5; i++) { std::cout << i << " "; } |
| int x = 0; while (x < 5) { std::cout << x << " "; x++; } |
| int x = 0; do { std::cout << x << " "; x++; } while (x < 5); |
| #include <iostream> int add(int a, int b) { // Function declaration return a + b; } int main() { int result = add(5, 3); // Call function std::cout << "Sum: " << result; return 0; } |
| #include <iostream> int main() { int numbers[3] = {10, 20, 30}; std::cout << numbers[1]; return 0; } |
| #include <iostream> class Car { public: std::string brand; void display() { std::cout << "Brand: " << brand; } }; int main() { Car myCar; // Create object myCar.brand = "Toyota"; myCar.display(); return 0; } |
| #include <fstream> #include <iostream> int main() { std::ofstream file("example.txt"); file << "Hello, File!"; // Write to file file.close(); std::ifstream infile("example.txt"); std::string content; infile >> content; // Read from file std::cout << content; infile.close(); return 0; } |
| Concept | Syntax | Example |
| Headers | #include <header> | #include <iostream> |
| Namespace | using namespace namespace_name; | using namespace std; |
| Main Function | int main() { /* code */ return 0; } | int main() { cout << "Hello, World!"; return 0; } |
| Variable Declaration | data_type variable_name = value; | int x = 10; |
| Input/Output | cin >> variable; cout << "message"; | cin >> x; cout << x; |
| Conditional | if (condition) { /* code */ } else { /* code */ } | if (x > 0) { cout << "Positive"; } else { cout << "Negative"; } |
| Loops | for(init; condition; update) { /* code */ } while(condition) { /* code */ } | for (int i = 0; i < 10; i++) { cout << i; } |
| Functions | return_type function_name(parameters) { /* code */ } | int add(int a, int b) { return a + b; } |
| Classes | class ClassName { public: /* members */ private: /* members */ }; | class Car { public: string brand; }; |
| Object Creation | ClassName object_name; | Car myCar; |
| Pointers | data_type* pointer_name = &variable; | int x = 10; int* p = &x; |
| Arrays | data_type array_name[size]; | int arr[5] = {1, 2, 3, 4, 5}; |
| Vectors | #include <vector> vector<data_type> vector_name; | vector<int> v = {1, 2, 3}; |
| Strings | #include <string> string str = "text"; | string s = "Hello"; cout << s; |
| Inheritance | class Derived : access_specifier Base { /* code */ }; | class Car : public Vehicle { /* code */ }; |
| Access Modifiers | public, private, protected | public: int x; private: int y; |
| Dynamic Memory | data_type* pointer = new data_type; delete pointer; | int* p = new int; delete p; |
| Exception Handling | try { /* code */ } catch (exception_type e) { /* code */ } | try { throw 20; } catch (int e) { cout << e; } |
| Templates | template<typename T> return_type function_name(T param) { /* code */ } | template<typename T> T add(T a, T b) { return a + b; } |
| File Handling | #include <fstream> ifstream inFile("file.txt"); ofstream outFile("file.txt"); | ifstream inFile("input.txt"); string data; inFile >> data; cout << data; inFile.close(); |