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
int a=1,b=2,c=3;
int x=1;
int y=10;
a = x ? b : c;
cout<< a; // Outputs 2 (the value of b)
a = y ? b : c;
cout<< a; // Outputs 2 (the value of b)
Now, look at the following.
a=0;
x=0;
a = x ? b : c;
cout<< a; // Outputs 3 (the value of c !!!!)
Why this unusual behaviour ?? Only when a and x are both 0, the expression evaluates to false , otherwise, always it is true. Please explain.
Because x is 0.
Recall that the ternary operator, if written condition ? a : b returns a if condition is true and b otherwise. You are using it with numbers, and any number except 0 is considered true as a boolean.
x ? b : c in your case is 0 ? 2 : 3, and since 0 is false, it evaluates to 3. That 3 then gets assigned to your a variable and printed - nothing unusual going on here.
This looks perfectly fine. The expression a = x ? b : c is equivalent to
if (x)
a = b;
else
a = c;
x will evaluate to true for any nonzero value, so if you assign 0 to x prior to executing the expression, the value of c will be assigned to a, and if you assign 1 to x prior to executing the expression, the value of b will be assigned to a. The prior value of a is immaterial here.
The reason is that in C and C++ any non-zero value is evaluated as "true".
The value of 'a' only depends on the value of 'x' and 'y'. Since initially 'x' and 'y' are both greater than 0, the condition evaluates to true and you get the value of 'b' in 'a'.
In the second case, 'x' is zero which evaluates the condition to false which causes 'a' to have the value of 'c'.
a = x ? b : c;
is the same as
if(x != 0) {
a = b;
} else {
a = c;
}
Therefore if you set x = 0 you will get a = c.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
basically I have this function repeat I understand what it does, what I don't understand is the this part of the condition in the for loop v ? sizeof(g) / 8 : 0, if someone could explain it me I would appreciate a lot because I don't get what it does.
program:
char g[1024];
double *repeat(double *v) {
for (int i = 0; i < (v ? sizeof(g) / 8 : 0); i++)
{
if (v[i] > 2 * i)
v[i] = i;
else
v[i] = 0;
}
return v;
}
int main()
{
double *t;
*t = 2.0;
t = repeat(t);
printf("Numero: %f\n",*t);
return 0;
}
This is somewhat obscure code, as illustrated by the number of incorrect attempts at answers. The key here is that the ?: operator makes an expression, that is, it's code that produces a value. So, first look at the value of the expression:
(v ? sizeof(g) / 8 : 0)
where v is a pointer. When a pointer is used in a context that expects a boolean value, the value is true if the pointer is not a null pointer, and false if it is. So, you could think of this expression as
(v != nullptr ? sizeof(g) / 8 : 0)
This expression is the upper bound of the for loop. The loop will execute for as long as i < (v ? sizeof(g) / 8 : 0) is true. So, if v is a null pointer, the condition is i < 0, which is false, and the body of the loop will not be run. If v is not a null pointer, the condition is i < sizeof(g)/8, and the body of the loop will be run as many times as whatever that value is.
If v is true, then sizeof(g) / 8 gets executed. If v is false, then 0 gets executed
If the input parameter v is not NULL, then we compare i to the result of sizeof(g) / 8 (64 in this particular case); otherwise we compare i to 0.
The following code would be equivalent (and probably a little easier to understand):
int count = 0;
if ( v )
count = sizeof g / 8;
for ( int i = 1; i < count; i++ ) { ... }
Since you say in a comment you are confused because v is a pointer, not a boolean — and yet is used in a condition:
Since the beginning of C++ there is a well-defined automatic conversion of pointers to booleans which is defined exactly for this use case of testing whether a pointer is null instead of the wordy if(p != 0 ). The latter is more complicated hence more error prone:
OK, we compare against a null pointer (in C++ a literal zero was the only null pointer literal before the introduction of nullptr).
OK, we test for inequality.
OK, two negations (null pointer and inequality) make for a positive: Is the pointer there? I think that's what it is. I'm tired. Let's be sure and play it through in our minds. I have a valid pointer. It is not equal to 0. Inequality therefore returns false. The false result indicates it's a valid pointer. I think this is what I wanted, right? Where was I?
Instead , you simply write if(p). no comparison, no double logic inversion, it couldn't be simpler: If the pointer is there, do something with it ;-).
i < (v ? sizeof(g) / 8 : 0)
This is the equivalent of saying this shorthand:
if(v){
i < sizeof(g)/8;
}else{
i < 0;
}
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.
Here is a part of code
int main()
{
int x=5,y=10;
if(x=!y)
{
cout<<"h";
}
else
{
cout<<"p";
}
getch();
}
The output was p, please explain, how the code works and the meaning of x=!y.
Looks like a typo that produces valid code. Expanding it helps--
if (x = (!y))
Since y is 10, !y == 0, and assignments themselves produce a value. In particular the value of x = 0 is 0, so the test evaluates to 0 and that's why you get the result.
But this is a crazy thing to write in this context, presumably what was, or what should have been intended was
if (x != y)
I.e., not-equals.
x=!y is an assignment.
x is being assigned the value of !y expression, which is a logical "NOT" operation. This operation returns true if the operand is zero, or false otherwise. The value true becomes 1 when assigned back to int; false becomes zero.
In C and C++ it is OK to use assignment expressions inside if conditionals and other control statements, such as for and while loops. The value being assigned is used to evaluate the condition, and the assignment itself is performed as a side effect. In this case, the condition is !y.
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:
In C++ what causes an assignment to evaluate as true or false when used in a control structure?
(6 answers)
Closed 5 years ago.
int a = 5;
if(a = 5)
{
cout<<"111111"<<endl;
}
if(a = 0)
{
cout<<"22222"<<endl;
}
the reslut is
111111
Press any key to continue
acrroding to some commments,assign success,the result is true。
"a = 0" and a = 1" should assign success.but the result is only the first executed....
why ?
Some comments? That seems dodgy and in this case is incorrect.
The result of operator= on ints is the value that has been assigned. In this case a = 5 results in 5 and a = 0 results in 0. Since 5 evaluates as true you see "111111" but since 0 evaluates as false you don't see "22222".
As for why assignment results in the value being assigned, take the case with multiple assignments:
a = b = 5;
This is the same as:
a = (b = 5);
So for this to work as expected (b = 5) must return 5.
The result of a=5 is 5, and the result of a=0 is 0, so your program is like:
int a = 5;
if(5)
{
cout<<"111111"<<endl;
}
if(0)
{
cout<<"22222"<<endl;
}
Since the if statement requires a boolean value, 5 converts to true and 0 converts to false implicitly. so your program is now like:
int a = 5;
if(true)
{
cout<<"111111"<<endl;
}
if(false)
{
cout<<"22222"<<endl;
}
So it will print "111111" only.
"acrroding to some commments,assign success,the result is true"
The result of assignment is the result of expression in the left side.
"a = 0" and a = 1" should assign success.but the result is only the first executed....
Nope. 0 is false, you may as well have written"
if(0) { /* some code which will never execute */ }
The if statement evaluates the result of the expression between the parentheses, which means that the expression itself must be evaluated completely before the condition is checked.