This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ Comma Operator
Uses of C comma operator
I am not new to C++, but this is the first time I see the following code:
int a=0;
int b=(a=2,a+1);
That is C++ code. Can you tell me what is going on here? And how variable b gets value 3?
This code is equivalent to this:
int a = 2 ;
int b = a + 1 ;
First expression to the left of comma gets evaluated, then the one to its right. The result of the right most expression is stored in the variable to the left of = sign.
Look up the comma operator for more details.
http://en.wikipedia.org/wiki/Comma_operator
(a = 2, a + 1); return 3 because in general case operator (a, b) return b, and calculation in (a, b) started from right to left. So, in your case, (a = 2, a + 1) return a + 1, and after operator a = 2 was executed a + 1 return 3.
Related
I've seen instances where someone would use return statements with multiple values. For example: return 8, 10, 6; As far as I know, only one of these values will actually be returned. What is the benefit of using return this way?
Specifically, this question appears on my homework.
The statement: return 2 * 3 + 1, 1 + 5; returns the value ____.
I know it would always return 6, so why would I ever write it like this?
Forgive me if this is a simple question. I am still somewhat new to programming.
The statement return 2 * 3 + 1, 1 + 5; returns the value 6.
This the trick of comma operator in C++. You can read more about it here:
https://en.cppreference.com/w/cpp/language/operator_other
A comma operator is basically a list of expressions separated by commas, they will be evaluated from left to right, and the result of the last item will be treated as the result of the whole comma operator.
Here is a simple example demonstrating how comma operator works.
int foo() {
int i = 1;
return i += 2, i++, i + 5; // This is a comma operator with three items
// i += 2 will be evaluated first, then i == 3
// i++ will be evaluated second, then i == 4
// i + 5 will be evaluate last, and the result is 9
// the result of the last item is returned by the return statement
}
int main() {
std::cout << foo();
return 0;
}
This code prints 9.
When you do this you are avoiding using math in your function block and this make your function like working 3 time and result would be the one you want
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).
This question already has answers here:
return statement in ternary operator c++
(5 answers)
Closed 3 years ago.
I am writing a simple factorial function to practice recursion within c++.
I wanted to write it in ternary format but it was not working so I wrote it out first to see if it worked the more traditional way.
Ternary Format:
num > 1 ? num * factorial(num-1) : return 1;
Traditional :
if (num > 1){
return num * factorial(num-1);
}
else {
return 1;
}
I am simply printing the result of factorial(5) which should display 120. While it works for the second it does not for the first. Why can I not use return in the ternary format? Or, am I using it wrong?
You can use factorial just fine in the conditional form, but you can't use return. return is a statement, whereas the conditional operator expects expressions. Conveniently, the conditional operator itself is an expression, which lets you do this.
return num > 1 ? num * factorial(num-1) : 1;
Note that we're returning the result of the whole expression, not just the num > 1 piece. If I wanted to be redundant with parentheses, I could do this, which is semantically equivalent.
return (num > 1 ? num * factorial(num-1) : 1);
This question already has answers here:
Post-increment and Pre-increment concept?
(14 answers)
Closed 6 years ago.
so I am looking at the following snippet of code
int a = 3;
int b = 2;
b = a++;
cout << ++b;
My understanding line by line is:
initiate a = 3
initiate b = 2;
assign the value of (a+1) to b, so b = 4;
print b+1, which is 5.
However this is wrong, can someone explain this in simple terms? I am new to C++
In the statement:
b = a++
a is evaluated for the assignment to b and after that a is then incremented so a = a+1.
On the other hand:
b = ++a
Increments a so a = a+1 before a is then evaluated for assignment to b.
You can look deeply here:
Pre-increment or post-increment in C/C++
In C++ when an increment or decrement is used in an expression, the position of "++" or "--" matters.
If the operator is before a variable then the increment or decrement will be done before the end of the expression, in the opposite situation the increment or decrement will be done after the entire expression is executed.
int b = ++a; // first add +1 to a, then assign a to b
int b = a++; // assign a to b, then add +1 to a
Here you go:
initiate a = 3
initiate b = 2;
assign the value of (a) to b, so b = 3 and increment a to 4;
print b+1, which is 4.
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).