Logo

Programming-Idioms

History of Idiom 7 > diff from v76 to v77

Edit summary for version 77 by phrank:
[C] C99 allows declatarion of loop variables inside for statement

Version 76

2019-09-27, 23:24:32

Version 77

2019-09-29, 15:20:47

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Extra Keywords
traverse traversal
Extra Keywords
traverse traversal
Code
int i;
for(i=0 ; i<n ; i++){
  T x = items[i];
  printf("Item %d = %s\n", i, toString(x) );
}
Code
for (size_t i = 0; i < n; i++) {
  printf("Item %d = %s\n", i, toString(items[i]));
}
Comments bubble
The loop variable i is the index. Inside the loop, access the value with items[i]
Comments bubble
The loop variable i is the index. Inside the loop, access the value with items[i]