473,385 Members | 1,523 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,385 software developers and data experts.

for loops with dual conditions

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

Nov 15 '05 #1
10 2216
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++
Nov 15 '05 #2
"termin" <db*****@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
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
Nov 15 '05 #3
termin wrote:
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.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
Nov 15 '05 #4
"termin" <db*****@gmail.com> wrote:
# 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++)
--
SM Ryan http://www.rawbw.com/~wyrmwif/
So basically, you just trace.
Nov 15 '05 #5
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

Nov 15 '05 #6
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
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++)
{
}


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.

--
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.
Nov 15 '05 #7
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
....
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.

Nov 15 '05 #8
Thanks i thought i discovered some flaw in C

Nov 15 '05 #9
"termin" <db*****@gmail.com> wrote in message
news:11********************@g49g2000cwa.googlegrou ps.com...
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
Nov 15 '05 #10
"termin" <db*****@gmail.com> writes:
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.

--
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.
Nov 15 '05 #11

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

Similar topics

2
by: SplaTTer | last post by:
I have a piece of code consisting of k nested for loops, for example: int k = 5; int *p = new int; double x,y; //x is read from file for(p=0;p<=6;p++) for(p=0;p<=3;p++) for(p=0;p<=3;p++)
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
5
by: Uday Deo | last post by:
Hi everyone, I am looping through 4 nested loops and I would like to break in the inner most loop on certain condition and get the control on the 2 nd loop instead of 3rd loop. Here is briefly...
24
by: Kunal | last post by:
Hello, I need help in removing if ..else conditions inside for loops. I have used the following method but I am not sure whether it has actually helped. Below is an example to illustrate what I...
1
by: Ben Fidge | last post by:
My client has upgraded their server to dual-Xeon and we're getting some strange symptoms. Mostly this is happening in static classes where static properties and members aren't protected, but also...
17
by: Gandalf | last post by:
how can I do width python a normal for loop width tree conditions like for example : for x=1;x<=100;x+x: print x thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.