473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

explain this.....

i've a code snippet below:
for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough the
condition in the for loop is set to false(0).

Please explain this out or give me an idea of the semantics of 'for'
statement......

Mar 20 '06 #1
24 1761
muttaa wrote:
i've a code snippet below:
for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough the
condition in the for loop is set to false(0).

Please explain this out or give me an idea of the semantics of 'for'
statement......


In future, please try to provide the smallest _compilable_ source code
exhibiting the problem. The above code is not complete. In any case the
printf() statement will not execute.

Mar 20 '06 #2
muttaa wrote:
i've a code snippet below:
for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough the
condition in the for loop is set to false(0).

Not for me. That expression _should_ evaluate to false (well, actually,
"non-zero").
Mar 20 '06 #3
"void * clvrmnky()" <cl**************@hotmail.com.invalid> writes:
muttaa wrote:
i've a code snippet below:
for(;0;)
printf("Gud Morning");
when this is run,i get the "gud morning" message printed,eventhough
the
condition in the for loop is set to false(0).

Not for me. That expression _should_ evaluate to false (well,
actually, "non-zero").


Non-zero is true; zero is false.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 20 '06 #4
"muttaa" <as***********@gmail.com> writes:
i've a code snippet below:
for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough the
condition in the for loop is set to false(0).

Please explain this out or give me an idea of the semantics of 'for'
statement......


You can learn about the semantics of the "for" statement in any decent
C textbook.

I have a hunch what the actual problem might be, but I'm not going to
guess. Post a complete compilable program (copy-and-paste; *don't*
re-type) so we don't have to guess.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 20 '06 #5
muttaa opined:
i've a code snippet below:
for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough
the condition in the for loop is set to false(0).

Please explain this out or give me an idea of the semantics of 'for'
statement......


Your compiler is badly broken, or you didn't show your /real/ code.

--
BR, Vladimir

MICRO:
Thinker toys.

Mar 20 '06 #6
muttaa wrote:
i've a code snippet below:
for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough the
condition in the for loop is set to false(0).


No you don't. Why don't you copy/paste the exact code you ran as this
obviously isn't it.

Robert Gamble

Mar 20 '06 #7
muttaa wrote:
i've a code snippet below:
for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough
the condition in the for loop is set to false(0).

Please explain this out or give me an idea of the semantics of 'for'
statement......

That's a question for your C textbook. If you don't have one, we can
recommend one. It's pointless for us to try to teach you C via random
questions on the newsgroup.


Brian
Mar 20 '06 #8
Keith Thompson wrote:
"void * clvrmnky()" <cl**************@hotmail.com.invalid> writes:
muttaa wrote:
i've a code snippet below:
for(;0;)
printf("Gud Morning");
when this is run,i get the "gud morning" message printed,eventhough
the
condition in the for loop is set to false(0).

Not for me. That expression _should_ evaluate to false (well,
actually, "non-zero").


Non-zero is true; zero is false.

Sorry, switched my logic during writing this after reading a synopsis of
the spec for control expressions. My comment still holds: the for loop
should not be entered, and I think this is the expected behaviour.
Mar 20 '06 #9
"muttaa" <as***********@gmail.com> writes:
i've a code snippet below:
for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough the
condition in the for loop is set to false(0).

Please explain this out or give me an idea of the semantics of 'for'
statement......


The above snippet won't compile on its own... you are generally
expected to post the minimum amount of /compilable/ code that
illustrates the problem.

I compiled and ran the following, which printed nothing. Are you
certain you have the right code? Please post the /whole/, minimal
program that is printing the message you are seeing.

Also, note that nothing is required to be output (even ignoring the
for condition) if you don't include a newline character or fflush(stdout).

#include <stdio.h>

int main(void)
{
for (;0;)
printf("Gud morning.\n");
}
Mar 20 '06 #10
muttaa wrote:
i've a code snippet below:
for(;0;)

