English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

for(int i=0; i<10; i++)
{

printf("----------");

}

The initialize statement is ended with ";" so is the condition but the incerement is not terminated,
So its a Expression and C compiler takes only Statement's but still it works without showing any ERROR...!!!!
Why is it???

2006-09-28 04:34:12 · 6 answers · asked by Abhax 1 in Computers & Internet Programming & Design

6 answers

u have a good question to create trouble amongst C litrates .. but i have a good explanation and its not just my thinking ..

first of all, as u know 'for' is not a function, so there is no question of arguments. for is a construct and it is not being broken in to 3 different statements, as u said
int i=0; is a statement .. then what about i<10; will this be treated as statement by the compiler?
if no then how can i++ (in a for construct) be treated as statement. if it is treated so then it should be i++;, then u r right..but no it is not treated as seprate statements but the whole construct is internally interpreted ... here ; is used just to seprate the 3 different things needed for a for construct and not taken as termination character. again if u say int i=0; is interpreted as a statement then for loop should have to terminate just after initialization of statement ..whice is not the case.

one more thing ....

have u seen the if - else conditional construct. ...

what happened if i do this ..
int i=0;
if(i==0)
{
printf("\n I m inside the IF construct bcoz i is equal to 0");
}

now see this ...
int i=0;
if(i=2)
{
printf("\n I m inside the IF construct bcoz i is assigned 2");
}
// here i have used "i=2" (assigned 2 to i) and not "i==2".
ok now here u can see "if(i=2)" is an assignment statement .. but have i placed a ; over there ... ? like this if(i=2;)
but still if(i=2) will work.

i hope u r satisfied with this answer.
if u still have any problem regarding this u can send me message

2006-09-28 05:35:03 · answer #1 · answered by Digitally Й!Й 3 · 0 1

actually, that is the proper syntax for a for loop, so why would it give you an error?
the compiler sees the "for" keyword and knows that it needs to set up a loop based on the other parameters (that are separated by semicolons).
what the compiler would do with your example is to initialize a internal register to 0, compare it with 10, execute the printf statement, increment the register and loop back to where it compares it with 10.

2006-09-28 09:48:35 · answer #2 · answered by justme 7 · 0 0

To be honest, I never really thought of it much. It may be that the compiler treats each of those three expressions as inputs to a function "for" (internally, I mean), and so those are just arguments.

Not a bad question, but I expect that a complete answer would have to get knee-deep in compiler inner workings.

2006-09-28 04:43:40 · answer #3 · answered by mike_w40 3 · 0 0

the syntax here is:

statement :=
for(;
;
)

The whole loop is a single statement.

2006-09-28 05:01:00 · answer #4 · answered by n0body 4 · 0 0

as far as i know
the syntax of a for statement contains
1.the initialisation
2.a condition
3.the increment
these three are SEPARATED by semicolons
thats it

2006-09-28 04:44:47 · answer #5 · answered by sunil 3 · 0 0

i might get photos of Deaner wearing his blue tights and deliver them to his buddies and kinfolk.. then placed them on youtube.. then post a billboard close to the place he works.. then i might fairydust Nubs.. get lulu each and every of the blue eyeshadow she ever needs get Sil each and every of the steak he will ever desire.. and wrap my youngsters in bubblewrap and finally heal Jackpots shanking wound he have been given in penal complex!!

2016-12-12 16:46:16 · answer #6 · answered by ? 4 · 0 0

fedest.com, questions and answers