473,405 Members | 2,415 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,405 software developers and data experts.

Array indices

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.

Nov 16 '06 #1
3 1191
mm*****@gmail.com 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) 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 16 '06 #2
In article <11*********************@h54g2000cwb.googlegroups. com>,
<mm*****@gmail.comwrote:
>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
Nov 16 '06 #3
mm*****@gmail.com 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 ---
Nov 16 '06 #4

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

Similar topics

2
by: Steve | last post by:
I'm working on an e-commerce site, and one of the things I need to do is split an existing order into two orders. The problem I'm having is not creating the new order, but getting the remaining...
6
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
11
by: Dr John Stockton | last post by:
Q1 : Given an array such as might have been generated by var A = is there a highly effective way of reducing it to - i.e. removing the undefineds and shifting the rest down? ...
9
by: Randell D. | last post by:
Folks, I can program fairly comfortably in PHP and can, for the most part using these skills and others that I've picked up over the years manage to read/understand most code in Javascript... so...
7
by: Adam Hartshorne | last post by:
As a result of a graphics based algorihtms, I have a list of indices to a set of nodes. I want to efficiently identify any node indices that are stored multiple times in the array and the...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
7
by: MBS | last post by:
Greetings. I'm still pretty new to PHP and I have a question. I know that variables are preceeded by a "$" (dollar sign). Typically, a variable has one value, unless it is an array. Then it is...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
9
by: dennis.sam | last post by:
Hi, Is there away to define a multi-dimensional array with respect to the number of dimensions the array has? For example, given a user spec of "a b c d", I want to create a 4 dimensional array...
11
by: memeticvirus | last post by:
I have an array cli::array<float, 2and I would like to access a subset of it's values by compiling an array of pointers. But, it's not possible to create an array of type...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.