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.
Related
I'm learning c++ and currently learning about operator precedence. I'm playing with the following examples. Imagine each piece as distinct pieces of code run at separate times, not multiple code blocks within the same method.
int b = 4;
int result = ++b;
// In the above example the result will be 5, as expected.
int b = 4;
int result = ++b + b;
// Here the result will be 10 as expected.
int b = 4;
int result = ++b + ++b;
Here the result is 12. I don't understand why. Shouldn't the compiler evaluate ++b changing 4 to 5, then ++b changing 5 to 6, resulting in 5+6 = 11?
It's undefined behaviour, violating sequence rules.
Between the previous and next sequence points a scalar object must have its stored value modified at most once by the evaluation of an expression, otherwise the behavior is undefined.
int b = 4;
int result = ++b + ++b;
I have these lines of code:
int a = 10, b = 1;
a = --b ? b : (b = -99);
cout << "a= " << a << "b= " <<b<< endl;
the output gives me b=-99 as a is not equal to 0(which makes senses) but it also changes the value of a to a=-99 how?
Your code is the equivalent of:
int a = 10, b = 1;
b -= 1; // b == 0
int x;
if (b != 0) x = b;
else x = b = -99;
a = x;
// at this point a and b have the same value
The ternary operator works as follows:
if (--b != 0) { // b is not 0 means true
a = b;
} else { // b is 0 means false
a = (b = -99);
}
You assign the value to a, so --b is 0 which is considered as false. Then you assign to b value -99 and afterward, you assign b to a. So, both variables have their values -99 in the end. Hope it will help you.
According to ternary operator, The first argument is a comparison argument(condition), the second is the result upon a true comparison, and the third is the result upon a false comparison.
So In Your case, the condition is not True, So the false statement get executed.
so now b has -99.
In c, c++, java we've seen, assigns the value of the variable
a = b = -99
In this, the both a & b gets the same value. Like this, ternary operator also assigning the value of the variable from false statement .
(a = (b = -99 ))
I think what is confusing you here is that, in C, an expression (such as b = -99) has both consequences and a value. The consequence of b = -99 is that b is assigned the value of -99; but note also that the value of this expression is -99.
Thus, in a ternary expression, which takes the general form:
lhs = test ? v_if_true : v_if_false;
the expression test is first evaluated and lhs is assigned either v_if_true (if test evaluates to non-zero) or v_if_false (if test evaluates to zero).
In your case, test is --b. The consequence of this expression is that the value of b is decreased by one, and the value of the expression is the resulting (modified) value of b (which will be zero in your code).
So, your ternary expression assigns to a the value of the v_if_false expression which is b = -99 (the brackets you give add clarity to the expression, but don't change its evaluation). So, as mentioned above, this expression (as well as modifying b) also has a calculated value of -99, which is then given to a.
If you wanted to leave a unchanged in the case when the v_if_false expression is executed, then you could do this using the comma operator, as follows (though I would not recommend using such code):
a = --b ? b : ((b = -99), a); // If --b == 0, this becomes a = a
This works because the value of an expression containing the comma operator is the value of the sub-expression after (to the right of) the comma.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I have to do trace tables for this but I don't understand everything from the code such as a==b; c-=--d; b+=a%10
int a=3,b=4, c=5,d=6;
if(a==b)c++;else c--;
while(d>2){
c-=--d; b-=a;
}
int a=3,b=0, c=7,d=5;
if(a=b)d++;else c--;
while(d>2){
c+=a;a+=--d;
}
int a=3,b=11, c=15,d=5;
if(a>b)d--;else c--;
for(;d>3;c/=a){
b=--d;
}
int a=31,b=14, c=95,d=56;
if(a<b)d++;else c--;
while(d>53){
b+=a%10;d--; a/=10;
}
c -= --d;
mean
c = c - (--d);
The same goes for
b += a%10
which mean
b = b + (a%10)
You can do the same with *, / and % operator
--d mean that d is decreased before the instruction get executed, ++d would mean d will be increased before the instruction got executed
if(a==b)c++;else c--;
is the same as the more readable
if (a==b)
{
c = c + 1;
}
else
{
c = c - 1;
}
int a=3,b=4, c=5,d=6;
if(a==b) c++;
else c--;
while(d>2){
c-=--d; b-=a;
}
The == operator means comparasion if a and b are the same. In this case a and b are different, so it goes to the else and decrements the c variable for 1.
The -- after c means that the value is decreased before the instruction gets executed.
So, that means c will become 4.
While d > 2, means it will loop as long as d > 2.
c -= --d; b-=a;
That means:
c = c - --d
b = b - a
So, that means the value of d will decrease by 1 each time the loop is executed and will keep looping until it's >2.
int a=3,b=0, c=7,d=5;
if(a=b)d++;
else c--;
while(d>2){
c+=a;a+=--d;
}
The = operator means to assign a value, so when it executed a=b, it will assign the value of b to a but since it's 0, which means false it will go to else and decrease the value of c by 1.
So c will become 6.
The while loop is similar to the first one.
int a=3,b=11, c=15,d=5;
if(a>b)d--;else c--;
for(;d>3;c/=a){
b=--d;
}
This one is quite simple if-statement. It checks whether a is bigger than b. If so, it executes d--, else c--.
As for the for-loop, it goes until d > 3 and it executes c /= a each time as well.
c /= a also can be written as c = c / a.
int a=31,b=14, c=95,d=56;
if(a<b)d++;else c--;
while(d>53){
b+=a%10;d--; a/=10;
}
The last one if statement is simple as well and similar to the above one.
The while loop will be executed until d > 53.
The command inside b+=a%10;d--; a/=10; can be also written as:
b = b + a % 10
d--
a = a / 10
a==b
This is an important Boolean condition that checks whether a is equal to b and returns true if it is and returns false if it is not. This can be changed (casted) to an int of value 1 or 0.
c-=--d;
I wouldn't prefer writing such code.
However c -= k statement is equivalent to c = c-k
and --d; is same as decrement d by 1.
The code does these two things in one statement. But order is important. Since -- comes before d, first this decrement is evaluated and then the -= operator is considered.
Similarly for +=
I would suggest looking up a good C++ learning resource such as www.cplusplus.com or www.learncpp.com
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.
Is there a c++ operator that i could use for a for loop where it would add or subtract to variables based on whether one of the variables is less than or greater 0.
For instance
int a;
int b;
for(int i=0;i<some_number; i++)
result = a +< b
result = a-> b
No.
You can combine with the ?: operator.
int a;
int b;
for(int i=0;i<some_number; i++)
result = (a < b)? result+b:result-b;
That is if I understood your example correctly.
-> is an existing dereference operator.
Operator ?: is an equivalent to the if...else construct. If the statement before ? evaluates to true, the statement right after the ? gets executed, otherwise the statement after the : gets executed.
Do you want something like this?
result += a > 0 ? b : -b;
Note that this will subtract b if a == 0, which isn't quite what you asked for.
Not directly, but the ternary operator is close.
for(int i=0;i<some_number; i++)
result = (a > 0)?(a):(b);
This line will be equivalent to result = a when a is greater than 0, and result = b elsewise.
It could also be written as result = a?a:b;, but the longer form is more readable.
Not sure if this would be any help?
result = a + (b*(a < b));
result = a - (b*(a > b));
Basically, (a < b) is converted into a boolean, which is basically either 1 (true) or 0 (false). b multiplied by 0 is of course zero, so nothing is added, and b multiplied by 1 is exactly b's value.