i am just learning c++ programming language and stuck at some point will anyone help me out. the problem is i had google some stuff and came to know that if conditions can change the varaibles value for temporary basis. my code is below.
#include <iostream>
using namespace std;
int main()
{
int a = 2;
int b = a + 1;
if ((a = 3) == b)
{
cout << a;
}
else
{
cout << a + 1;
}
return 0;
}
in the above code its printing the else block why not the if block the conditions must be true ?
You are mistaken.
If you will change your code the following way
then you will see the output
In the expression of the if statement
the left operand of the equality operator is evaluated. As a result
a
becomes equal to3
and in turn is equal tob
.If your compiler supports the C++ 17 Standard then you could declare the variables inside the if statement like