A for loop in C++ is used to repeat a block of code a specific number of times. Here's the basic syntax:
for (initialization; condition; increment) {
// Code to be executed
}
Here's a simple example that prints numbers from 1 to 5:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}
return 0;
}
In this example:
Initialization: int i = 1 sets the starting point.
Condition: i <= 5 checks if the loop should continue.
Increment: i++ increases the value of i after each iteration.
Copilot for work
沒有留言:
發佈留言