Concept Flow - Control flow analysis behavior
Start
Check Condition
Yes| No
Execute Block
Update State
Repeat or Exit
Control flow analysis checks conditions and tracks variable states to decide which code runs next.
let x: number | undefined; if (Math.random() > 0.5) { x = 10; } if (x !== undefined) { console.log(x + 5); }
| Step | Action | Condition | Variable State | Output |
|---|---|---|---|---|
| 1 | Declare x | N/A | x = undefined | |
| 2 | Evaluate Math.random() > 0.5 | true or false | x = undefined | |
| 3 | If true, assign x = 10 | true | x = 10 | |
| 4 | If false, skip assignment | false | x = undefined | |
| 5 | Check if x !== undefined | depends on x | x = 10 or undefined | |
| 6 | If true, print x + 5 | true | x = 10 | 15 |
| 7 | If false, skip print | false | x = undefined |
| Variable | Start | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|
| x | undefined | 10 | undefined | 10 or undefined |
Control flow analysis tracks variable states through conditions. It ensures variables are safe to use after checks. Example: assign x conditionally, then use x only if defined. This prevents runtime errors from undefined values. Always check variables before use when types allow undefined.