for loops with dual conditions

T

termin

Why is that the rightmost condition in a for loop's dual condition only
taken into account so whats the use of the other condition


eg
for(i=0,j=0;i<40,j<100;i++,j++)
{


}



i and j are incremented to 100
 
E

Emmanuel Delahaye

termin wrote on 16/07/05 :
Why is that the rightmost condition in a for loop's dual condition only
taken into account so whats the use of the other condition

eg
for(i=0,j=0;i<40,j<100;i++,j++)
{

}

i and j are incremented to 100

Because the C language is defined like that. Only the rightmost
expression is evalued in the exit condition expression. Use the logical
operators, and don't use the comma operator here.

for(i = 0, j = 0; i < 40 && j < 100; i++, j++)
{
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
A

Alexei A. Frounze

termin said:
Why is that the rightmost condition in a for loop's dual condition only
taken into account so whats the use of the other condition


eg
for(i=0,j=0;i<40,j<100;i++,j++)
{


}



i and j are incremented to 100

Because in C, an expression of the form a, b evaluates to the last one, b.
If you want both conditions to be taken in account transform them into a
single one, without coma.

Alex
 
R

Richard Heathfield

termin said:
Why is that the rightmost condition in a for loop's dual condition only
taken into account so whats the use of the other condition


eg
for(i=0,j=0;i<40,j<100;i++,j++)

for(i = 0, j = 0; i < 40 && j < 100; i++, j++)


Look up "comma operator" in your C book to understand why only the second of
your two tests contributed to your loop control.
 
S

SM Ryan

# Why is that the rightmost condition in a for loop's dual condition only
# taken into account so whats the use of the other condition
#
#
# eg
# for(i=0,j=0;i<40,j<100;i++,j++)

Possibly you want
for(i=0,j=0;i<40 && j<100;i++,j++)
or
for(i=0;i<40;i++) for(j=0;j<100;j++)
 
V

vire

So,it comes to me that
if you want to get some logic returns(just like "true" or "false") in
the comma sentence.
only the rightmost expression return the logic value.
like:
//codition 1:
int i;
if(i=1,i+=1,i==2)
printf("right executed\n");
//another condition:
int i;
i=1;
i+=1;
if(i==2)
printf("right executed\n");
both of them are executed right.

so
for(i=0,j=0;i<40,j<100;i++,j++­) { }
means
for(i=0,j=0;j<100;i++,j++){}
"i<40" was executed but it had no effection to the for loop

you can do many things in a comma expression,they will executed one by
one.
but only the rightmost expresstion will return the value true or false

i hope these will be helpful.
did i make myself understood?i am poor in english


-vire
 
K

Keith Thompson

Emmanuel Delahaye said:
termin wrote on 16/07/05 :

Because the C language is defined like that. Only the rightmost
expression is evalued in the exit condition expression. Use the
logical operators, and don't use the comma operator here.

for(i = 0, j = 0; i < 40 && j < 100; i++, j++)
{
}

Both subexpressions (i<40 and j<100) are evaluated, but the result of
the first one is discarded. This has nothing to do with being part of
a for loop, it's just the way the comma operator is defined to work.

It's hard to tell what the OP is actually trying to do. If he can
tell us what he's trying to accomplish, we might be able to help.
 
K

Kenny McCormack

Keith Thompson said:
Both subexpressions (i<40 and j<100) are evaluated, but the result of
the first one is discarded. This has nothing to do with being part of
a for loop, it's just the way the comma operator is defined to work.

True enough.
It's hard to tell what the OP is actually trying to do. If he can
tell us what he's trying to accomplish, we might be able to help.

Of course, that's true of every thread-starting post in this ng.

And if they did tell us what they were trying to accomplish, it would take
all the fun out of the guessing.
 
A

Alexei A. Frounze

termin said:
Thanks i thought i discovered some flaw in C

C is such a language of which you can often thing that something is wrong or
isn't working. In reality, what is wrong is often your understanding of C
and what isn't working is your code written in misunderstanding of C.
Yes, I would think of a few bad or weak things in C, but it's what it is. If
you choose C, obey its rules.

Alex
 
K

Keith Thompson

termin said:
Thanks i thought i discovered some flaw in C

And again:

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.
 

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

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top