Actions

EmSys

CCoding Style - Use Idioms for consistency

From EdWiki

Revision as of 09:30, 23 June 2015 by Jshankar (Talk | contribs) (1 revision imported: EdWiki -- Emsys pages from Shukra)

(diff) ā† Older revision | Latest revision (diff) | Newer revision ā†’ (diff)

Use Idioms for consistency

Like natural languages, programming languages have idioms. A central part of learning any language is developing a familiarity with its idioms.

1

i = 0;
While( i <=  nā€“1 )
    array[i++] = 1.0;

2

for( i = 0; i < n; )
    array[i++] = 1.0;

3

for( i = n; --i; i >= 0 )
    array[i] = 1.0

All of the above are correct, but the idiomatic form is like this:

for( i = 0; i < n; i++ )
    array[i] = 1.0;