Array indices

M

mmu2643

Hi,

I had a noobie question:

i = 0;
while (len > i) && (SOME_VAL != arr )
i++;

In the above snippet if len is the number of elements in arr, when i ==
len, since the first condition fails, will the second one not be
evaluated at all?

Hope to hear from someone out there. Thanks.
 
K

Keith Thompson

I had a noobie question:

i = 0;
while (len > i) && (SOME_VAL != arr )
i++;

In the above snippet if len is the number of elements in arr, when i ==
len, since the first condition fails, will the second one not be
evaluated at all?

Hope to hear from someone out there. Thanks.


Quick answer: Yes. But read on.

The second condition won't be evaluated because the code you posted
won't even compile. You have to enclose the entire condition in a
while statement in parentheses; you haven't done that.

When you post code here, it's (almost) always best to try compiling it
first, and to copy-and-paste the exact code that you compiled.
Otherwise, you risk accidentally introducing irrelevant errors (typos
or whatever) that can obscure what you're actually asking about. If
possible, you should post an entire compilable program, not just a
code fragment, so we can try it ourselves if necessary. It happens in
this case that the error you made (the missing parentheses) was
trivial, and the problem you're actually asking about was still
obvious, but that's not always going to be true.

Finally, your question is really about how the "&&" operator works.
Any decent C textbook will answer this question for you. We're happy
to help here, but it's easier for everyone if you can just look it up.
The comp.lang.c FAQ is at <http://www.c-faq.com>. Section 18 has
references to several good book. (K&R2 is widely considered to be the
best C tutorial; H&S5 is a good reference; see 18.10 to find out what
those abbreviations mean.) (BTW, the FAQ incorrectly says that H&S is
in its 4th edition; it's actually in its 5th.)
 
W

Walter Roberson

I had a noobie question:
i = 0;
while (len > i) && (SOME_VAL != arr )
i++;

In the above snippet if len is the number of elements in arr, when i ==
len, since the first condition fails, will the second one not be
evaluated at all?

Correct. && does not evaluate the right hand side if the left hand
side is false, and || does not evaluate the right hand side if
the left hand side is true.
 
J

Joe Wright

Hi,

I had a noobie question:

i = 0;
while (len > i) && (SOME_VAL != arr )
i++;

In the above snippet if len is the number of elements in arr, when i ==
len, since the first condition fails, will the second one not be
evaluated at all?

Hope to hear from someone out there. Thanks.

Badly formed. The while condition must be completely parenthesized,
something like..
while ((len > i) && (SOME_VAL != arr)) ++i;
or..
while (len > i && SOME_VAL != arr) ++i;

...and yes, if len > i is false, the second condition is not evaluated.
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top