and expressions, control structures, arrays and strings, functions and recursion, and pointers
and references:
**3.1 Data Types and Variables:**
In C++, data types represent different kinds of values that can be stored and manipulated in
variables. Here are some commonly used data types:
- Integer Types: `int`, `short`, `long`, `long long`
- Floating-Point Types: `float`, `double`
- Character Types: `char`
- Boolean Type: `bool`
- Void Type: `void`
Variables are named memory locations used to store values of a specific data type. You
declare variables by specifying the data type and an identifier:
```cpp
int age; // Declaration
age = 25; // Initialization
double pi = 3.14159; // Declaration and initialization
```
**3.2 Operators and Expressions:**
C++ provides various operators to perform operations on data. Some common operators
include:
- Arithmetic Operators: `+`, `-`, `*`, `/`, `%`
- Comparison Operators: `==`, `!=`, `<`, `>`, `<=`, `>=`
- Logical Operators: `&&`, `||`, `!`
- Assignment Operators: `=`, `+=`, `-=`, `*=`, `/=`
- Increment and Decrement Operators: `++`, `--`
- Bitwise Operators: `&`, `|`, `^`, `<<`, `>>`
Expressions are combinations of operators, constants, variables, and function calls that
produce a value. For example:
```cpp
int sum = 5 + 3; // Arithmetic expression
bool result = x > 10; // Comparison expression
bool isTrue = !isFalse; // Logical expression
```
**3.3 Control Structures:**
, Control structures allow you to control the flow of execution based on certain conditions.
Common control structures include:
- if-else Statement:
```cpp
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
```
- switch Statement:
```cpp
switch (variable) {
case value1:
// Code to execute if variable matches value1
break;
case value2:
// Code to execute if variable matches value2
break;
default:
// Code to execute if variable does not match any case
}
```
- Loops:
- while Loop:
```cpp
while (condition) {
// Code to execute repeatedly as long as the condition is true
}
```
- do-while Loop:
```cpp
do {
// Code to execute repeatedly at least once, then as long as the condition is true
} while (condition);
```
- for Loop:
```cpp
for (initialization; condition; update) {
// Code to execute repeatedly as long as the condition is true
}
```