Today, I have a collection of simple incremental loops to generate numbers between 0 to 9.
A. "for" statement
1. A common way to make a loop
Code:
for(i=0; i<10; i++)
{
printf("%d", i);
}
2. Strip the initialization
Code:
i = 0;
for(;i<10; i++)
{
printf("%d", i);
}
3. Use ++ inside conditional statement
Code:
i = 0;
for( ; i++<10; )
{
printf("%d", (i - 1));
}
4. Use ++ inside the loop
Code:
i = 0;
for( ; i<10; )
{
printf("%d", i++);
}
B. "while" statement
1.
Code:
i = 0;
while(i<10)
{
printf("%d", i);
i++;
}
2.
Code:
i = 0;
while(i++<10)
{
printf("%d", (i - 1));
}
3.
Code:
i = 0;
while(i<10)
{
printf("%d", i++);
}
C. "do-while" statement
1.
Code:
i = 0;
do
{
printf("%d", i);
i++;
}
while(i<10);
2.
Code:
i = 0;
do
{
printf("%d", i);
}
while(++i<10);
3.
Code:
i = 0;
do
{
printf("%d", i++);
}
while(i<10);
D. linear style
1.
Code:
i = 0;
loop:
printf("%d", i);
i++;
if(i<10)
goto loop;
2.
Code:
i = 0;
loop2:
printf("%d", i);
if(++i<10)
goto loop2;
3.
Code:
i = 0;
loop3:
printf("%d", i++);
if(i<10)
goto loop3;
Btw, I need your suggestion...
Which is nicest syntax for you? Why?
Would you like to guess which is (type and run)faster after all?
Is there another syntax? (maybe inline assembly, but I know nothing about this *sob*)