I know a for loops repeat an action some number of times. I read the mdm article on it.
Can someone explain like Im 5?
but I dont understand the syntax, especially the step++ part shown here
for (let step = 0; step < 5; step++) {
// Runs 5 times, with values of step 0 through 4.
console.log("Walking east one step");
}
What is the full "for" loop syntax in C? - Stack Overflow
c - Can someone explain what a "for" loop is in the simplest terms possible? - Stack Overflow
Syntactic sugar in C - (ab)using "for" loops
There is a caveat though. As the author said in the summary section, while the prologue/epilogue example should work in normal conditions, it would break (pun not intended) when you break or goto inside the loop. And break inside the loop happens a lot. Be careful when using this to manage resources. In C++, using a stack allocated object with a destructor in the initialization part can handle exceptional control flows.
What makes for-loops difficult for beginners?
Videos
The comma is not exclusive of for loops; it is the comma operator.
x = (a, b);
will do first a, then b, then set x to the value of b.
The for syntax is:
for (init; condition; increment)
...
Which is somewhat (ignoring continue and break for now) equivalent to:
init;
while (condition) {
...
increment;
}
So your for loop example is (again ignoring continue and break) equivalent to
p=0;
while (p+=(a&1)*b,a!=1) {
...
a>>=1,b<<=1;
}
Which acts as if it were (again ignoring continue and break):
p=0;
while (true) {
p+=(a&1)*b;
if (a == 1) break;
...
a>>=1;
b<<=1;
}
Two extra details of the for loop which were not in the simplified conversion to a while loop above:
- If the condition is omitted, it is always
true(resulting in an infinite loop unless abreak,goto, or something else breaks the loop). - A
continueacts as if it were a goto to a label just before the increment, unlike acontinuein the while loop which would skip the increment.
Also, an important detail about the comma operator: it is a sequence point, like && and || (which is why I can split it in separate statements and keep its meaning intact).
Changes in C99
The C99 standard introduces a couple of nuances not mentioned earlier in this explanation (which is very good for C89/C90).
First, all loops are blocks in their own right. Effectively,
for (...) { ... }
is itself wrapped in a pair of braces
{
for (...) { ... }
}
The standard sayeth:
ISO/IEC 9899:1999 §6.8.5 Iteration statements
¶5 An iteration statement is a block whose scope is a strict subset of the scope of its enclosing block. The loop body is also a block whose scope is a strict subset of the scope of the iteration statement.
This is also described in the Rationale in terms of the extra set of braces.
Secondly, the init portion in C99 can be a (single) declaration, as in
for (int i = 0; i < sizeof(something); i++) { ... }
Now the 'block wrapped around the loop' comes into its own; it explains why the variable i cannot be accessed outside the loop. You can declare more than one variable, but they must all be of the same type:
for (int i = 0, j = sizeof(something); i < j; i++, j--) { ... }
The standard sayeth:
ISO/IEC 9899:1999 §6.8.5.3 The for statement
The statement
for ( clause-1 ; expression-2 ; expression-3 ) statementbehaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any variables it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.133)
Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.
133) Thus, clause-1 specifies initialization for the loop, possibly declaring one or more variables for use in the loop; the controlling expression, expression-2, specifies an evaluation made before each iteration, such that execution of the loop continues until the expression compares equal to 0; and expression-3 specifies an operation (such as incrementing) that is performed after each iteration.
The comma simply separates two expressions and is valid anywhere in C where a normal expression is allowed. These are executed in order from left to right. The value of the rightmost expression is the value of the overall expression.
for loops consist of three parts, any of which may also be empty; one (the first) is executed before the first iteration, and one (the third) at the end of each iteration. These parts usually initialize and increment a counter, respectively; but they may do anything.
The second part is a test that is executed at the beginning of each execution. If the test yields false, the loop is aborted. That's all there is to it.
Basics
For loops are short-hand while loops in numerical contexts.
Rather than write this:
x = 0;
while (x < 5) {
x++;
}
You can write this:
for (x = 0; x < 5; x++)
Syntax
Oh, and the parts are as follows:
for (start; condition; increment)
Where they mean the following:
- The start or initialization is the initial variable.
- The condition is works like the part in parenthesis in a while statement; the for's body will continue to execute until this becomes false.
- The increment or update is how the variable is changed; without it, the loop runs indefinitely.
A for loop simply means that you're instructing the program to perform an action repeatedly.
What's the action? It's whatever you decide that it is.
How many times do you do it? For as many times as your for loop specifies.
What do you do it to? That's also something that you specify.
Example:
for(i in 1:nrow(x)){
x[i,3] <- "A"
}
That's a for loop in R. It saying that for row 1 to the last row of an object called x make column 3 the character "A".
The action is <- "A" which means assign a value of the character A.
How many times to do it? 1:nrow(x)
What to do it to? x[i,3]
Another example:
for(i in 1:10){
a[i] <- TRUE
}
Here the action is to assign a value of TRUE.
The number of times to do it is (for) 1 to 10 (i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10).
What to do it to? The elements of object a.