COSC 104 LEC IV FURTHER READING
COSC 104 LEC IV FURTHER READING
For Constructs
A `for` loop is a powerful control structure that allows you to repeat a block of code a specified number
of times. It's commonly used for iterating over sequences, such as arrays or ranges of numbers.
1. **Syntax**:
c++
// Code to be executed
2. **Components**:
- **Initialization**: Executed once before the loop starts. Typically used to initialize a loop control
variable.
- **Condition**: Checked before each iteration. If true, the loop continues; otherwise, it terminates.
- **Increment/Decrement**: Executed after each iteration. Used to modify the loop control variable.
3. **Example**:
```cpp
#include <iostream>
int main() {
return 0;
```
Output:
12345
4. **Characteristics**:
- The loop control variable (`i` in the example) is scoped to the loop.
- You can use any valid C++ expression for initialization, condition, and increment/decrement.
- The `for` loop is versatile and widely used for various tasks.
2. **User-Defined Functions**:
- User-defined functions are functions that you create yourself within your program.
- These functions serve specific purposes based on your program's requirements.
- Key points:
- You define the function name, parameters, and the code inside the function.
- User-defined functions enhance code modularity and readability.
- Example of a user-defined function:
```cpp
#include <iostream>
using namespace std;
int main() {
int num = 5;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}
```
- Output:
```
Factorial of 5 is 120
```