for
Another looping construct, but this one is specialized for iterations. e.g. That previous example is better as a for. The for
control structure has syntax for ( range ) { statements }
. It will repeat the statements based on the range sounds confusing.
Let's say you want a variable i to go from 0 to n-1 , this means that you want to repeat the statements for i such that (i >= 0) and (i<n) , another way to write this is : ( 0 <= i < n) which is how the for construct works. Notice that it can only work with a normal variable (do not use arrays or struct members as the loop variable).
for
also has an alternate, more C-like syntax: for( assignment1; condition; assignment2) { statements }
. Allowed assignment operations are =
, +=
, /=
, *=
and -=
. The condition is a boolean expression which must be true, otherwise the for execution is stopped. Assignment1 is called at the beginning of the loop. assignment2 is done just after each iteration...