---
### Conditional Statements and Loops in C++
When you’re writing code in C++, **conditional statements** and **loops** are
essential for controlling the flow of your program. They help your program make
decisions and repeat certain actions when needed. Let’s dive into these concepts
and how they work.
---
### Conditional Statements
Conditional statements are like the “decision-makers” of your program. They help
your program decide which path to take based on certain conditions. In C++, there
are two main types of conditional statements: **if statements** and **switch
statements**.
- **If Statements**: This is the most basic form of decision-making. You can run a
block of code only if a condition is true. For example:
```cpp
int num = 10;
if (num > 5) {
std::cout << "Num is greater than 5." << std::endl;
}
```
If `num` is greater than 5, the code inside the if block will run. You can also
use **else** and **else if** for other conditions if the first one isn't true.
- **Switch Statements**: These are useful when you have several possible conditions
to check. It’s a bit like a series of if statements, but more organized. For
example:
```cpp
int num = 3;
switch (num) {
case 1:
std::cout << "Num is 1." << std::endl;
break;
case 2:
std::cout << "Num is 2." << std::endl;
break;
case 3:
std::cout << "Num is 3." << std::endl;
break;
default:
std::cout << "Num is not 1, 2, or 3." << std::endl;
}
```
In this example, the program checks the value of `num` and executes the
corresponding block of code. If `num` isn’t 1, 2, or 3, the `default` block runs.
---