C++ Basic Syntax Cheat Sheet

4 min read Jul 01, 2024
C++ Basic Syntax Cheat Sheet

C++ Basic Syntax Cheat Sheet

This cheat sheet provides a quick overview of essential C++ syntax concepts.

Basic Syntax

  • Comments:
    • Single-line: // This is a comment
    • Multi-line: /* This is a multi-line comment */
  • Data Types:
    • Integer: int (e.g., int age = 25;)
    • Floating-point: float (e.g., float height = 1.75;)
    • Character: char (e.g., char grade = 'A';)
    • Boolean: bool (e.g., bool is_active = true;)
    • String: string (e.g., string name = "John Doe";)
  • Variables:
    • Declare a variable: data_type variable_name;
    • Initialize a variable: data_type variable_name = value;
  • Operators:
    • Arithmetic: +, -, *, /, % (modulo)
    • Comparison: == (equals), != (not equals), > (greater than), < (less than), >= (greater than or equals), <= (less than or equals)
    • Logical: && (and), || (or), ! (not)
  • Control Flow:
    • if statement:
      if (condition) {
          // Code to execute if condition is true
      }
      
    • else if statement:
      if (condition1) {
          // Code to execute if condition1 is true
      } else if (condition2) {
          // Code to execute if condition2 is true
      }
      
    • else statement:
      if (condition) {
          // Code to execute if condition is true
      } else {
          // Code to execute if condition is false
      }
      
    • switch statement:
      switch (expression) {
      case value1:
          // Code to execute if expression == value1
          break;
      case value2:
          // Code to execute if expression == value2
          break;
      default:
          // Code to execute if no case matches
          break;
      }
      
    • for loop:
      for (initialization; condition; increment) {
          // Code to execute repeatedly
      }
      
    • while loop:
      while (condition) {
          // Code to execute repeatedly
      }
      
    • do-while loop:
      do {
          // Code to execute at least once
      } while (condition);
      

Input/Output

  • Input:
    • cin >> variable; (e.g., cin >> age;)
  • Output:
    • cout << "text"; (e.g., cout << "Hello world!";)
    • cout << variable; (e.g., cout << age;)

Functions

  • Function declaration:
    return_type function_name(parameter1, parameter2, ...);
    
  • Function definition:
    return_type function_name(parameter1, parameter2, ...) {
        // Function code
        return value;
    }
    
  • Function call:
    function_name(arguments);
    

Arrays

  • Declare an array:
    data_type array_name[size];
    
  • Initialize an array:
    int numbers[5] = {1, 2, 3, 4, 5};
    
  • Access array elements:
    array_name[index];
    

Pointers

  • Declare a pointer:
    data_type *pointer_name;
    
  • Assign a pointer:
    pointer_name = &variable;
    
  • Dereference a pointer:
    *pointer_name;
    

References

  • Declare a reference:
    data_type &reference_name = variable;
    

Structures

  • Define a structure:
    struct structure_name {
        data_type member1;
        data_type member2;
        // ...
    };
    
  • Create a structure variable:
    structure_name variable_name;
    
  • Access structure members:
    variable_name.member_name;
    

This cheat sheet provides a foundation for understanding C++ syntax. Remember to practice and explore further to deepen your knowledge.

Latest Posts