- #1
tandoorichicken
- 245
- 0
Is this the correct basic code for a for-loop?
for (n=1, n<=10, n++) {
/* statements */
}
If not what am I doing wrong?
for (n=1, n<=10, n++) {
/* statements */
}
If not what am I doing wrong?
HallsofIvy said:Use semi-colons rather than commas!
for (int i=0;i<=10;i++)
{
}
(I cannot bring myself to put that first "{" up on the previous line! It looks so unbalanced.)
A for-loop is a programming structure that allows you to execute a block of code repeatedly for a specified number of times or until a certain condition is met.
To write a for-loop, you need to specify the initial value, the condition for the loop to continue, and the increment or decrement value. For example: for(i=0; i<5; i++) { //code to be executed }
The purpose of a for-loop is to automate repetitive tasks and make the code more efficient. It allows you to iterate through a set of data or execute a block of code multiple times without having to write the code multiple times.
The main difference between a for-loop and a while-loop is that a for-loop is used when the number of iterations is known beforehand, whereas a while-loop is used when the number of iterations is not known and is dependent on a condition.
If the condition in a for-loop is never met, the loop will not be executed and the code will continue to execute after the loop. This can result in the loop not being executed at all or only being executed once, depending on the condition.