The deal is, a for loop executes its three statements as separate statements.
If you have a ++i or i++ inside another statement (a function call, or some
math) the increment happens either before (++i) or after (i++) the rest of the
statement. Here's what I think Bilal was trying to do (this should compile, by
the way):
#include <stdio.h>
int main(int argc, char **argv)
{
int i = 0;
printf("post-increment:\n");
while (i < 10)
{
printf("i: %d\n", i++);
}
i = 0;
printf("\npre-increment:\n");
while (i < 10)
{
printf("i: %d\n", ++i);
}
}
/*** End ***/
Or, in C++:
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
int i = 0;
cout << "post-increment:" << endl;
while (i < 10)
{
cout << "i: " << i++ << endl;
}
i = 0;
cout << "\npre-increment:" << endl;
while (i < 10)
{
cout << "i: " << ++i << endl;
}
}
/*** End ***/
--- In [email protected], Bilal Nawaz <bilalnawazhall...@...> wrote:
>
> both these increments operator are same because of the following reasons
> 1.i++ is postincrement operator means it increments the next value of i after
> printing that value
> for example
> (for int i=0;i<10;i++)
> {
> cout<<i<<endl;
> }
> after printing zero it will make increment
> whereas
> (for int j=0;j<10;++j)
> {
> cout<<j<<endl;
> }
> it will make increment first then print values of j in the given range
> regards
> bilal nawaz
>
>
>
>
> ________________________________
> From: Asad Abbas <cyberstuden...@...>
> To: c prog <[email protected]>; c4swimmers ygroup <c4swimm...@...>
> Sent: Sun, October 18, 2009 4:26:12 PM
> Subject: [c-prog] for loop i++ and ++i ?
>
>
> Why these both statements have same output??
> why i++ and ++i works alike in for loop??
>
>
> for(int i=0;i<5;i++)
> or
> for(int i=0;i<5;++i) ;
> cout << i << endl;
>
> //output:
> /*
> 0
> 1
> 2
> 3
> 4
> */
>
> Thanx in Advance
> Asad Abbas
> UET Taxila
> Pakistan---- --------- --------- --------- --------- --------- ---------
>
> One of the main causes of the fall of the Roman Empire was that, lacking
> zero, they had no way to indicate successful termination of their C programs.
> Robert Firth
>
> [Non-text portions of this message have been removed]
>
>
>
>
>
>
>
> [Non-text portions of this message have been removed]
>