Q: sequence of excution in if

Z

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<int> v;
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
 
A

Alf P. Steinbach

* 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<int> v;
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.
 
D

Daniel T.

"zl2k said:
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<int> v;
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.)
 
K

Kai-Uwe Bux

zl2k said:
vector<int> v;
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
 
J

Jerry Coffin

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<int> v;
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.
 

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
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top