"imperfectfreak" <im************@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Advait Vaidya wrote:
>i am not getting the numerical figure .
1
12
123
1234
12345
plse help me.
i am writing these following logic:
# include <iostream.h>
void main()
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=1;j<i;j++)
cout<<j;
cout<<endl;
}
}
this program displays 11234455 on screen.
please help me.
Advait Vaidya
Try writing code that is a little neater and easier to read. Also don't
use depreciated headers.
You mean "deprecated", right?
>
#include <iostream>
int main(int argc, char * argv[])
or just:
int main()
{
for(int i(0); i < 6; i++)
Why start at 0? And there's nothing wrong with initialization using =.
Most people would write this as:
for (int i = 1; i <= 5; ++i) // (or i++, it doesn't matter here)
{
for(int j(1); j < i + 1; j++) std::cout << j;
Why put the loop body on the same line? That makes it difficult to debug.
(It also goes against your "easier to read" statement above, in my opinion.)
How about
:
for (int j = 1; j <= i; ++j)
You seem to not like using <= for comparisons. I'm guessing this is because
you've heard that it's quicker to compare using < than <=?
While it _may_ be that in some case of un-optimized code, using <= might
take more machine cycles, it seems more readable to me to use <=, because
that shows your actual upper bound right there in the code, without having
to mentally subtract one to get the real upper bound. (Also, in cases where
the compiler isn't optimizing the loop anyway, the use of i+1 would mean an
extra step of adding 1 every iteration before comparing, negating your
assumed speedup from using < instead of <=.)
std::cout << std::endl;
}
}