Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Array indices

Reply
Thread Tools

Array indices

 
 
mmu2643@gmail.com
Guest
Posts: n/a
 
      11-16-2006
Hi,

I had a noobie question:

i = 0;
while (len > i) && (SOME_VAL != arr [i])
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.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      11-16-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> I had a noobie question:
>
> i = 0;
> while (len > i) && (SOME_VAL != arr [i])
> 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.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      11-16-2006
In article <(E-Mail Removed). com>,
<(E-Mail Removed)> wrote:

>I had a noobie question:


>i = 0;
>while (len > i) && (SOME_VAL != arr [i])
> 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.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      11-16-2006
(E-Mail Removed) wrote:
> Hi,
>
> I had a noobie question:
>
> i = 0;
> while (len > i) && (SOME_VAL != arr [i])
> 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])) ++i;
or..
while (len > i && SOME_VAL != arr[i]) ++i;

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

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using an array value as indices for an array KLaus Blank VHDL 1 07-07-2008 07:38 PM
64-bit array indices RFE Thomas Hawtin Java 2 08-12-2006 04:23 AM
checking array indices Sterten C Programming 26 07-27-2004 01:24 AM
using a string as array indices Justin Perl 1 06-02-2004 11:38 PM
how to efficiently do sorting and get array of indices? b83503104 C Programming 3 05-21-2004 10:44 AM



Advertisments