Control Flow
Control Flow#
Control flow determines how your program executes code. It allows programs to make decisions, repeat actions, and choose different paths based on conditions.
graph TD
A[Control Flow Types] --> B[Conditional Statements]
A --> C[Switch Statements]
A --> D[Loops]
B --> B1[if/else<br/>Make decisions based on conditions]
C --> C1[switch/match<br/>Choose between multiple options]
D --> D1[for loops<br/>Repeat known number of times]
D --> D2[while loops<br/>Repeat while condition is true]
D --> D3[do-while loops<br/>Execute at least once, then repeat]
style A fill:#e8f5e8
style B fill:#e3f2fd
style C fill:#e3f2fd
style D fill:#e3f2fd
style B1 fill:#c8e6c9
style C1 fill:#c8e6c9
style D1 fill:#fff3e0
style D2 fill:#fff3e0
style D3 fill:#fff3e0
There are three main types of control flow:
1. Conditional Statements (if/else)#
Make decisions based on conditions. If a condition is true, do one thing; otherwise, do something else.
2. Switch Statements#
Choose between multiple options based on a single value. More efficient than long if-else chains.
3. Loops#
Repeat code multiple times. Three main types:
For Loops#
Repeat a specific number of times or iterate through collections.
While Loops#
Repeat while a condition is true.
Do-While Loops#
Execute at least once, then repeat while condition is true.
Loop Control#
break: Exit the loop immediatelycontinue: Skip to next iteration