An if statement with an initializer combines a local initialization and a condition: if (init; condition). The initialized name remains in scope only within the statement, keeping related checks together. section 3.3.4
Checking for a misplaced digit
A number-guessing game can search for a guessed digit and test the returned position in one statement: if (size_t idx = ns.find(guess_char, 0); idx != std::string::npos). When the digit is found, the code marks it as misplaced and consumes that position. section 3.3.4
Without the initializer form, the index would be declared and checked in separate statements. Both approaches work, but the initializer form keeps the temporary index local to the conditional logic. section 3.3.4
if (size_t idx = ns.find(guess_char, 0); idx != std::string::npos)
Inspect the compact form used to find a digit and test whether the search succeeded. section 3.3.4
