Section 7: Loops
Types of Loops
- While
- Do while
- For
- For each
Loop Flowcharts
1. While Loop
Entry Controlled Loop: Checks condition before execution.
graph TD
Start((Start)) --> Cond{Condition}
Cond -- True --> Proc[Process]
Proc --> Cond
Cond -- False --> End((End))
2. Do-While Loop
Exit Controlled Loop: Executes body at least once before checking condition.
graph TD
Start((Start)) --> Proc[Process]
Proc --> Cond{Condition}
Cond -- True --> Proc
Cond -- False --> End((End))
Code Implementations
While Loop
Do-While Loop
For Loop
Key Notes
- Counter-Controlled: The
forloop is considered a counter-controlled loop.- Usually, a
forloop is written with a counteri. - Example:
for(int i=0; i<n; i++)whereiis the counter.
- Usually, a
- Infinite Loop: A for loop can be written with empty parameters to create an infinite loop:
for(;;)