TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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
Evaluation Mechanics
Thecondition 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.
- Boolean Types: Evaluates directly to
trueorfalse. - Arithmetic Types (Integers, Floating-Point): A value of exactly
0or0.0evaluates tofalse. Any non-zero value evaluates totrue. - Pointer Types: A null pointer (
nullptrorNULL) evaluates tofalse. Any non-null pointer evaluates totrue, regardless of whether the memory address it holds is valid (e.g., dangling pointers or uninitialized pointers holding non-zero garbage values still evaluate totrue). - Class Types: The class must define a user-defined conversion operator to a type that is contextually convertible to
bool(such asbool,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.
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.
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.
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.
Master C++ with Deep Grasping Methodology!Learn More





