Logo

Programming-Idioms

History of Idiom 7 > diff from v3 to v4

Edit summary for version 4 by :

Version 3

2015-08-01, 16:46:51

Version 4

2015-08-04, 16:37:51

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

Code
int i;
for(i=0 ; i<n ; i++){
  T x = items[i];
  printf("Item %d = %s", i, toString(x) );
}
Code
int i;
for(i=0 ; i<n ; i++){
  T x = items[i];
  printf("Item %d = %s\n", i, toString(x) );
}
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]