Class 04
Class 04 : For Loops, While Loops
Key Differences
Control Flow:
“Forloops” are more structured and provide a clear mechanism for initialization, condition checking, and iteration.“Whileloops” give us more flexibility, allowing us to control the loop more directly.
Structure:
“For loops” have an explicit initialization, condition, and iteration expression within the loop header.
“While loops” have just the condition at the beginning. We need to manually handle initialization and iteration within the loop body.
Pattern 1
Pattern 2
Summary
“Forloops” are commonly used when we know the number of iterations in advance or we want to iterate over a range of values.“Whileloops” are used when we're not sure how many iterations are needed and want to continue looping until a certain condition is met.