> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C++ If Statement

The `if` statement is a primary control flow construct in C++ that conditionally directs program execution based on the evaluation of a condition. At runtime, the program evaluates the provided condition; if the condition yields a value contextually convertible to `true`, the associated statement or block is executed. If it evaluates to `false`, execution bypasses the block, optionally falling through to an `else if` or `else` clause.

## Standard Syntax

```cpp theme={"dark"}
if (condition) {
    // Executed if condition is contextually true
} else if (alternative_condition) {
    // Executed if condition is false and alternative_condition is contextually true
} else {
    // Executed if all preceding conditions evaluate to false
}
```

## Evaluation Mechanics

The `condition` within the parentheses can be either an expression or a declaration (supported since C++98). In either case, the resulting evaluated expression or the initialized variable must be contextually convertible to `bool`.

```cpp theme={"dark"}
// Expression as condition
if (a == b) { }

// Declaration as condition
if (int* ptr = get_pointer()) { }
```

C++ enforces specific conversion rules during this evaluation:

* **Boolean Types:** Evaluates directly to `true` or `false`.
* **Arithmetic Types (Integers, Floating-Point):** A value of exactly `0` or `0.0` evaluates to `false`. Any non-zero value evaluates to `true`.
* **Pointer Types:** A null pointer (`nullptr` or `NULL`) evaluates to `false`. Any non-null pointer evaluates to `true`, regardless of whether the memory address it holds is valid (e.g., dangling pointers or uninitialized pointers holding non-zero garbage values still evaluate to `true`).
* **Class Types:** The class must define a user-defined conversion operator to a type that is contextually convertible to `bool` (such as `bool`, `int`, `void*`, or another class type with such a conversion).

## C++17: `if` with Initialization

C++17 introduced the ability to include an `init-statement` directly before the condition. The lexical scope of the variable declared in the `init-statement` is strictly bound to the `if` block and any subsequent `else if` or `else` blocks.

```cpp theme={"dark"}
if (init-statement; condition) {
    // Variable declared in init-statement is in scope here
} else {
    // Variable is also in scope here
}
```

## C++17: `if constexpr`

The `if constexpr` statement shifts the evaluation of the condition from runtime to compile-time. The condition must be a core constant expression.

If the condition evaluates to `true`, the compiler discards the `else` branch entirely. If `false`, the `if` branch is discarded. The discarded statements are not instantiated, meaning they do not need to be well-formed for types where the condition fails.

```cpp theme={"dark"}
template <typename T>
void process(T value) {
    if constexpr (std::is_integral_v<T>) {
        // Compiled only if T is an integral type
    } else {
        // Compiled only if T is not an integral type
    }
}
```

## C++20: `[[likely]]` and `[[unlikely]]` Attributes

C++20 introduced statement attributes to provide hints to the compiler's optimizer regarding branch prediction. These attributes are placed immediately before the statement block to indicate which execution path is statistically more probable.

```cpp theme={"dark"}
if (condition) [[likely]] {
    // Compiler optimizes for this path being taken
} else [[unlikely]] {
    // Compiler optimizes for this path being avoided
}
```

## C++23: `if consteval`

C++23 introduced `if consteval`, a control flow statement that dictates execution based on whether the current context is manifestly constant-evaluated (e.g., during compile-time execution of a `constexpr` or `consteval` function). It is not an expression, does not evaluate to a boolean value, and does not require a condition expression. It directly executes the first block if the context is constant-evaluated, and the `else` block (if present) otherwise.

```cpp theme={"dark"}
constexpr int compute() {
    if consteval {
        return 1; // Executes only during compile-time evaluation
    } else {
        return 0; // Executes during runtime evaluation
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
