Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
C++
void main()

int n=1;

for(;n<5;)
printf("%d",n);
n++;

}

1 can above for loop is similar to while loop? when we initialize control variable before the loop and increment/decrement in the body of the loop
Posted
Updated 10-May-12 5:40am
v2
Comments
Sandeep Mewara 10-May-12 11:40am    
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.

I added PRE tags to code part to atleast make your content readable.
Code-o-mat 10-May-12 12:06pm    
As already said, your question is hard to understand. Anyways, please note that in the code segment you are not increasing the variable 'n' inside of the 'for' loop, since 'n++' isn't in the body of the loop but after it.
e.g:
for (;n<5;)
{
printf("%d",n);
n++;
}
should work better.

1 solution

Not quite: Your version would never stop printing - you need "{" and "}" to contain the loop contents, or the first statement only will be looped on.

But:
C++
int i;
for (i = 0; i < 5; i++)
   printf("%d",i);

Is the same as:
C++
int i=0;
while (i < 5)
   {
   printf("%d",i);
   i++;
   }

or:
C#
int i=0;
for (;i < 5;)
   {
   printf("%d",i);
   i++;
   }
But you shouldn't really use the final example.

BTW: It is a good idea to always use curly brackets in a loop of if condition when you are starting to avoid just the kind of problem I mentions at the start!
 
Share this answer
 
Comments
OriginalGriff 10-May-12 12:23pm    
Um. And where in the loop does "n" get incremented?
Code-o-mat 10-May-12 13:56pm    
eh, you are right, i have no idea how i mixed that up...will delete my comment to remove the confusement. :) Sorry...
OriginalGriff 10-May-12 13:59pm    
It's easy enough to do!
I got caught by it in the days before autoindenting was common, and have stuck with bracketing everything ever since. :laugh:
Code-o-mat 10-May-12 14:15pm    
This reminds me of the time long long time ago when i was "headbanging" over a for loop that would only execute once no matter what i tried and i couldn't understand how that was possible, checked all the variables, instructions, increments, everything looked ok but still it wouldn't work. Then i realized there was a damned semicolon at the end of the loop. So something like this, of course the loop i am talking about was a bit...more complex:
int i;
for (i = 0; i < 10; i++);
{
printf("%d\n", i);
}
OriginalGriff 10-May-12 14:25pm    
Yep - and it takes forever to spot because you read what you expect to see :O
Did you also bang your head on
if (a = b) {...}
rather than
if (a== b) {...}
because I did! It's one of the reasons I always run with "treat warnings as errors" even now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900