printf("Gud Morning");
When I try this in my compiler it outputs:
error: parse error before "for"

Please explain this out or give me an idea of the semantics of 'for'
statement......


K&R2 p. 13, 18, 60, 224

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Mar 20 '06 #11
this is an example format of a "for" statement... for(i=0;i<10;i++)

in your code, for(;0;), of course, the 0 code does not necessarily mean
that it would be false.... actually, the statement for(;;) would still
be true and therefore, will run the statements after it....
the second argument in the for statement just checks if your variable
i.... a for loop is just like a while loop......

am i able to answer your question??? :p
Ayon kay muttaa:
i've a code snippet below:
for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough the
condition in the for loop is set to false(0).

Please explain this out or give me an idea of the semantics of 'for'
statement......


Mar 21 '06 #12
On Mon, 20 Mar 2006 20:27:06 -0800, mianaome wrote:
Ayon kay muttaa:
i've a code snippet below:

for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough the
condition in the for loop is set to false(0).

this is an example format of a "for" statement... for(i=0;i<10;i++)

in your code, for(;0;), of course, the 0 code does not necessarily mean
that it would be false.... actually, the statement for(;;) would still
be true and therefore, will run the statements after it....
the second argument in the for statement just checks if your variable
i.... a for loop is just like a while loop......

am i able to answer your question??? :p


No, you have, if anything, added to the confusion. 0 is false: when
used in a boolean context it always means false and never anything else.

A C "for" loop like this (the <En> are expressions and <S1> is a statement):

for (<E1>; <E2>; <E3>) <S1>

is essentially equivalent to:

<E1>;
while (<E2>) {
<S1>
<E3>;
}

(although not exactly equivalent because of "continue" statements.) The
OP's loop is therefore equivalent to:

;
while (0) {
printf("...");
;
}

A while statement with a false condition does not execute its body. In
fact the compiler is entitled to remove all of this code and generate
nothing. Hence the OP's (and others') surprise that anything is output.

[I have repaired your top-posting.]
--
Ben.
Mar 21 '06 #13
"muttaa" <as***********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
i've a code snippet below:
for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough the
condition in the for loop is set to false(0).

Please explain this out or give me an idea of the semantics of 'for'
statement......


Any chance your actual code looked something like this:

#include <stdio.h>

int main(void) {
int i;

{
for(;0;)
i = 1;
printf("Gud morning.\n");
}

return 0;
} /* main */

-Charles
Mar 21 '06 #14
"Charles M. Reinke" <cm******@ece.gatech.edu> writes:
"muttaa" <as***********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
i've a code snippet below:
for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough the
condition in the for loop is set to false(0).

Please explain this out or give me an idea of the semantics of 'for'
statement......


Any chance your actual code looked something like this:

#include <stdio.h>

int main(void) {
int i;

{
for(;0;)
i = 1;
printf("Gud morning.\n");
}

return 0;
} /* main */


Would it make a difference? The printf() would still never execute, as
the 0 condition would result in the for's body never being executed,
and in any case, the value of i is never used anywhere.
Mar 21 '06 #15
Micah Cowan wrote:
"Charles M. Reinke" <cm******@ece.gatech.edu> writes:
"muttaa" <as***********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
i've a code snippet below:
for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough the
condition in the for loop is set to false(0).

Please explain this out or give me an idea of the semantics of 'for'
statement......

Any chance your actual code looked something like this:

#include <stdio.h>

int main(void) {
int i;

{
for(;0;)
i = 1;
printf("Gud morning.\n");
}

return 0;
} /* main */


Would it make a difference? The printf() would still never execute, as
the 0 condition would result in the for's body never being executed,
and in any case, the value of i is never used anywhere.


Read it again and note what those curly braces delimit. Put the curly
braces around the code the way the compiler might see it.

