for loop

E

Eric Sosman

why does this loop exectue once?
for( ; 0 ; )
printf("WHY"); ???

It won't "exectue" at all, because it won't even compile.
I could, of course, write some surrounding code to create a
context that would let it compile (if you'll permit me to
delete the question marks), but what are the chances that
I'll recreate the program that puzzles you? Post *that*
program -- a short, complete, compilable example -- and
ask your question again.
 
C

Chris Dollin

why does this loop exectue once?
for( ; 0 ; )
printf("WHY"); ???

It doesn't. I expect your /actual source/ has a
mistake in it. I'm going to take a wild guess
that you had a semicolon after the for's closing
bracket.
 
V

vicky

why does this loop exectue once?
for( ; 0 ; )
printf("WHY"); ???

this statement under printf won't even compiled, but it s printed out
i din't know why
But
according to me it should be an unreachable code and output is
nothing.
 
K

Keith Thompson

why does this loop exectue once?
for( ; 0 ; )
printf("WHY"); ???

Show us a complete compilable program that exhibits the problem.
Don't paraphrase it, don't re-type it, copy-and-paste it. You should
be able to write a complete program in less than a dozen lines.

And let us know what compiler you're using, and what version. I seem
to recall that some old compiler has a bug with that particular
construct.

Why did it occur to you to write "for( ; 0 ; )" in the first place?
If you want to do nothing, there are easier ways to do it. The only
reason I can think of to write "for( ; 0 ; )" is to deliberately
trigger the above-mentioned compiler bug.
 
M

Malcolm McLean

Beej Jorgensen said:
It shouldn't--the "0" expression should be evaluated before the
printf().
Yes it should. Either the compiler is bugged - apparently one was released
which had this minor defect - or the code hasn't been copied properly, which
almost certainly means that there was a semi-colon after the for()
expression, turning it into an empty loop.
 
G

gw7rib

why does this loop exectue once?
for( ; 0 ; )
printf("WHY"); ???

This has come up before, and the answer seems to be a bug in the
Borland compiler. I take it you are using a Borland compiler?

Hope that helps.
Paul.
 
M

mohdalibaig

It doesn't. I expect your /actual source/ has a
mistake in it. I'm going to take a wild guess
that you had a semicolon after the for's closing
bracket.

i am writing the complete code here

#include<conio.h>
#include<stdio.h>
void main(void)
{
clrscr();
for(int i=0; 0; i++)
printf("Why");
getch();
}

the output on my screen is

Why

Although complier is showing a warning on printf line . Unreachable
code...
 
M

mohdalibaig

Show us a complete compilable program that exhibits the problem.
Don't paraphrase it, don't re-type it, copy-and-paste it. You should
be able to write a complete program in less than a dozen lines.

And let us know what compiler you're using, and what version. I seem
to recall that some old compiler has a bug with that particular
construct.

Why did it occur to you to write "for( ; 0 ; )" in the first place?
If you want to do nothing, there are easier ways to do it. The only
reason I can think of to write "for( ; 0 ; )" is to deliberately
trigger the above-mentioned compiler bug.


i am writing the complete code here

#include<conio.h>
#include<stdio.h>
void main(void)
{
clrscr();
for(int i=0; 0; i++)
printf("Why");
getch();
}
Although complier is showing a warning. Unreachable code. i am using
Borland compiler version 3.0
 
M

mohdalibaig

This has come up before, and the answer seems to be a bug in the
Borland compiler. I take it you are using a Borland compiler?

Hope that helps.
Paul.

yes. you are right. i am using borland compiler version 3.0
 
C

Christopher Benson-Manica

#include<conio.h>
#include<stdio.h>
void main(void)
{
clrscr();
for(int i=0; 0; i++)
printf("Why");
getch();
}

That's not a C program.

#include<stdio.h>

int main(void)
{
int i;
for( i=0; 0; i++ )
printf("Why");
return 0;
}
the output on my screen is
Why

Not for me. Bet that advice you got about buggy Borland compilers is
correct.
 
M

mohdalibaig

That's not a C program.

#include<stdio.h>

int main(void)
{
int i;
for( i=0; 0; i++ )
printf("Why");
return 0;

}

Not for me. Bet that advice you got about buggy Borland compilers is
correct.

Thanks. Which compiler do you have?
 
K

Keith Thompson

Show us a complete compilable program that exhibits the problem.
Don't paraphrase it, don't re-type it, copy-and-paste it. You should
be able to write a complete program in less than a dozen lines.

And let us know what compiler you're using, and what version. I seem
to recall that some old compiler has a bug with that particular
construct.

Why did it occur to you to write "for( ; 0 ; )" in the first place?
If you want to do nothing, there are easier ways to do it. The only
reason I can think of to write "for( ; 0 ; )" is to deliberately
trigger the above-mentioned compiler bug.
[signature snipped]

i am writing the complete code here

#include<conio.h>
#include<stdio.h>
void main(void)
{
clrscr();
for(int i=0; 0; i++)
printf("Why");
getch();
}
Although complier is showing a warning. Unreachable code. i am using
Borland compiler version 3.0

I think your question has been answered, but I'll offer a few tips
in addition.

When you post a followup, it's rarely necessary to quote the entire
parent article. In particular, don't quote the signature (the stuff
starting with "-- " on a line by itself) unless you're commenting on
it. Trim the quoted material to what's necessary for your followup to
make sense to someone who hasn't seen the parent.

Indentation is your friend. It's possible that you did indent your
code and Google messed it up; I think it may have trouble with tab
characters. Use spaces, not tabs, for indentation. (4 spaces is
usually a good indentation level, but some prefer more or fewer.)

main returns int, not void. "int main(void)".

<conio.h> is non-standard; so are clrscr() and getch(). Presumably
clrscr() clears the screen; why on Earth would you want your program
to do that? getch() presumably tells the program to wait for input,
so the window doesn't vanish when the program completes; if you're
using an IDE, figure out how to configure it so the window doesn't
vanish, or just run the program in a command window. (The details are
system-specific and off-topic here; check your documentation.)

Since main returns an int, it should return one. Add "return 0;"
before the closing '}'.

The comp.lang.c FAQ is at <http://www.c-faq.com>. I think you'll find
it instructive.
 
C

CBFalconer

.... snip ...

i am writing the complete code here

#include<conio.h>
#include<stdio.h>
void main(void)
{
clrscr();
for(int i=0; 0; i++)
printf("Why");
getch();
}

the output on my screen is

Why

Although complier is showing a warning on printf line . Unreachable
code...

Perfectly legitimate. You have included the undefined conio.h,
called the undefined clrscr() and getch() functions. Get rid of
these (they are doing nothing anyhow), and properly define main as
"int main(void)", then return 0 from main, properly declare the
variable i (I don't think you have a C99 compiler) and you will
probably be rid of the various undefined behaviour. Note that
undefined behaviour means any result is correct, including starting
WWIII, virgin birth, etc. The resulting code should be:+

#include <stdio.h>

int main(void) {
int i;
for (i = 0; 0; i++)
printf("Why\n");
return 0;
}

Note the \n in the printf string. If you still get "Why" your
compiler is borked.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top