Why this code produce 1? Someone, describe it for me pls.
#include <iostream>
using namespace std;
int main(){
int x = 0;
int y = 0;
if (x++&&y++){
y += 2;
}
cout << x + y << endl;
return 0;
}
Initially x and y are 0
Therefore x++ evaluates to false, and the second operand of && is never evaluated. x++ does increment x to 1. Since the condition is false, the conditional branch is not entered.
x + y is 1 + 0 which equals 1
user2079303 explains nicely (+1 by me already), as extension, I'll go a little more into detail:
if(x++) evaluates the value of x before the incrementation, so this little piece of code is equivalent to the following (need to buffer the old value!):
int tmp = x;
x++;
if(tmp)
Be aware that within c && cc, the second condition cc is not evaluated any more if c is already false! So if(x && y) is equivalent to
if(x)
{
if(y)
{
// ...
}
}
Putting all this together, your code is equivalent to this variant, where I separated the if clause into code lines each one containing only one single instruction:
int x = 0;
int y = 0;
int tmp = x;
x++;
if(tmp)
{
tmp = y;
y++;
if(tmp)
y += 2;
}
Suppose, your output now is quite obvious...
Related
I have this C code that I was working on and honestly I do not understand how it exactly works (I never used if statements like this before). Here is the code:
#include<stdio.h>
int main(int argc, char const *argv[])
{
int x = 2, y = 0;
if (x = ++y)
{
printf("%d is equal to %d\n", x,y);
}
else
{
printf("%d is not equal %d", x, y);
}
}
For some reason it seems that the assignment operator is also acting as an equality operator? Because when it evaluates X would be 1 and Y would be 1 and therefore it'll go to the first body, inside if. However if I do x = y++, it will go to the second statement. Just confused me a bit because isn't equality supposed to be == Not =
Here is a semantic fix of your code:
#include<stdio.h>
int main(int argc, char const *argv[])
{
int x = 2, y = 0;
if (x = ++y)
{
printf("y has been incremented and copied to x, which is now non-zero\n");
}
else
{
printf("y has been incremented and copied to x, which is now zero\n");
}
}
and
#include<stdio.h>
int main(int argc, char const *argv[])
{
int x = 2, y = 0;
if (x = y++)
{
printf("y has been copied to x and then y incremented, x is now non-zero (y was non-zero)\n");
}
else
{
printf("y has been copied to x and then y incremented, x is now zero (y was zero)\n");
}
}
As a general principle I advocate the use of Yoda expressions where a non-assignable value is put on the left e.g. if (1 == x). This guards against accidental assignment.
I am not able to understand, why the output of the code is not what I was expecting.
#include <iostream>
using namespace std;
int main()
{
int m = 2, n = 6;
int &x = m;
int &y = n;
m = x++;
x = m++;
n = y++;
y = n++;
cout<< m << " " << n;
return 0;
}
I was expecting 4 8
This line:
m = x++;
is equivalent to:
x = x++;
since m is a reference to x.
From c++17, the right hand side is evaluated first, resulting in 2. Then x is incremented to 3. Then the assignment of the right hand side value to the left hand side is done. But this uses the old value of the right hand side, which is 2. So the above statement effectively does nothing.
Before c++17,
m = x++;
is undefined behavior.
Notice how the operators are post-increment and not pre-increment... You're basically doing nothing.
m = x++; means that increment x (i.e. m) but return the old value of x (i.e. m). Assignment takes place after the increment and return of old value and the old value is what ends up getting assigned. So, you end up essentially with a bunch of self-assignments.
#include <iostream>
using namespace std;
int main()
{
double x = 1;
double y = 2;
int i = 1;
do
{
y /= 2.0;
x+= y;
++i;
cout << i;
}
while (x < 2.4);
}
I thought the output would be 2, but that is not correct. Can someone explain why?
Have a look at the condition which controls whether or not your loop will execute again. At the end of the first loop, the value of x is 2.0. Since this is less than 2.4, the loop runs a second time. In total that means i is incremented twice raising it to 3.
My goal for this program is to get input from the user for value 'y' and then multiply that value by 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and then print the 10 results. Right now, when the user enters a value for 'y', the program just returns a list of zeros. Any idea what I'm doing wrong?
#include <iostream>
using namespace std;
int main(){
int x;
int y;
int z;
x = 0;
z = x * y;
cout << "enter y" << endl;
cin >> y;
while (x < 10) {
cout << z << endl;
x = x + 1;
}
}
The problem you have is that you don't actually calculate z inside the loop, so it will always be the value you calculated outside the loop. Statements and expressions can't be done retroactively.
But you have a worse problem than that, because you use y before you initialize it, which leads to undefined behavior.
You need to change the location of the y input to be before the calculation of z that should be inside the loop
The statement z = x * y should be inside your while loop for it to work.
What is happening now is that,each time it is printing the same value of z which is calculated initially as x(which is 0) and y(which is some garbage value as it has not been initialized) to give 0
The first problem occurring in your code is that try to assign z by multiplying x with y, but y is not assigned to a certain value => ERROR! That means you should get your user input before you assign z.
You may wonder why you get ten outputs of '0': Inside the loop you only print the value assigned to z to the console but z is already assigned before and not changed during the repetition.
#include <iostream>
using namespace std;
int main(){
int x;
int y;
int z;
x = 0;
z = x * y; // y is never assigned to a value
cout << "enter y" << endl;
cin >> y; // y got assigned here!
while (x < 10) {
cout << z << endl; // z stays the same in the loop
x = x + 1;
}
}
I am trying to implement a simple compiler using flex & bison, and got stuck in the postfix notation.
(The compiler should behave like the C++ compiler)
Here is the problem:
Given the following code:
int x = 0;
int y = x++ || x++ ; //y=1 , x = 2 this is understandable
int z = x++ + x++ ; // z = 0 , x=2
the first line is fine because of the following grammar:
expression = expression || expression; // x=0
expression = 0 || expression // x= 1
expression = 0 || 1 //x=2
expression = 1 // x=2
y = 1
However, I don't understand why z=0.
When my bison grammar sees 'variable' ++ it first returns the variables value, and only then increments it by 1. I used to think thats how C++ works, but it won't work for the 'z' variable.
Any advice on how to solve this case?
int z = x++ + x++;
Although z may appear to be 0, it is not, it could in fact be any value and will depend entirely upon the compiler you are using. This is because the assignment of z has undefined behaviour.
The undefined behaviour comes from the value of x being changed more than once between sequence points. In C++, the || operator is a sequence point which is why the assignment of y is working as expected, however the + operator is not a sequence point.
There are of course various other sequence points in C++, the ; being a more prominent example.
I should also point out that the ++ operator returns the previous value of the variable, that is in this example
#include <iostream>
using namespace std;
int main() {
int x = 0;
int y = x++;
cout << y << endl;
return 0;
}
The value printed out for y is 0.
Saying the same thing another way. A C compiler is free to implement
int z = x++ + x++;
as either
z = x + x
incr x
incr x
or as
int r1 = x;
incr x
z = r1 + x
incr x
Your compiler appears to be using the first plan.