This discussion is not about correct code (since we have not seen any)
but about the specifics of the control expression in a for() statement.
That "i = 1" is there to illustrate a point and common mistake by novices.
Mar 21 '06 #16
Micah Cowan <mi***@cowan.name> writes:
"Charles M. Reinke" <cm******@ece.gatech.edu> writes:
"muttaa" <as***********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
> i've a code snippet below:
>
>
> for(;0;)
>
> printf("Gud Morning");
>
>
>
> when this is run,i get the "gud morning" message printed,eventhough the
> condition in the for loop is set to false(0).
>
> Please explain this out or give me an idea of the semantics of 'for'
> statement......
>


Any chance your actual code looked something like this:

#include <stdio.h>

int main(void) {
int i;

{
for(;0;)
i = 1;
printf("Gud morning.\n");
}

return 0;
} /* main */


Would it make a difference? The printf() would still never execute, as
the 0 condition would result in the for's body never being executed,
and in any case, the value of i is never used anywhere.


Take a closer look. The body of the for loop is the single statement
"i = 1;"; the printf is executed unconditionally *after* the for loop.
The braces surrounding the whole thing are misleading.

It turns out that wasn't the problem (the OP's ancient compiler was
actually buggy), but it was a reasonable guess. (My own guess was a
trailing semicolon on the "for" line, but that wasn't it either.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 21 '06 #17

Micah Cowan wrote:
"Charles M. Reinke" <cm******@ece.gatech.edu> writes:
"muttaa" <as***********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
i've a code snippet below:
for(;0;)

printf("Gud Morning");

when this is run,i get the "gud morning" message printed,eventhough the
condition in the for loop is set to false(0).

Please explain this out or give me an idea of the semantics of 'for'
statement......


Any chance your actual code looked something like this:

#include <stdio.h>

int main(void) {
int i;

{
for(;0;)
i = 1;
printf("Gud morning.\n");
}

return 0;
} /* main */


Would it make a difference? The printf() would still never execute, as
the 0 condition would result in the for's body never being executed,
and in any case, the value of i is never used anywhere.


Read more carefully. "i" never gets set to 1, but printf certainly
executes.

Mar 21 '06 #18
Micah Cowan wrote:
"Charles M. Reinke" <cm******@ece.gatech.edu> writes:


<snip>
Any chance your actual code looked something like this:

#include <stdio.h>

int main(void) {
int i;

{
for(;0;)
i = 1;
printf("Gud morning.\n");
}

return 0;
} /* main */


Would it make a difference? The printf() would still never execute, as
the 0 condition would result in the for's body never being executed,
and in any case, the value of i is never used anywhere.


Look at the positioning of the braces a bit more carefully...

I'm sure Charles made the mistake deliberately in this case.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Mar 21 '06 #19
"Charles M. Reinke" wrote:
"muttaa" <as***********@gmail.com> wrote in message
i've a code snippet below:

for(;0;)
printf("Gud Morning");

when this is run,i get the "gud morning" message printed,even
though the condition in the for loop is set to false(0).

Please explain this out or give me an idea of the semantics of
'for' statement......


Any chance your actual code looked something like this:

#include <stdio.h>

int main(void) {
int i;

{
for(;0;)
i = 1;
printf("Gud morning.\n");
}

return 0;
} /* main */


More likely it looked like this:

for(;0;);
printf("Gud Morning");

Passing it through indent should make such obvious.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Mar 21 '06 #20
"void * clvrmnky()" <cl**************@hotmail.com.invalid> writes:
Micah Cowan wrote:
"Charles M. Reinke" <cm******@ece.gatech.edu> writes:
"muttaa" <as***********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Any chance your actual code looked something like this:

#include <stdio.h>

int main(void) {
int i;

{
for(;0;)
i = 1;
printf("Gud morning.\n");
}

return 0;
} /* main */

Would it make a difference? The printf() would still never execute,
as
the 0 condition would result in the for's body never being executed,
and in any case, the value of i is never used anywhere.


Read it again and note what those curly braces delimit. Put the curly
braces around the code the way the compiler might see it.

This discussion is not about correct code (since we have not seen any)
but about the specifics of the control expression in a for()
statement. That "i = 1" is there to illustrate a point and common
mistake by novices.


Ah, quite. I have some trouble reading the original code, because my
newsreader actually substitutes for (;0;) with for (;0<smiley face>.
I think this is finally the last straw that makes me go look for where
to get that turned off.
Mar 22 '06 #21
Micah Cowan <mi***@cowan.name> writes:
[...]
Ah, quite. I have some trouble reading the original code, because my
newsreader actually substitutes for (;0;) with for (;0<smiley face>.
I think this is finally the last straw that makes me go look for where
to get that turned off.


<OT>gnus-treat-smiley?</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 22 '06 #22
Muttaa will you repost the Snippet with full FOR loop construction.
Which will able to us to explain you. (if it is actually working)

Watch for your repost.

Vadivel P.

Mar 22 '06 #23

Vadivel wrote:
Muttaa will you repost the Snippet with full FOR loop construction.
Which will able to us to explain you. (if it is actually working)

Watch for your repost.


You're not only late to the party, but didn't bother to check the
dress-code either.

Reading these:

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

will help.

--
BR, Vladimir

Mar 22 '06 #24
Vadivel wrote:
Muttaa will you repost the Snippet with full FOR loop construction.
Which will able to us to explain you. (if it is actually working)

Watch for your repost.

Vadivel P.


See the thread titled 'help me atleast now'. In a nutshell, there
turned out to be a bug in the Turbo C++ compilers 1.01 and 3.0

As an aside please quote the post to which you're replying. For more
information read the following URLs.

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://en.wikipedia.org/wiki/USENET>
<http://en.wikipedia.org/wiki/Netiquette>
<http://www.safalra.com/special/googlegroupsreply/>

Mar 22 '06 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce D | last post by:
I'm trying to understand MySQL better. I can't seem to figure out why the second query takes 2 minutes and the first query takes 12 seconds. It should be noted that the table, KBM, has 250...
14
by: Ina Schmitz | last post by:
Hello all, I don't succeed in displaying the explain plan. I use IBM DB2 Universal Database 8.2. I tried to do the example given in the online help for "Visual Explain". The tables...
10
by: Jeff Boes | last post by:
I'm hoping there's someone here with experience in building the Visual Explain tool from Red Hat. I downloaded it and the J2 SDK, but when I attempt to follow the build instructions, I get messages...
5
by: Jon Lapham | last post by:
I have been using the EXPLAIN ANALYZE command to debug some performance bottlenecks in my database. In doing so, I have found an oddity (to me anyway). The "19ms" total runtime reported below...
2
by: Dan Sugalski | last post by:
Is there any way to convince explain to go do its thing when given a query with placeholders in it? I'm trying to do some performance checking of some of the queries built into a system I'm...
4
by: marklawford | last post by:
Not having earned my DBA badge from the scouts just yet I'm a little lost with an error I'm getting. We've just upgraded our development database from 7.2 to 8.2 as the first step in upgrading...
2
by: heming_g | last post by:
two tables with the same table structure : tb_xxx and tb_xxx_tmp in table tb_xxx , column "listno" is the primary key of itself and foreign key of dozen of tables . here is my sql .. "...
0
by: dataguy | last post by:
I have my developers explaining the stored procedures that they write using visual studio. These stored procedures are for DB2 os390. In one case, one of the developers has defined a global...
5
by: kabotnet | last post by:
Hi, I'm new in db2, I'm trying to execute EXPLAIN command on some queries but i have error like: And message similar to: Token EXPLAIN is not valid, valid tokens ( END GET SET CALL DROP FREE...
1
by: w.l.fischer | last post by:
Hi, the following sequence: set current explain mode yes; set current explain snapshot yes; update ...; set current explain mode no;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.