ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
while loop is a pre-test control flow statement that repeatedly executes a target statement or block of code as long as a specified expression evaluates to true. Because the condition is evaluated before the loop body executes, the loop body will not execute at all if the initial condition evaluates to false.
Syntax
Execution Mechanics
- Condition Evaluation: The
conditionis evaluated first. This expression must be contextually convertible tobool. - Execution: If the condition evaluates to
true, the statements within the loop body are executed sequentially. - Iteration: After the loop body completes, control flow jumps back to the condition evaluation.
- Termination: If the condition evaluates to
false, the loop terminates immediately. Control flow passes to the first statement following the loop block.
Variable Declaration in Condition
C++ permits the declaration and initialization of a variable directly within thewhile condition. The loop executes as long as the initialized variable contextually converts to true (e.g., non-zero for integers, non-null for pointers).
Loop Control Statements
The standard execution flow of awhile loop can be altered using jump statements:
break: Immediately terminates the innermost enclosingwhileloop. Control flow jumps to the statement immediately following the loop body, bypassing any remaining condition checks.continue: Interrupts the current iteration. Control flow jumps directly to the end of the loop body, skipping any remaining statements, and forces an immediate re-evaluation of thewhilecondition.
Infinite Loops
If the condition expression never evaluates tofalse and no break statement is encountered, the loop becomes an infinite loop. The standard idiom for an intentional infinite loop in C++ is:
Master C++ with Deep Grasping Methodology!Learn More





