Wednesday, May 21, 2008

C/C++ If statement

Lately, I was been reading C/C++ Coding Styles. It talks about parenthesis-ing, spacing, alignment, etc...

I also recommends some common software practices, such as putting the numerical constant first in the comparison statements like if, while, for and ?:

for example:


if (1000 < stack_size) {
...
}

while (10 != num) {
...
}


for (int i=0; 20 == value; ++i) {
...
}


Why does it emphasize this rule ? It will help you to avoid ambiguous assignments in the conditional statements.

A common mistake in for loop
=====================


for(int i=0;i < X; i++)

whenever we write "for" loop we are tend to use the post-fix operator ++ instead of pre-fix ++. First one need to understand what is the difference between i++ and ++i, and when to use what.

i++ is more complex and takes more clock cycles than ++i. So please make it a habit to use ++i instead of i++, this may make your code a little bit faster.

2 comments:

Cogito Ergo Sum said...

I would better appreciate if you can blogging this kind of tips in your blog...me,a not so very good techie,this gives a feeling that i am able to learn something...pick some useful things :)..how about continue posting them.

Reemus Kumar said...

Sure, Thala. Will do it on regular basis.