What can be a subexpression? - c++

I read "C++.Primer plus. Stephen Prata"(6th edition).
On page 209 was:
y = (4 + x++) + (6 + x++);
The expression 4 + x++ is not a full
expression, so C++ does not guarantee that x will be incremented
immediately after the subexpression 4 + x++ is evaluated. Here the
full expression is the entire assignment statement, and the semicolon
marks the sequence point, so all that C++ guarantees is that x will
have been incremented twice by the time the program moves to the
following statement. C++ does not specify whether x is incremented
after each subexpression is evaluated or only after all the
expressions have been evaluated, which is why you should avoid
statements of this kind.
And I read "Sequence Points and Expression Evaluation" Visual Systems Journal, August 2002. Klaus Kreft & Angelika Langer.
There was:
x[i]=i++ + 1;
Let's assume variable i has the value 1 before we enter the statement.
What will be the result of evaluation of this expression? The correct
answer is: we don't know. However, programmers ever too often believe
that they know what this program fragment does. Typical answers
include: "x[1] will have the value 2", or "x[2] will have the value
2", or even "x[1] will have the value 3".
The third option is definitely wrong. This will not happen because i++
is a postfix increment and returns i's initial value 1; hence the
value of the right hand side of the assignment is 2, and definitely
not 3. [...] So far so good, but we do not know
which entry of the array x will be modified. Will the index be 1 or 2
when the right hand side value will be assigned to x[i]?
There is no definite answer to this question. It fully depends on the
order in which the compiler evaluates the subexpressions. If the
compiler starts on the right hand side of the assignment and evaluates
i++ + 1 before it figures out which position in the array x must be
assigned to then x[2] will be modified because i will have already
been incremented in the course of evaluating the subexpression i++.
Conversely, if the compiler starts on the left hand side and figures
out that it must assign to position i in array x, which at that time
will still be position 1, before it evaluates the right hand side then
we'll end up with a modification of x[1]. Both outcomes are equally
likely and equally correct. "
How understand where is subexpression?
4 + x++ and 6 + x++ are subexpressions, because they are into round brackets?
x[i] and i++ + 1 are subexpression? Why?
I'm interested in this, because I want to understand where side effect can happened in the hypothesis.

Breaking this down, the line
y = (4 + x++) + (6 + x++);
is an expression-statement. Such a thing consists of an expression followed by a ;, so
y = (4 + x++) + (6 + x++)
is an expression.
Since this expression is not part of another expression (but only of an expression-statement), it is a full-expression. A sub-expression on the other hand is an expression that is part of another expression. In the following, I will use capital letters to name expressions, rather than C++ identifiers.
The full-expression above is an assignment-expression of the form:
y = A
where A is the remaining additive-expression
(4 + x++) + (6 + x++)
An additive expression is of the form X + Y, so we break this down into two expressions
(4 + x++)
(6 + x++)
The first one consists of an expression of the form (Z), where Z is 4 + x++. And 4 + x++ consists of two expressions 4 and x++. And so on. All of these expressions are part of
y = (4 + x++) + (6 + x++)
and hence they are sub-expressions of the above expression.

What can be a subexpression?
Any expression can be a subexpression. Although, some expressions may not be subexpressions of certain other expressions.
4 + x++ and 6 + x++ are subexpressions
Correct. Both of those are arithmetic expressions, additions to be more specific.
because they are into round brackets?
Well, sort of. Being inside parentheses, they are indeed the subexpression of the parenthesized expression.
† In general, they're subexpressions because they're expressions, but are also part of another expression.
x[i] and i++ + 1 are subexpression? Why?
Yes, they are. See †.
Here is a handy list of all possible expressions in c++.
Let's find the subexpressions in y = (4 + x++) + (6 + x++);. The first expression that has no subexpressions is 4. It is a literal. It is a subexpression of 4 + x++ which is an addition. Additions have the form A + B. In this case, subexpression A is 4 an subexpressiond B is x++, which is a post increment. Oh, but that contains a subexpression too: x. It is an identifier, and contains no subexpressions. 4 + x++ is a subexpression of the parenthesized expression (4 + x++). That is a subexpression of (4 + x++) + (6 + x++) which is a subexpression of y = (4 + x++) + (6 + x++); which is an assignment. The assignement is a full-expression - not a subexpression. I left some of the subexpressions unexplored, and I shall leave them as an exercise for the reader.

Related

What does "expression must be a modifiable lvalue" mean?

int x;
(rand() % 100) + 1 = x;
It keeps saying:
expression should be a modifiable lvalue
Left and right operands of = are not interchangable.
It's the left operand that's being assigned to, so it has to be assignable.
Given int x;, x is assignable (a 'modifiable lvalue'), but x + 42 is not (an rvalue).
It means that you've put the wrong things on each side of the assignment operator (=). The expression on the left should be something you can assign a value to (the "l" in "lvalue" stands for "left"), and the expression on the right should compute the value you want to assign.
Try
x = (rand() % 100) + 1;
instead.
The lvalue always has to be a changeable/modifiable one.
So you can't do :
int a ;
3 = a ;
This means that you are trying to change 3 to a which is not possible because 3 is a constant.
So similarily,
Do this :
x = (rand() % 100) + 1;
And, the error should be gone.

