i came across the following 'for' statement in a code:
for (pass=rx_str[ptr1+1]; ptr1<size && pass; ptr1++) {
i'm curious about about the 'ptr1<size && pass' of the statement as i
have never come across a for loop layed out in this way.
1. is this valid?
2. if so, is it the same as saying 'ptr1<size && ptr1<pass' ?
> for (pass=rx_str[ptr1+1]; ptr1<size && pass; ptr1++) {
>
> i'm curious about about the 'ptr1<size && pass' of the statement as i
> have never come across a for loop layed out in this way.
> 1. is this valid?
> 2. if so, is it the same as saying 'ptr1<size && ptr1<pass' ?
- Andy.
----------------------------------------------
Andrew David, Software Manager, Ultronics Ltd http://www.Ultronics.com
________________________________________________________________________
This message has been checked for all known viruses, by Star Internet,
delivered through the MessageLabs Virus Control Centre.
For further information visit: http://www.star.net.uk/stats.asp
i apologise if this post appears more than once - just realised that the
1st time i sent it, my [OT] channel was turned off!
hello everyone,
i came across the following 'for' statement in a code:
for (pass=rx_str[ptr1+1]; ptr1<size && pass; ptr1++) {
i'm curious about about the 'ptr1<size && pass' of the statement as i
have never come across a for loop layed out in this way.
1. is this valid?
2. if so, is it the same as saying 'ptr1<size && ptr1<pass' ?
> -----Original Message-----
> From: Oluseyi Odeinde [SMTP:.....seyiKILLspam.....OCEAN.CF.AC.UK]
> Sent: Friday, January 26, 2001 5:15 PM
> To: EraseMEPICLISTspam_OUTTakeThisOuTMITVMA.MIT.EDU
> Subject: [OT]: quick question on C
>
> i apologise if this post appears more than once - just realised that the
> 1st time i sent it, my [OT] channel was turned off!
>
> hello everyone,
>
> i came across the following 'for' statement in a code:
>
> for (pass=rx_str[ptr1+1]; ptr1<size && pass; ptr1++) {
>
> i'm curious about about the 'ptr1<size && pass' of the statement as i
> have never come across a for loop layed out in this way.
> 1. is this valid?
> 2. if so, is it the same as saying 'ptr1<size && ptr1<pass' ?
>
> TIA
> Seyi
>
> --
> http://www.piclist.com hint: To leave the PICList
> piclist-unsubscribe-requestspam_OUTmitvma.mit.edu
>
> -----Original Message-----
> From: Alok Dubey (OCS-BLRAKS-AVS)
> Sent: Friday, January 26, 2001 5:25 PM
> To: 'pic microcontroller discussion list'
> Subject: RE: [OT]: quick question on C
>
> i hnk it is a boolean of size and pass which is then compared to ptr1
> alok
>
>
> -----Original Message-----
> From: Oluseyi Odeinde [SMTP:KILLspamseyiKILLspamOCEAN.CF.AC.UK]
> Sent: Friday, January 26, 2001 5:15 PM
> To: RemoveMEPICLISTTakeThisOuTMITVMA.MIT.EDU
> Subject: [OT]: quick question on C
>
> i apologise if this post appears more than once - just realised that
> the
> 1st time i sent it, my [OT] channel was turned off!
>
> hello everyone,
>
> i came across the following 'for' statement in a code:
>
> for (pass=rx_str[ptr1+1]; ptr1<size && pass; ptr1++) {
>
> i'm curious about about the 'ptr1<size && pass' of the statement as
> i
> have never come across a for loop layed out in this way.
> 1. is this valid?
> 2. if so, is it the same as saying 'ptr1<size && ptr1<pass' ?
>
> TIA
> Seyi
>
> --
> http://www.piclist.com hint: To leave the PICList
> spamBeGonepiclist-unsubscribe-requestspamBeGonemitvma.mit.edu
>
part 1 3000 bytes content-type:text/plain; charset=us-ascii
Hi,
Its the same as
"for( set initial condition ; termination condition ; iteration condition
)", if you like....where the termination condition is met if 'ptr1' is less
than 'size' AND pass is a non zero value.
> whoops
> make that a boolean AND of size and pass
> sorry
> alok
>
> > {Original Message removed} part 2 165 bytes content-type:application/octet-stream; (decode)
pls check he must be using size and pass as hex values..
now he is doing an AND on them i guess.. and that result is being compared
with ptr1
if he used && he is doing a logical/booloean and.. and watching the result
which can be 0 or 1 and comparing that with ptr1 (the result would be zero
if either size or pass was zero else always 1 even if either is negative)
if he had used & he was "and"ing the binary numbers / hex numbers and
taking the result ..so each bit in the binary representation of size and
pass would be ANDed.
Hope this helps ..
use this code.. try changing a and b and see the results
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main ()
{ int a=254 , b= -1 , c d;
c=(a&&b); d=(a&b)
printf (" %d : %d" , c , d);
}
u are right..
& is bit wise and.. thats wht i said..
&& is boolean.. eg a&&b will be zero if either a or b is zero.
but go back to ur code..
the line
inputport&&0x80
would return 1 too! coz the neither inputport nor 0x80 is 0
so u get the
printf ("MSB is 128")
running in that too..
try it .. i did :-)
u either check a&b for 128 that is the safest way to check
what amazes me is why someone is using && .. that to in the code mentioned
by Olyeusi.
well.. thanks for the same.. a good break from some boring socket code here
You cannot tell from your "for" code snippet if or how the variables are
being manipulated. From this "for" statement only, your comment would be
true; you'll have to look at the entire program (or at least that section of
code where this statement appears) to determine that.
by the way, b4 going into the for loop 'size' is 28, and and 'ptr1' is 18. so y
do u say ptr1 will never be > 1?. this is what the for loop looks like and as u
can see 'pass' is being decremented
What they're saying is that ptr1<size has to be true (a '1'), AND
pass also has to be true (a '1') before the statement is true
(a '1'). So, ptr1, size .. and .. pass are variables (or
constants) that are being tested for tru to make the rest of the
statement happen.
>
> hello everyone
>
> i came across the following 'for' statement in a code:
>
> for (pass=rx_str[ptr1+1]; ptr1<size && pass; ptr1++) {
>
> i'm curious about about the 'ptr1<size && pass' of the statement as i
> have never come across a for loop layed out in this way.
> 1. is this valid?
> 2. if so, is it the same as saying 'ptr1<size && ptr1<pass' ?
>
> Thanx in advance
> Seyi
>
> --
> http://www.piclist.com hint: To leave the PICList
> RemoveMEpiclist-unsubscribe-requestKILLspamTakeThisOuTmitvma.mit.edu
It's just a simple comparison. IF the value of 'ptr1' is less than
the value of 'size' AND if pass is not zero, execute the rest of the
statement. Otherwise, don't bother. Just go on with the next
statement. It doesn't matter if ptr1 ever gets greater than '1'.
As long as it is less than size, then the comparison is true (a '1').
Otherwise, it fails. Same with 'pass'. If pass is ever anything
other than '1', it fails, and consequently, so does the whole
statement. Very simple. Very easy to understand.
Regards,
Jim
On Fri, 26 January 2001, Alok Dubey wrote:
>
> i dont know how it makes sense to u ,.. but from wht i see ur ptr1 is never
> gona be more than 1
> alok
>
>
> > {Original Message removed}
>
> No: '&' is a bitwise AND, whereas '&&' is boolean AND.
This is correct.
> As in, if a port was read to see if one of 8 lights had been turned on
>
> InputPort = 0x8E;
> if (InputPort & 0x80) (bitwise)
> {
> // we would get here
> printf("MSB set....Someone turned a light on.");
> }
This is correct.
> whereas
>
> InputPort = 0x8E;
> if(InputPort && 0x80)
> {
> // We would not get here!
> printf("InputPort is now equal to 128");
> }
>
This is wrong.
InputPort && 0x80 is really shorthand for:
(InputPort != 0) && (0x80 != 0)
So we would get there!
Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)
will this not be evaluated as follows ((ptr1<size) && pass) hence the inner
bracket will evaluate as true/false which will then be logically anded with pass
to give a final true/false result.
> come to think of it, after taking a 2nd look at that statement - i think
it
> really means:
>
> for (pass=rx_str[ptr1+1]; ptr1<size && pass=1; ptr1++) {
Well, first, you probably meant to say 'pass==1'.
> beacuse we are dealing with a boolean AND, therefore for pass to be 'true'
it
> must equals 1. makes sense?
But now, a numeric or pointer type is treated as true as long as it is
non-zero (non-null for pointers)
Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)
> for (pass=rx_str[ptr1+1]; ptr1<size && pass=1; ptr1++) {
>
> beacuse we are dealing with a boolean AND, therefore for pass to be 'true' it
> must equals 1. makes sense?
No. When you evaluate a boolean expression with an integer variable,
false is zero, and true is nonzero.
> Otherwise, it fails. Same with 'pass'. If pass is ever anything
> other than '1', it fails, and consequently, so does the whole
> statement. Very simple. Very easy to understand.
>
> Regards,
>
> Jim
Save your high and mighty bullshit for the times when you are right, Jim.
Your statement:
"If pass is ever anything other than '1', it fails..."
is incorrect.
For all nonzero values of pass, it is evaluated as TRUE. It only
evaluates to false when pass==0
> i came across the following 'for' statement in a code:
>
> for (pass=rx_str[ptr1+1]; ptr1<size && pass; ptr1++) {
>
> i'm curious about about the 'ptr1<size && pass' of the statement as i
> have never come across a for loop layed out in this way.
> 1. is this valid?
Yes, it is valid. It is unclear code, and I'm not sure that it does what
the author wanted it to do.
> 2. if so, is it the same as saying 'ptr1<size && ptr1<pass' ?
No, it's the same as saying
(ptr1<size) && (pass!=0)
The reason I was suggesting that it may not do what the author intended is
because the first statement:
pass=rx_str[ptr1+1] gets evaluated exactly one time, so pass does not get
updated each loop iteration. Unless pass is updated in the loop body, or
the programmer intended to only test the 'firsrt' value of pass, then the
code is probably broken.
Apparently, Mr. Webb didn't read my RESEND of my response where I
caught my 'mistake' and corrected it. And what is this high and
mighty remark? I was just answering the question and trying to be
as plain and simple as I could. I'm sorry you can't understand that.
Seems to me you're the one trying to act high and mighty. What
language. What an attitude.
>
> > Otherwise, it fails. Same with 'pass'. If pass is ever anything
> > other than '1', it fails, and consequently, so does the whole
> > statement. Very simple. Very easy to understand.
> >
> > Regards,
> >
> > Jim
>
> Save your high and mighty bullshit for the times when you are right, Jim.
>
> Your statement:
> "If pass is ever anything other than '1', it fails..."
>
> is incorrect.
>
>
> For all nonzero values of pass, it is evaluated as TRUE. It only
> evaluates to false when pass==0
>
> -Steve
>
> --
> http://www.piclist.com hint: To leave the PICList
> piclist-unsubscribe-requestspammitvma.mit.edu
From the variable name, "rx_str", it appears that
the for loop is traversing an array of characters,
where the "ptr1<size" part is keeping it within the
bounds, and the "pass" part is checking for a NULL
character, or the end of the string. So, the
"pass" by itself is the same as "if (pass != NULL),
continue", which will break out if a NULL terminator
is seen before the end of the array is reached.
> i apologise if this post appears more than once - just realised that the
> 1st time i sent it, my [OT] channel was turned off!
>
> hello everyone,
>
> i came across the following 'for' statement in a code:
>
> for (pass=rx_str[ptr1+1]; ptr1<size && pass; ptr1++) {
>
> i'm curious about about the 'ptr1<size && pass' of the statement as i
> have never come across a for loop layed out in this way.
> 1. is this valid?
> 2. if so, is it the same as saying 'ptr1<size && ptr1<pass' ?
>
> TIA
> Seyi
>
> --
> http://www.piclist.com hint: To leave the PICList
> piclist-unsubscribe-requestspamBeGonespamBeGonemitvma.mit.edu
>
>
>
__________________________________________________
FREE voicemail, email, and fax...all in one place.
Sign Up Now! http://www.onebox.com
> Apparently, Mr. Webb didn't read my RESEND of my response where I
I was responding to your "RESEND", or at lest your second message on the
subject. Re-read it.
> caught my 'mistake' and corrected it. And what is this high and
> mighty remark? I was just answering the question and trying to be
Perhaps I mis-interpreted your "Very simple. Very easy to understand."
statement. What I saw was one post asking for help, a half dozen or so
posts giving incorrect / partially correct information, and among all of
this confusion your (incorrect) post saying that it was "Very simple."
and "Very easy to understand."
I probably let my frustration get the best of me, and as a result I
over-reacted. I concede that the comment was inappropriate, and for that
I apologize. As for the factual content, I stand by my original post.
The person who wrote this code wants to make sure that the loop won't run
if pass=0. If pass is anything but 0, then it will run until ptrl is
equal to or greater than size.
>
> hello everyone
>
> i came across the following 'for' statement in a code:
>
> for (pass=rx_str[ptr1+1]; ptr1<size && pass; ptr1++) {
>
> i'm curious about about the 'ptr1<size && pass' of the statement as i
> have never come across a for loop layed out in this way.
> 1. is this valid?
> 2. if so, is it the same as saying 'ptr1<size && ptr1<pass' ?
>
> Thanx in advance
> Seyi
>
> --
> http://www.piclist.com hint: To leave the PICList
> TakeThisOuTpiclist-unsubscribe-requestspamRemoveMEmitvma.mit.edu
> -----Original Message-----
> From: Oluseyi Odeinde [SMTP:seyiRemoveMEOCEAN.CF.AC.UK]
> Sent: Friday, January 26, 2001 1:54 PM
> To: EraseMEPICLISTSTOPspamRemoveMEMITVMA.MIT.EDU
> Subject: Re: [OT]: quick question on C
>
> come to think of it, after taking a 2nd look at that statement - i think
> it
> really means:
>
> for (pass=rx_str[ptr1+1]; ptr1<size && pass=1; ptr1++) {
>
> beacuse we are dealing with a boolean AND, therefore for pass to be 'true'
> it
> must equals 1. makes sense?
>
Almost, apart from the fact that any non-zero value can be "true" in C, so
this should be:
for(pass=rx_str[ptr1+1]; ptr1<size && pass != 0; ptr1++) etc.
This is pretty nasty coding, the author of Code Complete would have a heart
attack just looking at this! It could almost certainly be simplified by
using a while loop.
> > > for (pass=rx_str[ptr1+1]; ptr1<size && pass; ptr1++) {
> > >
> > > 2. if so, is it the same as saying 'ptr1<size && ptr1<pass' ?
I think the source of the confusion is operator precedence. The logical
operators (&&, ||) have lower precedence than math comparison, so:
a. ptr1<size && pass
Is the same as
b. (ptr1<size) && pass
Which is the same as
c. (ptr<size) && (pass != 0)
While the rules are fixed and eventually decipherable, they're confusing
enough at this end of things that most code reviewer here would ask the
code to be changed to at least form "b"...
Second, My RESEND had "*****RESEND*****UPDATED*****' all over it,
so what you responded to was not my resend, but just my second
message.
Third, it is 'very simple and easy to understand' to me. I'm at
fault regarding this one as maybe (apparently) the asker of the
original question isn't as well versed in 'C' as you, I and others
are. I just assumed that he would understand what I was saying.
Fourth, and please note this is not a flame, but just a question
asked calmly and sincerely. You are you to tell me what I should
and shouldn't do with my opinion(s)? I am a part of this list as
much as you are. I try not to judge and repremand everyone I don't
agree with, (and sometimes there are many). I won't say I haven't,
but I do try to control my judgements. And once in a while, I do
jump on the soapbox and rant about something, but it isn't directed
to anyone in particular, rather everyone in general. And now that
I think about it, I guess I'm doing what I just said you have no
right to do. That is, I'm saying, in a roundabout way, that your
opinion about my opinion is wrong. So forgive me too.
Just so all of you know. I will answer a question when I believe I
have the answer to the question. But, I do NOT CLAIM to be an
expert at PIC's or programming languages. I believe I am well
versed enough to be able to answer a wide range of questions about
both PIC's and programming languages relating to PIC's. But, I will
occasionally give the wrong answer. It's inevitable. I'm human.
So, I don't mind when I get a correction from someone (anyone).
The thing that really grinds my gears is the ones that come off
with an attitude. So, If you have an attitude, and see that I have
answered a question wrong, check the attitude at the door, and then
send me your response as to why I'm wrong. I will accept your
criticism for my mistake(s) without a whimper if given in the
correct tone or spirit. Otherwise I will lend a deaf ear to them.
Is this an attitude on my part? I don't think so. It's a philosphy
that I try to live by, and have tried to teach my kids as as I
believe you you will get better response and acceptance by people
if you correct them in a humane and dignified manner rather than
browbeating them. So far in my 46 years on this earth, this has
not failed. I intend to keep believing and practicing this
philosophy for the rest of my life. However long or short that may
be.
So with that in mind, let's just forget the whole thing ever
happened, and go on with answering the questions posted to the list.
One more thing. I apologise to the entire list for giving any
misinformation about any question(s) that I have answered. I'll
try to not let it happen again.
>
> > Apparently, Mr. Webb didn't read my RESEND of my response where I
>
> I was responding to your "RESEND", or at lest your second message on the
> subject. Re-read it.
>
> > caught my 'mistake' and corrected it. And what is this high and
> > mighty remark? I was just answering the question and trying to be
>
> Perhaps I mis-interpreted your "Very simple. Very easy to understand."
> statement. What I saw was one post asking for help, a half dozen or so
> posts giving incorrect / partially correct information, and among all of
> this confusion your (incorrect) post saying that it was "Very simple."
> and "Very easy to understand."
>
> I probably let my frustration get the best of me, and as a result I
> over-reacted. I concede that the comment was inappropriate, and for that
> I apologize. As for the factual content, I stand by my original post.
>
>
> -Steve
>
> --
> http://www.piclist.com hint: To leave the PICList
> EraseMEpiclist-unsubscribe-requestRemoveMEmitvma.mit.edu
for(pass=rx_str[ptr1+1]; ptr1<size && pass != 0; ptr1++) etc.
This is pretty nasty coding, the author of Code Complete would have a
heart attack just looking at this! It could almost certainly be
simplified by using a while loop.
Or even something like:
for (pass = rx_str[ptr1+1]; ptr1<size; ptr1++) {
if (pass == 0)
break;
On the other hand, fragments similar to
((ptr < size) && string[ptr])
or ((ptr < endptr) && *ptr)
are pretty standard in "good" C string handling code. Bad C string
handling code doesn't check for passing the end of the string, and you
might see (ptr < end) fragments that have been tacked onto existing
for loops as "bug fixes."
> -----Original Message-----
> From: Stephen B Webb [SMTP:sbwebb0KILLspamEraseMESAC.UKY.EDU]
> Sent: Friday, January 26, 2001 8:39 PM
> To: EraseMEPICLIST@spam@@spam@MITVMA.MIT.EDU
> Subject: Re: [OT]: quick question on C
>
> > i came across the following 'for' statement in a code:
> >
> > for (pass=rx_str[ptr1+1]; ptr1<size && pass; ptr1++) {
> >
> > i'm curious about about the 'ptr1<size && pass' of the statement as i
> > have never come across a for loop layed out in this way.
> > 1. is this valid?
>
> Yes, it is valid. It is unclear code, and I'm not sure that it does what
> the author wanted it to do.
>
> > 2. if so, is it the same as saying 'ptr1<size && ptr1<pass' ?
>
> No, it's the same as saying
>
> (ptr1<size) && (pass!=0)
>
>
> The reason I was suggesting that it may not do what the author intended is
> because the first statement:
>
> pass=rx_str[ptr1+1] gets evaluated exactly one time, so pass does not get
> updated each loop iteration. Unless pass is updated in the loop body, or
> the programmer intended to only test the 'firsrt' value of pass, then the
> code is probably broken.
>
> -Steve
>
> --
> http://www.piclist.com hint: To leave the PICList
> @spam@piclist-unsubscribe-requestspamKILLspammitvma.mit.edu
>
-- http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads
> -----Original Message-----
> From: Bob Ammerman [SMTP:KILLspamRAMMERMANspam_OUTPRODIGY.NET]
> Sent: Friday, January 26, 2001 7:48 PM
> To: spam_OUTPICLISTTakeThisOuTMITVMA.MIT.EDU
> Subject: Re: [OT]: quick question on C
>
> ----- Original Message -----
> From: Alok Dubey <.....adubey.....RemoveMEWIPRO.CO.IN>
> To: <spam_OUTPICLISTTakeThisOuTEraseMEMITVMA.MIT.EDU>
> Sent: Friday, January 26, 2001 6:54 AM
> Subject: Re: [OT]: quick question on C
>
> > > for (pass=rx_str[ptr1+1]; ptr1<size && pass; ptr1++) {
>
>
> > i hnk it is a boolean of size and pass which is then compared to ptr1
> alok
>
> No, '&&' is lower in precedence than '<' so this is treated as:
>
> (ptr1 < size) && pass
>
> which in turn is treated as
>
> (ptr1 < size) && (pass != 0)
>
> Bob Ammerman
> RAm Systems
> (contract development of high performance, high function, low-level
> software)
>
> --
> http://www.piclist.com hint: To leave the PICList
> EraseMEpiclist-unsubscribe-requestspamBeGoneKILLspammitvma.mit.edu
>
-- http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads
> -----Original Message-----
> From: Bob Ammerman [SMTP:RemoveMERAMMERMANspamBeGonespamPRODIGY.NET]
> Sent: Friday, January 26, 2001 8:05 PM
> To: @spam@PICLISTspamMITVMA.MIT.EDU
> Subject: Re: [OT]: quick question on C
>
> > come to think of it, after taking a 2nd look at that statement - i think
> it
> > really means:
> >
> > for (pass=rx_str[ptr1+1]; ptr1<size && pass=1; ptr1++) {
>
> Well, first, you probably meant to say 'pass==1'.
>
> > beacuse we are dealing with a boolean AND, therefore for pass to be
> 'true'
> it
> > must equals 1. makes sense?
>
> But now, a numeric or pointer type is treated as true as long as it is
> non-zero (non-null for pointers)
>
> Bob Ammerman
> RAm Systems
> (contract development of high performance, high function, low-level
> software)
>
> --
> http://www.piclist.com hint: To leave the PICList
> TakeThisOuTpiclist-unsubscribe-requestKILLspam@spam@mitvma.mit.edu
>
-- http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads
Each expr can be any valid C expression, as complex as you like. Many
books on C omit to state this clearly. Similar things apply to the other
control statements.
Peter
-- http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads