- Basic Concepts
- For loop is a control structure for executing code repeatedly based on a condition
- It's commonly used for iterating over arrays or performing tasks multiple times
- For loop is known as a counter loop and is used when counting is involved
- Syntax and Structure
- Basic syntax: for (initialization; condition; increment/decrement) { code }
- Initialization executes before loop starts
- Condition checks before each iteration
- Increment/decrement executes after each iteration
- Control Flow
- Loop starts with initialization step
- Condition is checked before each iteration
- Loop body executes if condition is true
- Condition is re-checked after each iteration
- Loop ends when condition becomes false
- Special Cases
- Infinite loops can be created by omitting condition
- Break statement exits loop prematurely
- Continue statement skips current iteration
- Multiple initialization and update expressions can be used
- Practical Applications
- Perfect numbers can be checked using factor sum
- Armstrong numbers can be checked using cube sum
- Prime numbers can be checked using divisibility
- Fibonacci series can be generated using for loop