What does the following expression does: x = (a1,a2,...,n); [duplicate]

This question already has answers here:
How does the Comma Operator work
(9 answers)
Closed 3 years ago.
I was reading this question about sequence points and I saw this line:
i = (i, ++i, 1) + 1; // well defined (AFAIK)
I was wondering how is the following syntax called and what are its effects?
i = (a1, a2, ...,an);
This is the comma operator for int operands, together with grouping through parentheses, which is always allowed. First,
(i, ++i, 1)
evaluates i, then ++i, then 1 and returns the result of the last expression (which is 1). Then
(i, ++i, 1) + 1
is the same as
1 + 1;
which results in 2, so i is set to 2 here. Note that without the parentheses, the result would in most cases not be the same, as the comma operator has the lowest possible precedence (thanks to #dbush for helping me out here in the comments).

What exactly does the "+=" operator do? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I already know that x += 1 means x = x + 1.
The =-symbol allocates the value of the arithmetic expression on it's right to the variable on it's left.
But if i have an expression like x += y % 3, does that mean x = (x + y) % 3or x = x + (y % 3)?
The += operator is both allocating a value to a variable and expressing arithmatics, which has me confused.
Unfortunately I currently can't just test some code to check, so I hope I can get some help here.
I'm new to C++, so some of these very basic things still confuse me...
The =-symbol allocates the value of the arithmetic expression on it's right to the variable on it's left
It assigns the result. Allocation is something different, and it'll be important to remember the difference later (dynamic allocation in particular will be really confusing if you conflate it with assignment).
But if i have an expression like x += y % 3, does that mean x = (x + y) % 3 or x = x + (y % 3)?
Part of the reason for having the compound +=, -= etc. operators is that you don't expand the expression like this, avoiding the ambiguity created by your re-write.
x += y % 3
can be read as
tmp = y % 3; // evaluate right hand side
x += tmp; // assign to left hand side
(you can expand x += tmp to x = x + tmp if you really want to, after tmp has been evaluated).
The rules are all documented here in any case, and anyway you absolutely can just test some code to check: https://ideone.com/81tvjH
There is a confusion here, because you think x += 1; is always x = x + 1. This is true for integers, doubles... but generally NOT the case for other objects. Lots of implementations don't create a temporary object. Or could do something very evil.
So then it's a call to operator+=, which takes as the argument the result of the right hand side of the assignment.
So for integers, it's x = x + (y % 3).

C++ Boolean with Integers

OK so obviously this question might sound dumb for more experienced people, but, for the following lines the result I get is 0:
int x = 2,
y = -2;
cout << (x++ - y && (--x + y));
I understand it means that either one of these two expressions equals 0, but how? As far as I understand, this should be (3 && -1)?
Also, a little subquestion: when does x++ exactly take effect? On the next occurance of x within the same expression, after the left-shift operator within the same line, or in the next statement?
Thank you!
As far as I understand, this should be (3 && -1)?
you understand wrong:
first left side is fully evaluated, as it is necessary for short circuit evaluation with logical and (details can be found here)
x++ - y == 4 // as result of x++ == 2 so (2-(-2)), after that x == 3
result is true so right side is evaluated:
--x + y == 0 // as result of --x == 2 so (2+(-2)), after that x == 2
result on the right is false so result of and is false as well which printed by std::ostream as 0
Note: short circuit evaluation of logical or and and operations make such code valid (making them sequenced) but you better avoid such questionable expressions. For example simple replacing logical and to binary would make it UB.

How should i read an assignment?

I'm studying C++ as a beginner and my book explain me what is assignment but i can't understand a little concept that the book is trying to make me understand:
int a = 3; // a starts out with the value 3
a = a + 7; // a gets the value of a + 7 (that is, 10)
What my book says is: the last assignment deserves notice.First of all it clearly shows that = doesn't mean equals-clearly, a doesn't equal to a + 7.It means assignment, that is, to place a new value in a variable. What does it mean ? how should i read an assignment?
For example:
a = a + 7; // should i read it as: a is equal to a + 7 ?
The code might be expanded in following way:
int a; // declaration of the variable. a has undefined value.
a = 3; // Assign the value 3 to a. a is 3.
a = a + 7; // Assign value of (a + 7) to a. a is 10.
The expression (a + 7) is evaluated first and the value of (3 + 7) is assigned to a.
The token = is used for assignment and doesn't have anything to do with equality.
The assignment means that the right-hand side (rhs) will be evaluated to a single value, then assigned to the left-hand side (lhs)
a = a + 7;
Now, rhs --> a + 7 --> 3 + 7 --> 10, now we have a single value, and will assign 10 to a
In C++ = is the assignment operator. == is the equality operator.
Read the chapter about operator precence (the order in which operators performs operations on its operands).
= (the assignment operator) is a binary operator, has lowest precedence (that is the reason first a+7 operation is performed) and it assigns the result of an expression on its right side to the variable on its left side.
For, a = a + 7, never read it as a equals a + 7, but make habit of reading it as a gets assigned the value of a + 7 (or whatever the expression) is.
The = also replaces the exiting value of variable on left side (a) with the result of expression on right side ( a + 7).