Geek Slack

Learn C++
About Lesson


C++ Syntax


C++ Syntax

Understanding the syntax of C++ is crucial for writing effective programs. This chapter will introduce you to the basic syntax of C++ and provide examples to help you grasp the concepts.

1. Basic Structure of a C++ Program

Every C++ program consists of functions and declarations. The main function is the entry point of any C++ program.

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

2. Comments

Comments are used to explain the code and are ignored by the compiler. C++ supports two types of comments:

  • Single-line comments: Start with //
  • Multi-line comments: Enclosed within /* */
// This is a single-line comment
/* This is a 
   multi-line comment */

3. Variables and Data Types

Variables are used to store data. Each variable must be declared with a data type before it can be used. Common data types include:

  • int – Integer
  • float – Floating point number
  • double – Double-precision floating point number
  • char – Character
  • bool – Boolean (true/false)
int myNum = 5;
float myFloat = 5.99;
double myDouble = 9.98;
char myChar = 'A';
bool myBool = true;

4. Operators

C++ supports various operators for performing operations on variables and values. Here are some common types of operators:

Arithmetic Operators

int sum = 5 + 3;      // Addition
int diff = 5 - 3;     // Subtraction
int prod = 5 * 3;     // Multiplication
int quotient = 5 / 3; // Division
int mod = 5 % 3;      // Modulus

Relational Operators

bool isEqual = (5 == 3);   // Equal to
bool notEqual = (5 != 3); // Not equal to
bool greater = (5 > 3);   // Greater than
bool less = (5 < 3);      // Less than
bool greaterEqual = (5 >= 3); // Greater than or equal to
bool lessEqual = (5 <= 3);    // Less than or equal to

Logical Operators

bool andOp = (5 > 3 && 3 < 5); // Logical AND
bool orOp = (5 > 3 || 3 > 5);  // Logical OR
bool notOp = !(5 > 3);         // Logical NOT

5. Control Structures

Control structures are used to control the flow of the program. Common control structures include:

Conditional Statements

int num = 10;
if (num > 0) {
    std::cout << "Number is positive" << std::endl;
} else if (num < 0) {
    std::cout << "Number is negative" << std::endl;
} else {
    std::cout << "Number is zero" << std::endl;
}

Loops

// For loop
for (int i = 0; i < 5; i++) {
    std::cout << i << std::endl;
}

// While loop
int j = 0;
while (j < 5) {
    std::cout << j << std::endl;
    j++;
}

// Do-while loop
int k = 0;
do {
    std::cout << k << std::endl;
    k++;
} while (k < 5);

6. Functions

Functions are blocks of code that perform a specific task. They help in modularizing the code.

#include <iostream>

// Function declaration
int add(int a, int b);

int main() {
    int sum = add(5, 3);  // Function call
    std::cout << "Sum: " << sum << std::endl;
    return 0;
}

// Function definition
int add(int a, int b) {
    return a + b;
}

7. Arrays

Arrays are used to store multiple values of the same type in a single variable.

#include <iostream>

int main() {
    int myArray[5] = {10, 20, 30, 40, 50};  // Array declaration and initialization
    for (int i = 0; i < 5; i++) {
        std::cout << myArray[i] << std::endl;
    }
    return 0;
}

8. Strings

Strings in C++ are objects that represent sequences of characters.

#include <iostream>
#include <string>

int main() {
    std::string myString = "Hello, World!";
    std::cout << myString << std::endl;
    return 0;
}

Conclusion

This chapter introduced the basic syntax of C++ and provided examples to help you understand key concepts. With these foundations, you can start exploring more complex features of C++ and develop your programming skills further.

Join the conversation