Skip to main content

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.

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

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.
// 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.
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.
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.
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.
constexpr int compute() {
    if consteval {
        return 1; // Executes only during compile-time evaluation
    } else {
        return 0; // Executes during runtime evaluation
    }
}
Master C++ with Deep Grasping Methodology!Learn More