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

Q: sequence of excution in if

hi, all
If I am using gcc, is the sequence of excution in "if" the same as what
I wrote? Here is an example:

vector<intv;
unsigned short a;
.....
if (a < v.size() && v[a] 0)
cout<<v[a];

I expect that the "a < v.size()" will excute first so that I won't get
into the segmentation error in cas of a v.size(). But I am not sure
if gcc will do in this way. Thanks for your comments.

zl2k

Jul 24 '06 #1
4 1221
* zl2k:
hi, all
If I am using gcc, is the sequence of excution in "if" the same as what
I wrote? Here is an example:

vector<intv;
unsigned short a;
....
if (a < v.size() && v[a] 0)
cout<<v[a];

I expect that the "a < v.size()" will excute first so that I won't get
into the segmentation error in cas of a v.size(). But I am not sure
if gcc will do in this way. Thanks for your comments.
The built-in && introduces a sequence point. The left expression is
completely evaluated before the right one is evaluated, which it is if
and only if the left expression evaluates to non-zero.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 24 '06 #2
In article <11**********************@b28g2000cwb.googlegroups .com>,
"zl2k" <kd*******@gmail.comwrote:
hi, all
If I am using gcc, is the sequence of excution in "if" the same as what
I wrote? Here is an example:

vector<intv;
unsigned short a;
....
if (a < v.size() && v[a] 0)
cout<<v[a];

I expect that the "a < v.size()" will excute first so that I won't get
into the segmentation error in cas of a v.size(). But I am not sure
if gcc will do in this way. Thanks for your comments.
Yes it will. It's a requirement of the && operator (except when it is
overridden.)
Jul 24 '06 #3
zl2k wrote:
vector<intv;
unsigned short a;
....
if (a < v.size() && v[a] 0)
cout<<v[a];

I expect that the "a < v.size()" will excute first so that I won't get
into the segmentation error in cas of a v.size(). But I am not sure
if gcc will do in this way.
Any standard compliant compiler will do it the way you expected. As far as I
know, gcc is compliant in this regard.

Best

Kai-Uwe Bux
Jul 24 '06 #4
In article <11**********************@b28g2000cwb.googlegroups .com>,
kd*******@gmail.com says...
hi, all
If I am using gcc, is the sequence of excution in "if" the same as what
I wrote? Here is an example:

vector<intv;
unsigned short a;
....
if (a < v.size() && v[a] 0)
cout<<v[a];

I expect that the "a < v.size()" will excute first so that I won't get
into the segmentation error in cas of a v.size(). But I am not sure
if gcc will do in this way. Thanks for your comments.
You've had a number of correct replies already, but I thought it was
worth adding one point that they didn't really make clear: this
really has nothing to do with the 'if' statement. It's a
characteristic of the '&&' (and '||', for that matter) that happens
whether it's inside of an if statement or not.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 24 '06 #5

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

Similar topics

10
by: Anthony Best | last post by:
I'm working on an idea that uses sequences. I'm going to create a table like this: id serial, sequence int, keyword varchar(32), text text for every keyword there will be a uniq sequence...
5
by: Eric E | last post by:
Hi, I have a question about sequences. I need a field to have values with no holes in the sequence. However, the values do not need to be in order. My users will draw a number or numbers from...
1
by: Marek Lewczuk | last post by:
Hello, I would like to ask if my problem with sequence is a proper behavior or this is a bug (probably not)... I have a table: CREATE TABLE "testtable" ( "serialfield" SERIAL,...
3
by: kevin | last post by:
Is that even possible? I am creating a web service in .NET to expose some already created .NET programs to other groups. One group is writing the client in PERL, and thus wishes the wsdl schema...
8
by: regis | last post by:
Greetings, about scanf matching nonempty sequences using the "%" matches a nonempty sequence of anything except '-' "%" matches a nonempty sequence of anything except ']" matches a nonempty...
14
by: pat270881 | last post by:
hello, I have to implement a sequence class, however the header file is predefined class sequence { public: // TYPEDEFS and MEMBER CONSTANTS
6
by: Defcon2030 | last post by:
<bHey, can someone help me with this? I've been working on it for a few days now, and my head's starting to spin... </b> // FILE:ex1_imp.cxx // // // // CLASS IMPLEMENTED: sequence (see ex1.h...
1
davydany
by: davydany | last post by:
Hey guys...a n00b Here for this site. I'm making a sequence class for my C++ class. And The thing is in the array that I have, lets say i put in {13,17,38,18}, when i see the current values for the...
5
by: Anan18 | last post by:
Hello sir, I'm supposed to Implement and Test the sequence Class Using a Fixed-Sized Array (Chapter 3), from Data Structures & Other objects using c++. The header file is provided, and so is a test...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.