Messenger_creation_EF7DBF82-F5B7-4A91-B018-307A352F000F.jpeg

Full Transcript

# Chapter 14: For Loops ## 14.1 Introduction - For loops are a fundamental control structure in programming - They allow you to efficiently repeat a block of code a specific number of times. - For loops are especially useful when working with arrays or lists. ## 14.2 Structure of a For Loop A for...

# Chapter 14: For Loops ## 14.1 Introduction - For loops are a fundamental control structure in programming - They allow you to efficiently repeat a block of code a specific number of times. - For loops are especially useful when working with arrays or lists. ## 14.2 Structure of a For Loop A for loop in C++ consists of four main parts: 1. **Initialization**: This part is executed only once at the beginning of the loop. It is typically used to declare and initialize a *counter* variable. 2. **Condition**: This is a boolean expression that is evaluated before each iteration of the loop. If the condition is true, the loop body is executed. If it is false, the loop terminates. 3. **Increment/Decrement**: This part is executed after each iteration of the loop. It is typically used to update the counter variable. 4. **Body**: This is the block of code that is executed during each iteration of the loop. The general syntax of a for loop is: ```cpp for (initialization; condition; increment/decrement) { // Loop body } ``` ## 14.3 Example 1: Basic For Loop ```cpp #include int main() { for (int i = 0; i < 5; i++) { std::cout