Answer the question

Re: Answer the question

by Md. Imran Ali Limon -
Number of replies: 0
For Loop:

Best used when the number of iterations is known.
Syntax includes initialization, condition, and increment/decrement in one line.
Loop condition is checked before execution.

#include using namespace std;

int main() {

for (int i = 0; i < 5; i++) {
cout << i << " ";
}
return 0;
}

While Loop:

Best used when the number of iterations is unknown.
The condition is checked before each iteration.
May not execute at all if the condition is false initially.
#include using namespace std;

int main() {
int i = 0;

while (i < 5) {
cout << i << " ";
i++;
}
return 0;
}

Do-While Loop:

Best used when you need the loop to execute at least once.
The condition is checked after the loop body executes.
Ensures that the loop runs at least once, even if the condition is false initially.
#include using namespace std;

int main() {
int i = 0;

do {
cout << i << " ";
i++;
} while (i < 5);
return 0;
}